package com.its.vms.scheduler;

import com.its.app.utils.Elapsed;
import com.its.vms.config.ApplicationConfig;
import com.its.vms.service.*;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PreDestroy;

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

    private final ApplicationConfig config;
    private final UnitSystService unitSystService;
    private final VmsCtlrService vmsCtlrService;
    private final VmsManageService vmsManageService;
    private final VmsSymbService symbService;
    private final AppRepositoryService repoService;

    @PreDestroy
    public void onShutDown() {
         this.unitSystService.updateUnitSystStts(false);
    }

    @Scheduled(cron = "5 * * * * *")  // 1분주기 작업 실행
    public void scheduleUnitSystStts() {
        if (!this.config.isStartSchedule()) {
            return;
        }
        Elapsed elapsed = new Elapsed();
        log.info("scheduleUnitSystStts: start. {}", Thread.currentThread().getName());
        // 1. 프로세스 상태정보 업데이트
        this.unitSystService.updateUnitSystStts(true);
        // 2. 제어기 상태정보 업데이트
        this.vmsCtlrService.updateCtlrStts(true, null);
        log.info("scheduleUnitSystStts: ..end. {} ms. {}", elapsed.milliSeconds(), Thread.currentThread().getName());
    }

    @Scheduled(cron = "15 * * * * *")  // 1분주기 작업 실행
    public void scheduleVmsOnOffControl() {
        if (!this.config.isStartSchedule()) {
            return;
        }
        Elapsed elapsed = new Elapsed();
        log.info("scheduleVmsOnOffControl: start. {}", Thread.currentThread().getName());
        this.vmsManageService.loadVmsOnOffTime();
        log.info("scheduleVmsOnOffControl: ..end. {} ms. {}", elapsed.milliSeconds(), Thread.currentThread().getName());
    }

    @Scheduled(cron = "30 0/5 * * * *")    // 정주기 5분 30초에 스케쥴 실행
    public void schedule5M() {
        if (!this.config.isStartSchedule()) {
            return;
        }
        Elapsed elapsed = new Elapsed();
        log.info("scheduleVmsFormDownload..: start. {}", Thread.currentThread().getName());
        this.repoService.setStaticCycle(true);
        this.vmsManageService.jobMakeDownloadVmsForm();
        log.info("scheduleVmsFormDownload..: ..end. {} ms. {}", elapsed.milliSeconds(), Thread.currentThread().getName());
    }

    @Scheduled(cron = "10 3,8,13,18,23,28,33,38,43,48,53,58 * * * *")  // 5분주기 작업 실행
    public void scheduleVmsDnldSymb() {
        if (!this.config.isStartSchedule()) {
            return;
        }
        Elapsed elapsed = new Elapsed();
        log.info("scheduleVmsDnldSymb: start. {}", Thread.currentThread().getName());
        this.symbService.loadVmsScheSymbLib();
        log.info("scheduleVmsDnldSymb: ..end. {} ms. {}", elapsed.milliSeconds(), Thread.currentThread().getName());
    }

}
