package com.cpp.web.service.reprtdata.impl;

import com.cpp.system.service.ISysConfigService;
import com.cpp.web.service.reprtdata.ReportDataService;
import com.cpp.web.utils.DateTimeUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
@RequiredArgsConstructor
@Slf4j
public class ReportDataServiceImpl implements ReportDataService {
    // 当前文件类型应上报文件个数
    @Override
    public int shouldFileNum(String fileType, Long startTime, Long endTime, String stationCode) {
        int num = 0;
        if (fileType.equals("dq") || fileType.equals("nwp")) {
            num = DateTimeUtil.getDaysBetweenTwoDate(startTime, endTime);
        } else if (fileType.equals("cdq")) {
            num = DateTimeUtil.getMomentsBetweenTwoDate(startTime, endTime);
        } else if (fileType.equals("status") || fileType.equals("nbq") || fileType.equals("qxz") || fileType.equals("rp") || fileType.equals("fj") || fileType.equals("cft")) {
            num = DateTimeUtil.getMin5BetweenTwoDate(startTime, endTime);
        }
        return num;
    }

    @Override
    public List<String> shouldFileName(String fileType, Long startTime, Long endTime, String stationCode,String key) {

        List<String> list = new ArrayList<>();
        if ("rdq".equals(fileType) || "dq".equals(fileType) || "nwp".equals(fileType)) {
            list = getDatesInRange(startTime + 24 * 60 * 60 * 1000L, endTime + 24 * 60 * 60 * 1000L);
        }
        if (!key.equals("true")){
            if ("rcdq".equals(fileType) || "cdq".equals(fileType) || "status".equals(fileType) || "nbq".equals(fileType) || "qxz".equals(fileType) || "rp".equals(fileType) || "fj".equals(fileType) || "cft".equals(fileType)) {
                if (new Date(endTime).after(new Date())) {
                    list = getMin(startTime, new Date().getTime(), fileType);
                } else {
                    list = getMin(startTime, endTime, fileType);
                }
            }
        }else {
            if ("cdq".equals(fileType) || "status".equals(fileType) || "nbq".equals(fileType) || "qxz".equals(fileType) || "rp".equals(fileType) || "fj".equals(fileType) || "cft".equals(fileType)) {
                if (new Date(endTime).after(new Date())) {
                    list = getMin(startTime, new Date().getTime(), fileType);
                } else {
                    list = getMin(startTime, endTime, fileType);
                }
            }
        }

        return list;
    }

    /**
     * 获取每一天 判断dq和nwp
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public List<String> getDatesInRange(Long startTime, Long endTime) {
        List<String> list = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(startTime);

        Date currentDate = calendar.getTime();
        while (currentDate.getTime() <= endTime) {
            String formattedDate = sdf.format(currentDate);
            list.add(formattedDate);
            // 移动到下一天
            calendar.add(Calendar.DAY_OF_MONTH, 1);
            currentDate = calendar.getTime();
        }
        return list;
    }

    /**
     * 5分钟节点/15分钟节点
     *
     * @param startTime
     * @param endTime
     * @param fileType
     * @return
     */
    public List<String> getMin(Long startTime, Long endTime, String fileType) {
        List<String> list = new ArrayList<>();
        if ("cdq".equals(fileType)|| "rcdq".equals(fileType)){
            startTime = startTime + 15 * 60 * 1000L;
        }
        LocalDateTime start = LocalDateTime.ofInstant(Instant.ofEpochMilli(startTime), ZoneId.systemDefault());
        LocalDateTime end = LocalDateTime.ofInstant(Instant.ofEpochMilli(endTime), ZoneId.systemDefault());
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd_HHmm");
        LocalDateTime currentTime = start;
        while (!currentTime.isAfter(end)) {
            String formattedTime = currentTime.format(formatter);
            if ("cdq".equals(fileType) || "rcdq".equals(fileType)|| "status".equals(fileType)) {
                currentTime = currentTime.plusMinutes(15);
            } else {
                currentTime = currentTime.plusMinutes(5);
            }
            list.add(formattedTime);
        }
        return list;
    }
}