package com.bailuntec;

import com.alibaba.druid.pool.DruidDataSource;
import com.bailuntec.job.SyncCostFirstAllJob;
import com.bailuntec.job.SyncCostFirstFinishJob;
import com.bailuntec.job.SyncCostFirstJob;
import com.bailuntec.listener.SyncCostFirstJobListener;
import com.bailuntec.utils.PropertiesUtil;
import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.event.JobEventConfiguration;
import com.dangdang.ddframe.job.event.rdb.JobEventRdbConfiguration;
import com.dangdang.ddframe.job.lite.api.JobScheduler;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.reg.base.CoordinatorRegistryCenter;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import lombok.extern.slf4j.Slf4j;

import javax.sql.DataSource;

@Slf4j
public class Application {
    private static PropertiesUtil propertiesUtil = PropertiesUtil.getInstance("job");
    private static final String EVENT_RDB_STORAGE_DRIVER = propertiesUtil.getPropertyAsString("EVENT_RDB_STORAGE_DRIVER");
    private static final String EVENT_RDB_STORAGE_URL = propertiesUtil.getPropertyAsString("EVENT_RDB_STORAGE_URL");
    private static final String EVENT_RDB_STORAGE_USERNAME = propertiesUtil.getPropertyAsString("EVENT_RDB_STORAGE_USERNAME");
    private static final String EVENT_RDB_STORAGE_PASSWORD = propertiesUtil.getPropertyAsString("EVENT_RDB_STORAGE_PASSWORD");

    public static void main(String[] args) {
        new JobScheduler(createRegistryCenter(), createJobConfiguration(), createJobEventConfiguration(), new SyncCostFirstJobListener()).init();
        new JobScheduler(createRegistryCenter(), createJobConfiguration2(), createJobEventConfiguration()).init();
        new JobScheduler(createRegistryCenter(), createJobConfiguration3(), createJobEventConfiguration()).init();
    }

    private static CoordinatorRegistryCenter createRegistryCenter() {
        CoordinatorRegistryCenter regCenter = new ZookeeperRegistryCenter(new ZookeeperConfiguration(propertiesUtil.getPropertyAsString("ZOOKEEPER_SERVER"), propertiesUtil.getPropertyAsString("NAME_SPACE")));
        regCenter.init();
        return regCenter;
    }

    private static LiteJobConfiguration createJobConfiguration() {
        JobCoreConfiguration simpleCoreConfig = JobCoreConfiguration.newBuilder(
                propertiesUtil.getPropertyAsString("JOB_NAME"),
                propertiesUtil.getPropertyAsString("JOB_CRON"),
                propertiesUtil.getPropertyAsInt("SHARDING_TOTAL_COUNT"))
                .build();
        SimpleJobConfiguration simpleJobConfig = new SimpleJobConfiguration(simpleCoreConfig, SyncCostFirstJob.class.getCanonicalName());
        return LiteJobConfiguration.newBuilder(simpleJobConfig).build();
    }

    private static LiteJobConfiguration createJobConfiguration2() {
        JobCoreConfiguration simpleCoreConfig = JobCoreConfiguration.newBuilder(
                propertiesUtil.getPropertyAsString("COST_FIRST_FINISH_JOB_NAME"),
                propertiesUtil.getPropertyAsString("COST_FIRST_FINISH_JOB_CRON"),
                propertiesUtil.getPropertyAsInt("SHARDING_TOTAL_COUNT"))
                .build();

        SimpleJobConfiguration simpleJobConfig = new SimpleJobConfiguration(simpleCoreConfig, SyncCostFirstFinishJob.class.getCanonicalName());
        return LiteJobConfiguration.newBuilder(simpleJobConfig).build();
    }

    private static LiteJobConfiguration createJobConfiguration3() {
        JobCoreConfiguration simpleCoreConfig = JobCoreConfiguration.newBuilder(
                propertiesUtil.getPropertyAsString("COST_FIRST_ALL_JOB_NAME"),
                propertiesUtil.getPropertyAsString("COST_FIRST_ALL_JOB_CRON"),
                propertiesUtil.getPropertyAsInt("SHARDING_TOTAL_COUNT"))
                .build();

        SimpleJobConfiguration simpleJobConfig = new SimpleJobConfiguration(simpleCoreConfig, SyncCostFirstAllJob.class.getCanonicalName());
        return LiteJobConfiguration.newBuilder(simpleJobConfig).build();
    }

    private static JobEventConfiguration createJobEventConfiguration() {
        return new JobEventRdbConfiguration(setUpEventTraceDataSource());
    }

    private static DataSource setUpEventTraceDataSource() {
        DruidDataSource result = new DruidDataSource();
        result.setDriverClassName(EVENT_RDB_STORAGE_DRIVER);
        result.setUrl(EVENT_RDB_STORAGE_URL);
        result.setUsername(EVENT_RDB_STORAGE_USERNAME);
        result.setPassword(EVENT_RDB_STORAGE_PASSWORD);
        return result;
    }

}
