package com.viettel.campaign.config;

import com.viettel.campaign.job.CampaignJob;
import com.viettel.campaign.service.ApParamService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;

import java.util.Date;
import java.util.concurrent.ScheduledFuture;

/**
 * @author hanv_itsol
 * @project campaign
 */

@Slf4j
@EnableScheduling
@Configuration
public class JobConfig implements SchedulingConfigurer {

    TaskScheduler taskScheduler;
    private ScheduledFuture<?> job1;
    private ScheduledFuture<?> job2;

    @Bean
    public CampaignJob campaignJob(){
        return new CampaignJob();
    }

//    @Override
//    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
//
//        ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
//        threadPoolTaskScheduler.setPoolSize(10);
//        threadPoolTaskScheduler.setThreadNamePrefix("scheduler-thread");
//        threadPoolTaskScheduler.initialize();
//        job1(threadPoolTaskScheduler);
//        job2(threadPoolTaskScheduler);
//        this.taskScheduler = threadPoolTaskScheduler;
//        taskRegistrar.setTaskScheduler(threadPoolTaskScheduler);
//    }

    private void job1(TaskScheduler scheduler) {
        job1 = scheduler.schedule(() -> {
            log.info("processing job1 ...");
            log.info(Thread.currentThread().getName() + " The Task1 executed at " + new Date());
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }, triggerContext -> {

            String cronExp = "0/5 * * * * ?";// Can be pulled from a db .
            return new CronTrigger(cronExp).nextExecutionTime(triggerContext);
        });
    }

    private void job2(TaskScheduler scheduler) {
        job2 = scheduler.schedule(() -> {
//            log.info("processing job2 ...");
//            log.info(Thread.currentThread().getName() + " The Task2 executed at " + new Date());
//            campaignJob().process();

        }, triggerContext -> {
            String cronExp = "0/1 * * * * ?";// Can be pulled from a db . This will run every minute
            return new CronTrigger(cronExp).nextExecutionTime(triggerContext);
        });
    }



    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
        threadPoolTaskScheduler.setPoolSize(10);
        threadPoolTaskScheduler.setThreadNamePrefix("scheduler-thread");
        threadPoolTaskScheduler.initialize();
        scheduledTaskRegistrar.setTaskScheduler(threadPoolTaskScheduler);
//        scheduledTaskRegistrar.addTriggerTask(() -> campaignJob().process(), (TriggerContext triggerContext) -> yourService.getCron());
        scheduledTaskRegistrar.addTriggerTask(() -> campaignJob().process(), triggerContext -> {
            CronTrigger trigger = new CronTrigger("0/5 * * * * ?");
            return trigger.nextExecutionTime(triggerContext);
        });
    }
}
