package com.bonus.system.att.utils;

import com.bonus.common.core.utils.DateUtils;
import com.bonus.system.att.entity.Holiday;

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

/**
 * 计算工作日
 *
 * @author zys
 */
public class WorkdayCalculator {

    // 星期对应表
    static Integer[] weekDays = {Calendar.MONDAY, Calendar.TUESDAY, Calendar.WEDNESDAY, Calendar.THURSDAY,
            Calendar.FRIDAY, Calendar.SATURDAY, Calendar.SUNDAY};

    public static void main(String[] args) {

        List<String> list= getWorkDay("1,1,1,1,1,0,0", 1, getHolidays(), "2025-01-01");
        System.out.print(1);
    }

    /**
     *
     * @param attDays 周打卡日期
     * @param type 是否有节假日
     * @param holidays 月法假
     * @param pushDate 推送日期
     * @return
     */
    public static List<String> getWorkDay(String attDays, int type, List<Holiday> holidays, String pushDate) {
        List<Integer> dueWorkDates = getDueWorkDates(attDays);
        // 定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        try {
            // 解析日期字符串为 LocalDate 对象
            LocalDate date = LocalDate.parse(pushDate, formatter);
            // 获取年份和月份
            int year = date.getYear();
            int month = date.getMonthValue();  // 获取月份（1-12）
            // 获取工作日集合（排除周末）
            List<Date> workDays = getWorkDays(year, month, dueWorkDates);
            // 获取工作日字符串集合
            List<String> dateString = getDateString(workDays);
            // 获取法定节假日集合
            if (type == 1) {
                holidays.forEach(c -> {
                    if (c.getType().equals("1")) {
                        dateString.remove(c.getDate());
                    } else if (c.getType().equals("2")) {
                        dateString.add(c.getDate());
                    }
                });
            }
            return dateString;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ArrayList<>();
    }

    private static List<Integer> getDueWorkDates(String s) {
        List<Integer> week = new ArrayList<>();
        List<String> list = new ArrayList<>(Arrays.asList(s.split(",")));
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).equals("1")) {
                week.add(weekDays[i]);
            }
        }
        return week;
    }

    private static List<Holiday> getHolidays() {
        List<Holiday> holidays = new ArrayList<>();
        holidays.add(new Holiday("2025-01-01", "", "1"));
        holidays.add(new Holiday("2025-01-04", "", "1"));
        holidays.add(new Holiday("2025-01-05", "", "1"));
        holidays.add(new Holiday("2025-01-11", "", "1"));
        holidays.add(new Holiday("2025-01-12", "", "1"));
        holidays.add(new Holiday("2025-01-18", "", "1"));
        holidays.add(new Holiday("2025-01-19", "", "1"));
        holidays.add(new Holiday("2025-01-25", "", "1"));
        holidays.add(new Holiday("2025-01-26", "", "2"));
        holidays.add(new Holiday("2025-01-28", "", "1"));
        holidays.add(new Holiday("2025-01-29", "", "1"));
        holidays.add(new Holiday("2025-01-30", "", "1"));
        holidays.add(new Holiday("2025-01-31", "", "1"));
        return holidays;
    }


    // 返回工作日集合，只排除周末
    public static List<Date> getWorkDays(int year, int month, List<Integer> dueWorkDates) {
        // 用于储存每月工作日
        List<Date> dates = new ArrayList<Date>();

        Calendar cal = Calendar.getInstance();
        //设置月份
        cal.set(Calendar.YEAR, year);
        // 设置月份
        cal.set(Calendar.MONTH, month - 1);
        // 设置为当月第一天
        cal.set(Calendar.DATE, 1);
        while (cal.get(Calendar.YEAR) == year && cal.get(Calendar.MONTH) < month) {
            // 判断当前天为本周的第几天
            int day = cal.get(Calendar.DAY_OF_WEEK);
            // 如果不为周六或者周天,将日期进行储存
            List<Boolean> list = new ArrayList<>();
            dueWorkDates.forEach(c -> {
                if (day == c) {
                    list.add(true);
                }
            });
            if (!list.isEmpty()) {
                if (list.stream().allMatch(Boolean::booleanValue)) {
                    dates.add((Date) cal.getTime().clone());
                }
            }
//            if(!(day == Calendar.SUNDAY || day == Calendar.SATURDAY)){
//                dates.add((Date)cal.getTime().clone());
//            }
            // 将当前日期增加一天
            cal.add(Calendar.DATE, 1);
        }
        // 返回当前月工作日集合
        return dates;
    }

    /**
     * @param dateList
     * @return 返回日期字符串集合
     */
    public static List<String> getDateString(List<Date> dateList) {
        // 储存日期字符串
        List<String> dateString = new ArrayList<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        for (Date date : dateList) {
            String date2 = simpleDateFormat.format(date);
            dateString.add(date2);
        }
        return dateString;
    }

}
