package com.its.dsrc.scheduler;

import com.its.app.utils.Elapsed;
import com.its.app.utils.StatisticsTime;
import com.its.dsrc.config.DsrcConfig;
import com.its.dsrc.config.ProcessConfig;
import com.its.dsrc.service.RseCtlrService;
import com.its.dsrc.service.RseSectService;
import com.its.dsrc.service.StatisticsServices;
import com.its.dsrc.service.UnitSystService;
import com.its.dsrc.xnettcp.dsrc.process.service.MultiMediaDataService;
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 ProcessConfig processConfig;
    private final DsrcConfig dsrcConfig;
    private final UnitSystService unitSystService;
    private final RseCtlrService rseCtlrService;
    private final RseSectService rseSectService;
    private final StatisticsServices statisticsServices;

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

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

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

    @Scheduled(cron = "30 0/5 * * * *")  // DSRC Multimedia 작업 실행 ==> 교통정보 가공이 완료된 후에 실행
    public void MakeMultimediaJobSchedule() {
        if (!this.processConfig.isStartSchedule()) {
            return;
        }

        if (this.dsrcConfig.isPublicationMultimedia()) {
            Elapsed elapsed = new Elapsed();
            log.info("     MakeMultimediaJobSchedule: start. {}", Thread.currentThread().getName());
            MultiMediaDataService.getInstance().makeMultimediaData();
            log.info("     MakeMultimediaJobSchedule: ..end. {} ms. {}", elapsed.milliSeconds(), Thread.currentThread().getName());
        }
    }

    @Scheduled(cron = "40 0/5 * * * *")    // 정주기 5분 40초에 스케쥴 실행
    public void StatisticsSchedule() {
        if (!this.processConfig.isStartSchedule()) {
            return;
        }
        Elapsed elapsed = new Elapsed();
        log.info("            StatisticsSchedule: start. {}", Thread.currentThread().getName());
        if (this.processConfig.isStatistics()) {
            StatisticsTime its = new StatisticsTime();
            its.init();
            its.setProcessing(true);

            if (its.isStatHour()) {
                // 매시 5분 가공완료후 1시간 통계
                this.statisticsServices.CRT_TB_RSE_OBU_CLCT_STAT_HH(its.getStatHourTime(), its.getStatHourTime(), its.getStatHourTo());
            }

            if (its.isStatDay()) {
                // 00시 10분 가공이 끝나면 이전일의 통계 정보를 가공.
                this.statisticsServices.CRT_TB_RSE_OBU_CLCT_STAT_DD(its.getStatDayFrom(), its.getStatDayFrom(), its.getStatDayTo());
            }
            its.setProcessing(false);
        }
        log.info("            StatisticsSchedule: ..end. {} ms. {}", elapsed.milliSeconds(), Thread.currentThread().getName());
    }

}
