package com.cool.store.service.impl;

import cn.hutool.core.date.DateUtil;
import com.cool.store.utils.StringUtil;
import com.cool.store.vo.interview.CalendarInfo;
import com.cool.store.vo.interview.FreeBusyInfo;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class TimeSlotGenerator {
    public static void main(String[] args) {
        String startDate = "2023-02-27";
        String endDate = "2023-03-01";

        List<CalendarInfo> daySlots = generateDaySlots(startDate, endDate);
        for (CalendarInfo daySlot : daySlots) {
            System.out.println(daySlot);
        }
    }

    public static List<CalendarInfo> generateDaySlots(String startDate, String endDate) {
        List<CalendarInfo> daySlots = new ArrayList<>();

        Date start = DateUtil.parse(startDate);
        Date end = DateUtil.parse(endDate);

        Calendar startCal = DateUtil.calendar(start);
        Calendar endCal = DateUtil.calendar(end);

        while (!startCal.after(endCal)) {
            int year = startCal.get(Calendar.YEAR);
            int month = startCal.get(Calendar.MONTH) + 1;
            int day = startCal.get(Calendar.DAY_OF_MONTH);
            int dayOfWeek = startCal.get(Calendar.DAY_OF_WEEK);

            List<FreeBusyInfo> hourSlots = generateHourSlots();
            CalendarInfo daySlot = new CalendarInfo(transDayOfWeek(dayOfWeek),
                    String.valueOf(year),
                    StringUtil.addZeroForNum(String.valueOf(month),2),
                    StringUtil.addZeroForNum(String.valueOf(day),2),
                    DateUtil.format(startCal.getTime(), "yyyy-MM-dd"),
                    hourSlots);
            daySlots.add(daySlot);

            startCal.add(Calendar.DATE, 1);
        }

        return daySlots;
    }

    private static List<FreeBusyInfo> generateHourSlots() {
        List<FreeBusyInfo> hourSlots = new ArrayList<>();

        for (int hour = 10; hour < 17; hour++) {
            String startTime = String.format("%02d:00", hour);
            String endTime = String.format("%02d:00", hour + 1);
            //默认空闲
            FreeBusyInfo hourSlot = new FreeBusyInfo(startTime, endTime, true);
            hourSlots.add(hourSlot);
        }

        return hourSlots;
    }

    public static int transDayOfWeek(int dayOfWeek) {
        switch (dayOfWeek) {
            case 1:
                return 7;
            case 2:
                return 1;
            case 3:
                return 2;
            case 4:
                return 3;
            case 5:
                return 4;
            case 6:
                return 5;
            case 7:
                return 6;
            default:
                return 0;
        }
    }
}
