package com.cool.store.vo.interview;

import cn.hutool.core.date.DateUtil;
import com.cool.store.constants.CommonConstants;
import com.cool.store.entity.LineCalendarsEventDO;
import com.cool.store.enums.AppointmentStatusEnum;
import com.cool.store.enums.InterviewTypeEnum;
import com.cool.store.utils.CoolDateUtils;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.tuple.Pair;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author zhangchenbiao
 * @FileName: AppointmentTimeVO
 * @Description:
 * @date 2024-03-15 16:07
 */
@Data
public class AppointmentTimeVO {

    @ApiModelProperty("时间")
    private String time;

    @ApiModelProperty("状态 0:不可预约 1:已被预约 2:可预约")
    private Integer status;

    public static List<AppointmentTimeVO> getAppointmentTime(LocalDate localDate, List<Pair<String, Boolean>> timeSlots, List<LineCalendarsEventDO> eventList) {
        List<AppointmentTimeVO> resultList = new ArrayList<>();
        if(CollectionUtils.isEmpty(eventList)){
            for (Pair<String, Boolean> time : ListUtils.emptyIfNull(timeSlots)) {
                resultList.add(new AppointmentTimeVO(time.getLeft(), time.getRight() ? AppointmentStatusEnum.CAN_APPOINTMENT.getCode() : AppointmentStatusEnum.NOT_APPOINTMENT.getCode()));
            }
            return resultList;
        }
        List<Pair<LocalDateTime, LocalDateTime>> eventDateTime = new ArrayList<>();
        for (LineCalendarsEventDO event : eventList) {
            Date startTime = event.getStartTime();
            Date endTime = event.getEndTime();
            LocalDateTime startDateTime = LocalDateTime.parse(DateUtil.format(startTime, CoolDateUtils.DATE_FORMAT_SEC), DateTimeFormatter.ofPattern(CoolDateUtils.DATE_FORMAT_SEC));
            LocalDateTime endDateTime = LocalDateTime.parse(DateUtil.format(endTime, CoolDateUtils.DATE_FORMAT_SEC), DateTimeFormatter.ofPattern(CoolDateUtils.DATE_FORMAT_SEC));
            eventDateTime.add(Pair.of(startDateTime, endDateTime));
        }
        for (Pair<String, Boolean> time : ListUtils.emptyIfNull(timeSlots)) {
            String timeStr = time.getLeft();
            boolean isAvailable = time.getRight();
            String[] timeArray = timeStr.split("-");
            String[] startTime = timeArray[0].split(CommonConstants.COLON);
            Integer startHour = Integer.parseInt(startTime[0]);
            Integer startMinute = Integer.parseInt(startTime[1]);
            LocalDateTime startDateTime = LocalDateTime.of(localDate, LocalTime.of(startHour, startMinute));
            String[] endTime = timeArray[1].split(CommonConstants.COLON);
            Integer endHour = Integer.parseInt(endTime[0]);
            Integer endMinute = Integer.parseInt(endTime[1]);
            LocalDateTime endDateTime = LocalDateTime.of(localDate, LocalTime.of(endHour, endMinute));
            boolean isOverlapping = false;
            if(isAvailable){
                for (Pair<LocalDateTime, LocalDateTime> event : eventDateTime) {
                    isOverlapping = isOverlapping(event.getLeft(), event.getRight(), startDateTime, endDateTime);
                    if(isOverlapping){
                        break;
                    }
                }
            }
            AppointmentStatusEnum appointmentStatus = isAvailable ? AppointmentStatusEnum.CAN_APPOINTMENT : AppointmentStatusEnum.NOT_APPOINTMENT;
            if (isOverlapping && appointmentStatus.equals(AppointmentStatusEnum.CAN_APPOINTMENT)){
                appointmentStatus = AppointmentStatusEnum.APPOINTMENT;
            }
            resultList.add(new AppointmentTimeVO(time.getLeft(), appointmentStatus.getCode()));
        }
        return resultList;
    }

    public AppointmentTimeVO(String time, Integer status) {
        this.time = time;
        this.status = status;
    }

    public static boolean isOverlapping(LocalDateTime start1, LocalDateTime end1, LocalDateTime start2, LocalDateTime end2) {
        if(start1.equals(start2) && end1.equals(end2)){
            return true;
        }
        // 如果第二个时间段的开始时间在第一个时间段内（包括两端点）
        if (start2.isAfter(start1) && start2.isBefore(end1)) {
            return true;
        }
        // 或者第二个时间段的结束时间在第一个时间段内（包括两端点）
        if (end2.isAfter(start1) && end2.isBefore(end1)) {
            return true;
        }
        // 或者第一个时间段的开始时间在第二个时间段内
        if (start1.isAfter(start2) && start1.isBefore(end2)) {
            return true;
        }
        // 或者第一个时间段的结束时间在第二个时间段内
        if (end1.isAfter(start2) && end1.isBefore(end2)) {
            return true;
        }
        // 如果以上条件都不满足，则说明两个时间段不重叠
        return false;
    }
}
