package org.jeecg.modules.system.app.utils;

import com.alibaba.fastjson.JSONObject;
import org.jeecg.modules.system.app.vo.time.NextSevenDayVo;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.TextStyle;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author DM
 * @date 2025/7/9 16:21
 * @description
 */
public class TimeRangeFinder {
    // 定义时间段结构
     static class TimeRange {
        LocalTime start;
        LocalTime end;

        public TimeRange(LocalTime start, LocalTime end) {
            this.start = start;
            this.end = end;
        }

        @Override
        public String toString() {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
            return start.format(formatter) + "-" + end.format(formatter);
        }
    }

    // 解析字符串为时间段对象
    public static List<TimeRange> parseTimeRanges(String input) {
        if (input == null || input.trim().isEmpty()) {
            return Collections.emptyList();
        }

        return Arrays.stream(input.split(","))
                .map(String::trim)
                .filter(s -> s.contains("-"))
                .map(range -> {
                    String[] parts = range.split("-");
                    LocalTime start = LocalTime.parse(parts[0].trim(), DateTimeFormatter.ofPattern("H:mm"));
                    LocalTime end = LocalTime.parse(parts[1].trim(), DateTimeFormatter.ofPattern("H:mm"));
                    return new TimeRange(start, end);
                })
                .sorted(Comparator.comparing(tr -> tr.start))
                .collect(Collectors.toList());
    }

    // 合并时间段
    public static List<TimeRange> mergeTimeRanges(List<TimeRange> ranges) {
        if (ranges.isEmpty()) return Collections.emptyList();

        List<TimeRange> merged = new ArrayList<>();
        TimeRange current = ranges.get(0);

        for (int i = 1; i < ranges.size(); i++) {
            TimeRange next = ranges.get(i);
            if (!next.start.isAfter(current.end)) {
                // 有重叠或相邻，合并
                current = new TimeRange(current.start, current.end.isAfter(next.end) ? current.end : next.end);
            } else {
                // 无重叠，加入当前并切换到下一个
                merged.add(current);
                current = next;
            }
        }

        merged.add(current); // 添加最后一个时间段
        return merged;
    }

    // 格式化输出
    public static String formatMergedRanges(List<TimeRange> merged) {
        return merged.stream().map(TimeRange::toString).collect(Collectors.joining(","));
    }

    //获取当前时间及后面6天的
    public static List<NextSevenDayVo> getNextSevenDays() {
        List<NextSevenDayVo> dates = new ArrayList<>();
        LocalDate today = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM-dd");
        for (int i = 0; i < 7; i++) { // 包含今天共8天
            NextSevenDayVo nextSevenDayVo= new NextSevenDayVo();
            LocalDate localDate = today.plusDays(i);
            Locale locale = Locale.CHINA;
            String format = localDate.format(formatter);
//            Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
            String date = localDate.toString();
            DayOfWeek dayOfWeek = localDate.getDayOfWeek();
            int dayOfWeekNum = dayOfWeek.getValue();
            String display;
            switch (i) {
                case 0:
                    display = "今天(" + format + ")";
                    break;
                case 1:
                    display = "明天(" + format + ")";
                    break;
                case 2:
                    display = "后天(" + format + ")";
                    break;
                default:

                    String weekday = dayOfWeek.getDisplayName(TextStyle.SHORT, locale);
                    display = weekday + "(" + format + ")";
            }
            nextSevenDayVo.setWeekDayDate(date);
            nextSevenDayVo.setWeekDay(dayOfWeekNum);
            nextSevenDayVo.setWeekDayAndDate(display);
            dates.add(nextSevenDayVo);
        }
        return dates;
    }

    // 主方法示例
    public static void main(String[] args) {
//        String input = "6:00-7:00, 6:30-8:00, 18:00-20:00, 19:30-21:00";
//        List<TimeRange> parsed = parseTimeRanges(input);
//        List<TimeRange> merged = mergeTimeRanges(parsed);
//        String result = formatMergedRanges(merged);
        List<NextSevenDayVo> nextSevenDays = getNextSevenDays();
        System.out.println(JSONObject.toJSONString(nextSevenDays));
//        System.out.println(result); // 输出：6:00-8:00,18:00-21:00
    }

}