package com.ruoyi.zhushi.util;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.YearMonth;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CalendarGenerator {
    // 判断是否为闰年
    public static boolean isLeapYear(int year) {
        return java.time.Year.of(year).isLeap();
    }

    // 生成当年每月的日期和星期
    public static Map<Integer, List<DayInfo>> generateYearCalendar(int year) {
        Map<Integer, List<DayInfo>> calendar = new HashMap<>();

        // 遍历每个月
        for (int month = 1; month <= 12; month++) {
            YearMonth yearMonth = YearMonth.of(year, month);
            int daysInMonth = yearMonth.lengthOfMonth(); // 获取当月天数

            List<DayInfo> days = new ArrayList<>();
            // 遍历每天
            for (int day = 1; day <= daysInMonth; day++) {
                LocalDate date = LocalDate.of(year, month, day);
                DayOfWeek dayOfWeek = date.getDayOfWeek();

                days.add(new DayInfo(day, dayOfWeek));
            }

            calendar.put(month, days);
        }

        return calendar;
    }

    // 日期信息类
    public static class DayInfo {
        private int day;
        private DayOfWeek dayOfWeek;

        public DayInfo(int day, DayOfWeek dayOfWeek) {
            this.day = day;
            this.dayOfWeek = dayOfWeek;
        }

        // Getters and toString
        public int getDay() {
            return day;
        }

        public DayOfWeek getDayOfWeek() {
            return dayOfWeek;
        }

        @Override
        public String toString() {
            return day + "日 (" + dayOfWeek.getDisplayName(
                java.time.format.TextStyle.SHORT, java.util.Locale.CHINESE) + ")";
        }
    }
}
