package com.zbkj.common.utils;

import com.zbkj.common.constants.Constants;
import com.zbkj.common.response.DeliveryTimeResponse;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;

public class DeliveryUtil {

    /**
     * 判断时间时上午还是下午
     * @param dateTime
     * @return
     */
    public static Integer isAmOrPm(LocalDateTime dateTime) {
        // 定义上午和下午的分界时间（中午12点）
        LocalTime noon = LocalTime.of(12, 0);
        // 判断是上午还是下午
        // 上午
        if (dateTime.toLocalTime().isBefore(noon)) {
            return 0;
        } else { // 下午
            return 1;
        }
    }

    /**
     * 判断时间是否是今天
     * @param dateTime
     * @return
     */
    public static Integer isNowDay(LocalDateTime dateTime) {
        // 获取当前日期和时间
        LocalDateTime now = LocalDateTime.now();
        // 判断是否是今天
        // 不是今天
        if (!dateTime.toLocalDate().equals(now.toLocalDate())) {
            return 0;
        }
        // 是今天
        return 1;
    }

    /**
     * 获取多天内指定上午和下午时间区间的每小时时间段
     *
     * @param startDate      起始日期
     * @param numberOfDays   天数
     * @param morningStart   上午开始时间
     * @param morningEnd     上午结束时间
     * @param afternoonStart 下午开始时间
     * @param afternoonEnd   下午结束时间
     * @return 每小时时间段的列表
     */
    public static List<DeliveryTimeResponse> getHourlyTimeIntervals(
            LocalDate startDate, int numberOfDays, int splitMin,
            LocalTime morningStart, LocalTime morningEnd,
            LocalTime afternoonStart, LocalTime afternoonEnd) {
        List<DeliveryTimeResponse> responseList = new ArrayList<>();
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter hmFor = DateTimeFormatter.ofPattern(Constants.HH_MM);
        DateTimeFormatter mdFor = DateTimeFormatter.ofPattern(Constants.HH_MONTH_MM_DAY);
        DateTimeFormatter ymdFor = DateTimeFormatter.ofPattern(Constants.DATE_FORMAT_DATE);
        // 遍历每一天
        for (int i = 0; i < numberOfDays; i++) {
            LocalDate currentDate = startDate.plusDays(i);
            Integer isNowDay = isNowDay(LocalDateTime.of(currentDate, LocalTime.now()));
            List<String> intervals = new ArrayList<>();
            // 处理上午时间段
            if (isNowDay == 0) {
                intervals.addAll(getHourlyIntervalsForPeriod(currentDate, morningStart, morningEnd, hmFor, splitMin));
            }
            // 处理下午时间段
            if (isNowDay == 0 || (isNowDay == 1 && isAmOrPm(now) == 0)) {
                intervals.addAll(getHourlyIntervalsForPeriod(currentDate, afternoonStart, afternoonEnd, hmFor, splitMin));
            }
            if (!intervals.isEmpty()) {
                responseList.add(new DeliveryTimeResponse(getTargetDate(currentDate, mdFor), currentDate.format(ymdFor), intervals));
            }
        }
        return responseList;
    }

    private static String getTargetDate(LocalDate targetDate, DateTimeFormatter md) {
        LocalDate today = LocalDate.now();
        long daysUntil = ChronoUnit.DAYS.between(today, targetDate);
        // 判断日期
        if (targetDate.isEqual(today)) {
            return "今天 " + targetDate.format(md);
        } else if (targetDate.isAfter(today)) {
            switch ((int) daysUntil) {
                case 1:
                    return "明天 " + targetDate.format(md);
                case 2:
                    return "后天 " + targetDate.format(md);
                default:
                    return "其他 " + targetDate.format(md);
            }
        }
        return "";
    }

    /**
     * 获取某一天的指定时间区间的每小时时间段
     *
     * @param date      当前日期
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @param formatter 日期时间格式化器
     * @return 每小时时间段的列表
     */
    private static List<String> getHourlyIntervalsForPeriod(LocalDate date, LocalTime startTime, LocalTime endTime, DateTimeFormatter formatter, Integer splitMin) {
        List<String> intervals = new ArrayList<>();
        LocalTime currentTime = startTime;
        // 遍历每小时
        while (currentTime.isBefore(endTime) || currentTime.equals(endTime)) {
            LocalDateTime startDateTime = LocalDateTime.of(date, currentTime);
            LocalDateTime endDateTime = startDateTime.plusMinutes(splitMin);
            // 如果结束时间超过当前时间段的结束时间，则调整为结束时间
            if (endDateTime.toLocalTime().isAfter(endTime)) {
                endDateTime = LocalDateTime.of(date, endTime);
            }
            String start = startDateTime.format(formatter);
            String end = endDateTime.format(formatter);
            intervals.add(start + "-" + end);

            if (end.equals(endTime.toString())) {
                break;
            }
            // 增加分钟数
            currentTime = currentTime.plusMinutes(splitMin);
        }
        return intervals;
    }

}
