package com.hotent.base.util;

import java.time.LocalDate;
import java.time.Month;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;

/**
 * 统计报表工具类
 * @author 梁宏锦
 * @date 2024/08/13
 **/
public class ChartUtil {

    /**
     * 获取指定月份 开始日期和结束日期
     */
    public static LocalDate[] getDateRangeByMonth(int year, int month) {
        LocalDate startDate = LocalDate.of(year, month, 1);
        LocalDate endDate = startDate.with(TemporalAdjusters.lastDayOfMonth());

        LocalDate now = LocalDate.now();
        if (month == now.getMonthValue()) {
            return new LocalDate[]{startDate, now};
        } else {
            return new LocalDate[]{startDate, endDate};
        }
    }
    /**
     * 获取指定月份 每一天
     */
    public static List<LocalDate> getDaysInMonth(int year, int month) {
        List<LocalDate> days = new ArrayList<>();
        YearMonth yearMonth = YearMonth.of(year, month);
        LocalDate firstDayOfMonth = yearMonth.atDay(1);
        for (int day = 1; day <= firstDayOfMonth.lengthOfMonth(); day++) {
            days.add(firstDayOfMonth.withDayOfMonth(day));
        }
        return days;
    }

    /**
     * 获取指定季度 开始日期和结束日期
     */
    public static LocalDate[] getDateRangeByQuarter(int year, int quarter) {
        LocalDate now = LocalDate.now();
        int currentYear = now.getYear();
        int currentQuarter = (now.getMonthValue() - 1) / 3 + 1; // 获取当前日期和当前季度
        Month firstMonth = Month.of(quarter * 3 - 2); // 季度转月份，1季度=1月，2季度=4月，以此类推
        Month lastMonth = Month.of(quarter * 3); // 注意：这里已经是季度最后一个月份，不需要+1

        LocalDate startDate = LocalDate.of(year, firstMonth, 1);
        LocalDate endDate;
        if ( year == currentYear && quarter == currentQuarter) {
            endDate = now.withMonth(lastMonth.getValue()).with(TemporalAdjusters.lastDayOfMonth());
        } else {
            endDate = LocalDate.of(year, lastMonth, lastMonth.length(isLeapYear(year)));
        }
        return new LocalDate[]{startDate, endDate};
    }
    // 辅助方法：判断是否为闰年
    private static boolean isLeapYear(int year) {
        return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
    }
    /**
     * 获取指定季度 每一月
     */
    public static List<String> getDaysInQuarter(int year, int quarter) {
        List<String> months = new ArrayList<>();
        int startMonth = (quarter - 1) * 3 + 1;
        int endMonth = quarter * 3;
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
        for (int month = startMonth; month <= endMonth; month++) {
            YearMonth yearMonth = YearMonth.of(year, month);
            months.add(yearMonth.format(formatter));
        }
        return months;
    }

    /**
     * 获取指定年度 开始日期和结束日期
     */
    public static LocalDate[] getDateRangeByYear(int year) {
        LocalDate now = LocalDate.now();
        int currentYear = now.getYear();

        LocalDate startDate = LocalDate.of(year, 1, 1);
        LocalDate endDate;
        if (year == currentYear) {
            endDate = now;
        } else {
            endDate = LocalDate.of(year, 12, 31);
        }
        return new LocalDate[]{startDate, endDate};
    }
    /**
     * 获取指定年度 往后每一年
     */
    public static List<String> getYearRange(int year) {
        List<String> months = new ArrayList<>();

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
        for (Month monthEnum : Month.values()) {
            YearMonth yearMonth = YearMonth.of(year, monthEnum);
            String formattedMonth = yearMonth.format(formatter);
            months.add(formattedMonth);
        }
        return months;
    }

    public static void main(String[] args) {
        LocalDate now = LocalDate.now();
        int currentYear = now.getYear();
        int month = 8;
        //LocalDate[] dateRange = getDateRangeByMonth(currentYear, month);
        LocalDate[] dateRange = ChartUtil.getDateRangeByQuarter(currentYear, 3);
        //LocalDate[] dateRange = getDateRangeByYear(currentYear);
        System.out.println("Start Date: " + dateRange[0] + ", End Date: " + dateRange[1]);
    }
}
