package com.its.vms.scheduler;

import com.its.app.utils.Elapsed;
import com.its.vms.config.ApplicationConfig;
import com.its.vms.service.AppRepositoryService;
import com.its.vms.service.UnitSystService;
import com.its.vms.service.VmsCtlrService;
import com.its.vms.service.VmsManageService;
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 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("unitSystStts: start. {}", Thread.currentThread().getName());
        // 1. 프로세스 상태정보 업데이트
        this.unitSystService.updateUnitSystStts(true);
        // 1. 제어기 상태정보 업데이트(제어기/프로세스 상태정보 UDP 전송)
        this.vmsCtlrService.updateCtlrStts(true, null);
        log.info("unitSystStts: ..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.repoService.setStaticCycle(false);
        this.vmsManageService.loadVmsOnOffTime();
        this.vmsManageService.jobMakeDownloadVmsForm();;
        log.info("scheduleVmsOnOffControl: ..end. {} ms. {}", elapsed.milliSeconds(), Thread.currentThread().getName());
    }

    @Scheduled(cron = "50 4,9,14,19,24,29,34,39,44,49,54,59 * * * *")  // 5분주기 작업 실행(DSRC 구간 소통정보 생성)
    public void scheduleMakeSectTraf() {
        if (!this.config.isStartSchedule()) {
            return;
        }
        Elapsed elapsed = new Elapsed();
        log.info("makeSectTraf: start. {}", Thread.currentThread().getName());
        //this.rseSectService.makeDsrcSectTraffic();
        log.info("makeSectTraf: ..end. {} ms. {}", elapsed.milliSeconds(), Thread.currentThread().getName());
    }

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

    @Scheduled(cron = "0/10 * * * * *")  // 10초 주기 작업 실행
    public void scheduleFrED() {
        if (!this.config.isStartSchedule()) {
            return;
        }
//        this.rseCtlrService.monitoringSession();
    }

}
