package com.its.vds.stat.server.controller;

import com.its.vds.stat.server.service.VdsStatServerService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Slf4j
@Controller
@RequiredArgsConstructor
public class VdsStatBatchServerController {

    private final VdsStatServerService service;

    public void run(String startDtStr, String endDtStr) {

        log.info("Start Batch Job: {} ~ {}", startDtStr, endDtStr);

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        LocalDateTime start = LocalDateTime.parse(startDtStr, formatter);
        LocalDateTime end = LocalDateTime.parse(endDtStr, formatter);

        while (!start.isAfter(end)) {
            // 문자열 변환
            String startStr = start.format(formatter);
            String plus5Str = start.plusMinutes(5).format(formatter);
            String plus5Minus1Str = start.plusMinutes(5).minusSeconds(1).format(formatter);

            log.info("------------------------------------------------------------");
            log.info("05MIN: {}, {}", startStr, plus5Minus1Str);

            int hour = start.getHour();
            int minute = start.getMinute();
            int day = start.getDayOfMonth();
            int month = start.getMonthValue();

            int result = 0;
            String start05MinFrom= start.plusMinutes(-5).format(formatter);
            String start05MinTo= start.minusSeconds(1).format(formatter);
            this.service.delVdsDtctHs(start05MinFrom);
            result = this.service.crtVdsDtctHs(start05MinFrom, start05MinFrom, start05MinTo);
            log.info(" 5MIN: {}, {}, {} EA.", start05MinFrom, start05MinTo, result);

            if (minute % 15 == 0) {
                String start15MinFrom= start.plusMinutes(-15).format(formatter);
                String start15MinTo= start.minusSeconds(1).format(formatter);
                this.service.delVdsDtct15MinStat(start05MinFrom);
                result = this.service.crtVdsDtct15MinStat(start15MinFrom, start15MinFrom, start15MinTo);
                log.info("15MIN: {}, {}, {} EA.", start15MinFrom, start15MinTo, result);
            }

            if (minute == 0) {
                String startHourFrom= start.plusMinutes(-60).format(formatter);
                String startHourTo= start.minusSeconds(1).format(formatter);
                this.service.delVdsDtctHhStat(startHourFrom);
                result = this.service.crtVdsDtctHhStat(startHourFrom, startHourFrom, startHourTo);
                log.info(" HOUR: {}, {}, {} EA.", startHourFrom, startHourTo, result);
            }


            if (hour == 0 && minute == 0) {
                String startDayFrom = start.minusDays(1).format(formatter);
                String startDayTo   = start.minusSeconds(1).format(formatter);
                this.service.delVdsDtctDdStat(startDayFrom);
                result = this.service.crtVdsDtctDdStat(startDayFrom, startDayFrom, startDayTo);
                log.info("  DAY: {}, {}, {} EA.", startDayFrom, startDayTo, result);

                String startMonthFrom = start.minusMonths(1).withDayOfMonth(1).format(formatter);
                String startMonthTo   = start.minusSeconds(1).format(formatter);
                result = this.service.crtVdsDtctMnStat(startMonthFrom, startMonthFrom, startMonthTo);
                log.info("MONTH: {}, {}, {} EA.", startMonthFrom, startMonthTo, result);

//                int lastYear = start.getYear() - 1;
//                String statYearFrom = String.format("%d0101000000", lastYear);
//                String statYearTo   = String.format("%d1231235959", lastYear);
//                log.info(" YEAR: {}, {}", statYearFrom, statYearTo);
            }

            // 5분 증가
            start = start.plusMinutes(5);
        }
        log.info("=========================================================");
    }
}
