package com.samsung.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.quartz.QuartzProperties;
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.scheduling.quartz.SchedulerFactoryBean;

import com.samsung.embo.cronJob.config.GhrpJobConfig;
import com.samsung.embo.formula.controller.FormulaController;

import lombok.extern.log4j.Log4j2;

import java.util.Properties;

@Configuration
/* @Profile({"local"}) */

public class QuartzConfig {
	private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger(QuartzConfig.class);

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

    final
    MboBatchConfig mboBatchConfig;

    final
    QuartzProperties quartzProperties;

    final
    GhrpJobConfig ghrpJobConfig;

    public QuartzConfig(KnoxSyncBatchConfig knoxSyncBatchConfig, SysUseLogBatchConfig sysUseLogBatchConfig, UserBatchConfig userBatchConfig, QuartzProperties quartzProperties,MboBatchConfig mboBatchConfig,GhrpJobConfig ghrpJobConfig) {
        this.knoxSyncBatchConfig = knoxSyncBatchConfig;
        this.sysUseLogBatchConfig = sysUseLogBatchConfig;
        this.userBatchConfig = userBatchConfig;
        this.quartzProperties = quartzProperties;
        this.mboBatchConfig =mboBatchConfig;
        this.ghrpJobConfig = ghrpJobConfig;
    }

    @Bean
    public SchedulerFactoryBean setSchedulerFactoryBean(ApplicationContext applicationContext) {

    	log.info("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
    	log.info("$$$$$$$$$$ start SchedulerFactoryBean local");
    	log.info("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();

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

        schedulerFactoryBean.setJobFactory(jobFactory);
        schedulerFactoryBean.setWaitForJobsToCompleteOnShutdown(true);
        schedulerFactoryBean.setStartupDelay(5);//60
        schedulerFactoryBean.setOverwriteExistingJobs(true);

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

		/*
		 * knoxSyncBatchConfig.knoxApprovalSyncTrigger().getObject(),
		 * userBatchConfig.batchUserLongTermCheckTrigger().getObject(),
		 * userBatchConfig.batchUserAuthExpiredTrigger().getObject(),
		 * sysUseLogBatchConfig.batchSysUseLogTrigger().getObject(),
		 * sysUseLogBatchConfig.batchMenuUseHistoryTrigger().getObject(),
		 */

        schedulerFactoryBean.setTriggers(

                ghrpJobConfig.recvDeptInfoTrigger().getObject(),
                ghrpJobConfig.recvUserInfoTrigger().getObject()

                //,mboBatchConfig.batchMbo001JobTrigger().getObject()
        );
        return schedulerFactoryBean;
    }
}
