package com.samsung.config;

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.boot.autoconfigure.quartz.QuartzDataSource;
import org.springframework.boot.autoconfigure.quartz.QuartzProperties;
import org.springframework.boot.autoconfigure.quartz.QuartzTransactionManager;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.scheduling.quartz.SpringBeanJobFactory;

import javax.sql.DataSource;
import java.util.Properties;

@Configuration
@Profile({"xxx"})
public class QuartzClusteringConfig {

    final
    KnoxSyncBatchConfig knoxSyncBatchConfig;
    final
    SysUseLogBatchConfig sysUseLogBatchConfig;
    final
    UserBatchConfig userBatchConfig;

    final
    QuartzProperties quartzProperties;

    public QuartzClusteringConfig(KnoxSyncBatchConfig knoxSyncBatchConfig, SysUseLogBatchConfig sysUseLogBatchConfig, UserBatchConfig userBatchConfig, QuartzProperties quartzProperties) {
        this.knoxSyncBatchConfig = knoxSyncBatchConfig;
        this.sysUseLogBatchConfig = sysUseLogBatchConfig;
        this.userBatchConfig = userBatchConfig;
        this.quartzProperties = quartzProperties;
    }

    @Bean
    public SchedulerFactoryBean setSchedulerFactoryBean(ApplicationContext applicationContext) {
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();

        AutoWiringSpringBeanJobFactory jobFactory = new AutoWiringSpringBeanJobFactory();
        jobFactory.setApplicationContext(applicationContext);

        schedulerFactoryBean.setJobFactory(jobFactory);
        schedulerFactoryBean.setDataSource(quartzDataSource());
        schedulerFactoryBean.setApplicationContext(applicationContext);
        schedulerFactoryBean.setTransactionManager(quartzTransactionManager());
        schedulerFactoryBean.setWaitForJobsToCompleteOnShutdown(true);
        schedulerFactoryBean.setStartupDelay(60);
        schedulerFactoryBean.setOverwriteExistingJobs(true);

        Properties properties = new Properties();
        properties.putAll(quartzProperties.getProperties());
        schedulerFactoryBean.setQuartzProperties(properties);

        schedulerFactoryBean.setTriggers(
                knoxSyncBatchConfig.knoxApprovalSyncTrigger().getObject(),
                userBatchConfig.batchUserLongTermCheckTrigger().getObject(),
                userBatchConfig.batchUserAuthExpiredTrigger().getObject(),
                sysUseLogBatchConfig.batchSysUseLogTrigger().getObject(),
                sysUseLogBatchConfig.batchMenuUseHistoryTrigger().getObject()
        );

        return schedulerFactoryBean;
    }

    @Bean
    @ConfigurationProperties(prefix = "spring.quartz.datasource")
    @QuartzDataSource
    public DataSource quartzDataSource() {
        //return DataSourceBuilder.create().build();
        return DataSourceBuilder.create().type(HikariDataSource.class).build();
    }

    @Bean
    @QuartzTransactionManager
    public DataSourceTransactionManager quartzTransactionManager() {
        return new DataSourceTransactionManager(quartzDataSource());
    }
}
