package cn.estsh.i3plus.ext.mes.pcn.pojo.util;

/**
 * @PROJECT_NAME: i3plus-andon-panasonic
 * @DESCRIPTION:
 * @USER: xinwang.yi
 * @DATE: 2023-04-26 16:42
 */

import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;

public class SxDateRangeUtils {

    public final static String DAY_START_SS = " 00:00:00";
    public final static String DAY_END_SS = " 23:59:59";

    /**
     * 计算两个年月日格式的时间区间日期，并将这个日期用集合返回
     * @param startDate 开始日期，格式为"yyyy-MM-dd"
     * @param endDate 结束日期，格式为"yyyy-MM-dd"
     * @return 区间日期集合
     */
    public static List<String> getDateRange(String startDate, String endDate, String pattern) {
        LocalDate start = LocalDate.parse(startDate);
        LocalDate end = LocalDate.parse(endDate);
        List<String> dates = new ArrayList<>();
        long numOfDaysBetween = ChronoUnit.DAYS.between(start, end);
        for (int index = 0; index <= numOfDaysBetween; index++) {
            dates.add(start.plusDays(index).format(DateTimeFormatter.ofPattern(pattern)));
        }
        return dates;
    }

    /**
     * 计算两个年月日格式的时间区间日期，并将这个日期用集合返回
     * @param startDate 开始日期，格式为"yyyy-MM-dd"
     * @param endDate 结束日期，格式为"yyyy-MM-dd"
     * @return 区间月集合
     */
    public static List<String> getMonthRange(String startDate, String endDate, String pattern) {
        LocalDate start = LocalDate.parse(startDate);
        LocalDate end = LocalDate.parse(endDate);
        List<String> dates = new ArrayList<>();
        long numOfDaysBetween = ChronoUnit.MONTHS.between(start, end);
        for (int index = 0; index <= numOfDaysBetween; index++) {
            dates.add(start.plusMonths(index).format(DateTimeFormatter.ofPattern(pattern)));
        }
        return dates;
    }

    private static final int WEEK_LENGTH = 7;

    /**
     * 计算两个时间区间并将其纳入到集合中，并按每7个值算一周封装
     * @param startDateVal 开始日期
     * @param endDateVal 结束日期
     * @return 封装好的日期集合
     */
    public static Map<String, List<String>> calculateWeeks(String startDateVal, String endDateVal) {
        Map<String, List<String>> dateMap = new LinkedHashMap<>();
        int weekNumber = 1;
        List<String> weekDates = new ArrayList<>();
        LocalDate currentDate = LocalDate.parse(startDateVal, DateTimeFormatter.ofPattern(MesPcnExtConstWords.DATE_FORMAT));
        LocalDate endDate = LocalDate.parse(endDateVal, DateTimeFormatter.ofPattern(MesPcnExtConstWords.DATE_FORMAT));
        while (!currentDate.isAfter(endDate)) {
            weekDates.add(currentDate.toString());
            if (weekDates.size() == WEEK_LENGTH) {
                dateMap.put("第" + weekNumber + "周", weekDates);
                weekDates = new ArrayList<>();
                weekNumber++;
            }
            currentDate = currentDate.plusDays(1);
        }
        if (!weekDates.isEmpty()) {
            dateMap.put("第" + weekNumber + "周", weekDates);
        }
        return dateMap;
    }

    /**
     * 根据当前日期获取指定日期
     * @return
     */
    public static String getDateByNowTime(int num) {
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();

        // 根据当前日期获取指定日期
        LocalDate lastSevenDays = currentDate.minusDays(num);

        return lastSevenDays.toString();
    }

    public static String getLastDayTimeOfMonth(String yearMonthStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(sdf.parse(yearMonthStr));
        } catch (Exception e) {
            e.printStackTrace();
        }
        int lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        calendar.set(Calendar.DATE, lastDay);
        Date date = calendar.getTime();
        sdf.applyPattern("yyyy-MM-dd");
        return sdf.format(date) + DAY_END_SS;
    }
}
