package com.cool.store.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.cool.store.dto.calendar.UserFreeBusyInfoDTO;
import com.cool.store.entity.HyPartnerInterviewPlanDO;
import com.cool.store.entity.HyPartnerLineInfoDO;
import com.cool.store.enums.ErrorCodeEnum;
import com.cool.store.exception.ApiException;
import com.cool.store.exception.ServiceException;
import com.cool.store.http.ISVHttpRequest;
import com.cool.store.mapper.HyPartnerInterviewPlanMapper;
import com.cool.store.mapper.HyPartnerLineInfoMapper;
import com.cool.store.request.GetFreeBusyListReq;
import com.cool.store.service.FeiShuService;
import com.cool.store.service.InterviewService;
import com.cool.store.vo.interview.CalendarInfo;
import com.cool.store.vo.interview.FreeBusyInfo;
import com.cool.store.vo.interview.GetFreeBusyListVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: young.yu
 * @Date: 2023-06-16  13:13
 * @Description:
 */
@Service
public class FeiShuServiceImpl implements FeiShuService {

    @Autowired
    private ISVHttpRequest isvHttpRequest;

    @Autowired
    private HyPartnerInterviewPlanMapper hyPartnerInterviewPlanMapper;

    @Autowired
    private InterviewService interviewService;

    @Override
    public GetFreeBusyListVO getFreeBusyList(GetFreeBusyListReq request) throws ApiException {
        GetFreeBusyListVO freeBusyListVO = new GetFreeBusyListVO();
        String startDate = request.getStartDate();//开始日期
        String endDate = request.getEndDate();//结束日期
        String partnerId = request.getPartnerId();//加盟商用户ID
        String interviewerId = request.getInterviewerId();//面试官ID
        String lineId = request.getLineId();//线索id
        //如果加盟商用户ID为空且面试官ID为空，返回错误信息
        if (!(StringUtils.isNotEmpty(partnerId) && StringUtils.isNotEmpty(lineId)) && StringUtils.isEmpty(interviewerId)) {
            throw new ServiceException(ErrorCodeEnum.INTERVIEW_AND_PARTNER_ID_IS_NULL);
        }
        //如果加盟商用户ID不为空且面试官ID为空
        if (StringUtils.isNotEmpty(partnerId) && StringUtils.isNotEmpty(lineId)) {
            interviewerId = interviewService.getInterviewerByPartner(partnerId, lineId);
        }
        long startTime = 0L;
        long endTime = 0L;
        DateTime startDateTime = null;
        DateTime endDateTime = null;

        if (!StringUtils.isEmpty(startDate)&&!StringUtils.isEmpty(endDate)) {
            startDateTime = DateUtil.beginOfDay(DateUtil.parse(startDate));
            endDateTime = DateUtil.endOfDay(DateUtil.parse(endDate));
        }
        if (startDateTime.isAfter(endDateTime)) {
            throw new ServiceException(ErrorCodeEnum.DATE_PARAMS_IS_ERROR);
        }

        startTime = startDateTime.getTime();
        endTime = endDateTime.getTime();

        List<UserFreeBusyInfoDTO> UserFreeBusyInfoList = isvHttpRequest.getFreeBusyList(interviewerId, startTime, endTime);

        //把startTime和endTime转换成yyyy-MM-dd格式
        String startTimeStr = DateUtil.format(startDateTime, "yyyy-MM-dd");
        String endTimeStr = DateUtil.format(endDateTime, "yyyy-MM-dd");
        List<CalendarInfo> daySlots = TimeSlotGenerator.generateDaySlots(startTimeStr, endTimeStr);
        freeBusyListVO.setCalendarList(daySlots);

        //如果查询结果为空，则直接返回全部时间段都是空闲的
        if (!CollectionUtils.isEmpty(UserFreeBusyInfoList)) {
            for (CalendarInfo daySlot : daySlots) {
                List<FreeBusyInfo> freeBusyList = daySlot.getFreeBusyList();
                for (FreeBusyInfo freeBusyInfo : freeBusyList) {
                    //把日期和时间拼接成yyyy-MM-dd HH:mm格式
                    String startStr = daySlot.getDateStr() + " " + freeBusyInfo.getStartTime();
                    String endStr = daySlot.getDateStr() + " " + freeBusyInfo.getEndTime();
                    //把yyyy-MM-dd HH:mm格式转换成long类型
                    long startTimeLong = DateUtil.parse(startStr).getTime();
                    long endTimeLong = DateUtil.parse(endStr).getTime();

                    for (UserFreeBusyInfoDTO userFreeBusyInfoDTO : UserFreeBusyInfoList) {
                        //比较两个时间段是否有重叠
                        if(!(endTimeLong <= userFreeBusyInfoDTO.getStartTime() || startTimeLong >= userFreeBusyInfoDTO.getEndTime())){
                            freeBusyInfo.setFree(false);
                            break;
                        }
                    }

                }
            }
        }


        return freeBusyListVO;
    }
}
