package com.its.wthr.scheduler;

import com.its.app.utils.Elapsed;
import com.its.wthr.config.AtmpConfig;
import com.its.wthr.config.FrcsConfig;
import com.its.wthr.config.ServerConfig;
import com.its.wthr.service.AtmpService;
import com.its.wthr.service.FrcsService;
import com.its.wthr.service.UnitSystService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Slf4j
@EnableScheduling
@RequiredArgsConstructor
@Component
public class SchedulerTask {

    private final ServerConfig serverConfig;
    private final UnitSystService unitSystService;
    private final AtmpService atmpService;
    private final FrcsService frcsService;
    private final AtmpConfig atmpConfig;
    private final FrcsConfig frcsConfig;

    @Scheduled(cron = "2 * * * * ?")  // 1분주기 작업 실행
    public void unitSystSchedule() {
        if (!this.serverConfig.isStartSchedule()) {
            return;
        }
        Elapsed elapsed = new Elapsed();
        log.info("UnitSystSchedule :: start. {}", Thread.currentThread().getName());
        // 1. 프로세스 상태정보 업데이트
        try {
            this.unitSystService.updateUnitSyst(true);
        } catch (Exception e) {
            log.error("UnitSystSchedule :: ..end. {} ms. {}, ERROR: {}", elapsed.milliSeconds(), Thread.currentThread().getName(), e.getMessage());
        }

        //long jobElapsed = elapsed.milliSeconds();
        //if (jobElapsed > 3000) {
        //    log.info("UnitSystSchedule :: ..end. {} ms. {}", jobElapsed, Thread.currentThread().getName());
        //}
        log.info("UnitSystSchedule :: ..end. {} ms. {}", elapsed.milliSeconds(), Thread.currentThread().getName());
    }

    @Scheduled(cron = "${application.atmp.crontab:40 0/30 * * * *}")
    @Async("appJobExecutor")
    public void atmpSchedule() {
        if (!this.serverConfig.isStartSchedule()) {
            log.error("AtmpSchedule :: schedule setting not yet");
            return;
        }
        if (!this.atmpConfig.isEnabled()) {
            log.error("AtmpSchedule :: schedule is not enable.");
            return;
        }

        Elapsed elapsed = new Elapsed();
        log.info("AtmpSchedule :: start. {}", Thread.currentThread().getName());
        try {
            this.atmpService.doJob();
        } catch (Exception e) {
            log.error("AtmpSchedule :: ..end. {} ms. {}, ERROR: {}", elapsed.milliSeconds(), Thread.currentThread().getName(), e.getMessage());
        }
        log.info("AtmpSchedule :: ..end. {} ms. {}", elapsed.milliSeconds(), Thread.currentThread().getName());
    }

    @Scheduled(cron = "${application.frcs.crontab:50 0/30 * * * *}")
    @Async("appJobExecutor")
    public void frcsSchedule() {
        if (!this.serverConfig.isStartSchedule()) {
            log.error("FrcsSchedule :: schedule setting not yet");
            return;
        }
        if (!this.frcsConfig.isEnabled()) {
            log.error("FrcsSchedule :: schedule is not enable.");
            return;
        }

        Elapsed elapsed = new Elapsed();
        log.info("FrcsSchedule :: start. {}", Thread.currentThread().getName());
        try {
            this.frcsService.doJob();
        } catch (Exception e) {
            log.error("FrcsSchedule :: ..end. {} ms. {}, ERROR: {}", elapsed.milliSeconds(), Thread.currentThread().getName(), e.getMessage());
        }
        log.info("FrcsSchedule :: ..end. {} ms. {}", elapsed.milliSeconds(), Thread.currentThread().getName());
    }

}
