package egovframework.com.its.web.server.scheduler;

import egovframework.com.its.web.server.service.CommonService;
import egovframework.com.its.web.server.util.TrafficData;
import lombok.RequiredArgsConstructor;
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.PostConstruct;
import javax.annotation.PreDestroy;
import java.text.SimpleDateFormat;
import java.util.Date;

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

    private final CommonService commonService;

    public String getSysTime() {
        SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date dtLog = new Date();
        return sdfDate.format(dtLog);
    }

    @Scheduled(cron = "30 1,3,6,8,11,13,16,18,21,23,26,28,31,33,36,38,41,43,46,48,51,53,56,58 * * * ?")  // 5분주기 작업 실행
    public void trafficSchedule() {
        loadTrafficVertex();
    }

    @PostConstruct
    private void init() {
        loadTrafficVertex();
    }

    @PreDestroy
    public void preDestroy() {

    }

    private void loadTrafficVertex() {
//        log.info("TrafficSchedule: loadTrafficVertex START: {}", getSysTime());

        long startTime = System.currentTimeMillis();
        TrafficData.setRoadVertexArr(this.commonService.getRoadVertexArr());
        long endTime = System.currentTimeMillis();
//        log.info("TrafficSchedule: Load ROAD VERTEX ARR: {} ms.", endTime - startTime);

        startTime = System.currentTimeMillis();
        TrafficData.setIfscVertexArr(this.commonService.getIfscVertexArr());
        endTime = System.currentTimeMillis();
//        log.info("TrafficSchedule: Load IFSC VERTEX ARR: {} ms.", endTime - startTime);
//        startTime = System.currentTimeMillis();
//        TrafficData.setLink(this.commonService.getLinkVertexArr());
//        endTime = System.currentTimeMillis();
//        log.info("TrafficSchedule: Load LINK VERTEX ARR: {} ms.", endTime - startTime);
//        log.info("TrafficSchedule: loadTrafficVertex ..END: {}", getSysTime());
    }
}
