package com.cool.store.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.cool.store.constants.CommonConstants;
import com.cool.store.dao.HyPartnerInterviewPlanDAO;
import com.cool.store.dao.HyPartnerLineInfoDAO;
import com.cool.store.dto.calendar.UserCalendarsEventDTO;
import com.cool.store.dto.partner.AdvanceLineDTO;
import com.cool.store.dto.partner.SpecialDateRangeInterviewCountDTO;
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.service.DeskService;
import com.cool.store.service.HyPartnerInterviewPlanService;
import com.cool.store.service.HyPartnerLineInfoService;
import com.cool.store.utils.CoolDateUtils;
import com.cool.store.vo.InterviewDetailInfoVO;
import com.cool.store.vo.InterviewPlanVO;
import com.cool.store.vo.InterviewScheduleInfoVO;
import com.cool.store.vo.StageCountVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.utils.DateUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author suzhuhong
 * @Date 2023/6/8 14:45
 * @Version 1.0
 */
@Service
public class DeskServiceImpl implements DeskService {

    @Resource
    HyPartnerInterviewPlanDAO hyPartnerInterviewPlanDAO;
    @Resource
    HyPartnerLineInfoDAO hyPartnerLineInfoDAO;
    @Resource
    HyPartnerInterviewPlanService hyPartnerInterviewPlanService;
    @Resource
    HyPartnerLineInfoService hyPartnerLineInfoService;
    @Resource
    private ISVHttpRequest isvHttpRequest;

    @Override
    public HashMap<String, InterviewPlanVO>  getInterviewPlan(String userId) {

        String currentDate = DateUtil.format(new Date(), CoolDateUtils.DATE_FORMAT_DAY);
        String endDate = DateUtil.format(CoolDateUtils.getDateFormatDay(new Date(), 6), CoolDateUtils.DATE_FORMAT_DAY);
        List<AdvanceLineDTO>  currentDateInterviewList = hyPartnerInterviewPlanDAO.getCurrentDateInterviewCount(userId, currentDate,endDate);
        Map<String, Integer> dateIntegerMap = currentDateInterviewList.stream().collect(Collectors.toMap(AdvanceLineDTO::getDate, AdvanceLineDTO::getCount));
        //临期数量
        List<AdvanceLineDTO> adventLineList = hyPartnerLineInfoDAO.getAdventLineCount(userId, currentDate,endDate);
        Map<String, Integer> adventLinerMap = adventLineList.stream().collect(Collectors.toMap(AdvanceLineDTO::getDate, AdvanceLineDTO::getCount));

        HashMap<String, InterviewPlanVO> resultMap = new HashMap<>();
        String tempDate = currentDate;
        int i = 0;
        while (true){
            tempDate = DateUtil.format(CoolDateUtils.getDateFormatDay(new Date(), i++), CoolDateUtils.DATE_FORMAT_DAY);
            Integer currentDateInterviewCount = dateIntegerMap.getOrDefault(tempDate, 0);
            Integer adventLineCount = adventLinerMap.getOrDefault(tempDate,0);
            InterviewPlanVO interviewPlanVO = new InterviewPlanVO();
            interviewPlanVO.setHasInterview(currentDateInterviewCount>0);
            interviewPlanVO.setHasAdventLine(adventLineCount>0);
            resultMap.put(tempDate,interviewPlanVO);
            if (tempDate.equals(endDate)){
                break;
            }
        }
        return resultMap;
    }


    @Override
    public InterviewScheduleInfoVO interviewSchedule(String userId,String selectedData) {
        if (StringUtils.isEmpty(userId)||selectedData==null){
            throw  new ServiceException(ErrorCodeEnum.PARAMS_REQUIRED);
        }
        Date dateTime = CoolDateUtils.parseDate(selectedData);

        InterviewScheduleInfoVO interviewScheduleInfoVO = new InterviewScheduleInfoVO();
        //查询面试数量

        String currentDate = DateUtil.format(dateTime,  CoolDateUtils.DATE_FORMAT_DAY);
        String startTime = DateUtil.format(CoolDateUtils.getDateFormatDayMinTime(dateTime,1), CoolDateUtils.DATE_FORMAT_SEC);
        String endTime = DateUtil.format(CoolDateUtils.getDateFormatDay(dateTime,6), CoolDateUtils.DATE_FORMAT_SEC);
        SpecialDateRangeInterviewCountDTO interviewCount = hyPartnerInterviewPlanDAO.getInterviewCount(userId, currentDate, startTime, endTime);

        interviewScheduleInfoVO.setCurrentDayInterviewCount(interviewCount.getCurrentDayInterviewCount());
        interviewScheduleInfoVO.setLastSevenDayInterviewCount(interviewCount.getCurrentDayInterviewCount()+interviewCount.getLastSevenDayInterviewCount());

        //查询面试列表
        //当天时间 与入参无关
        List<InterviewDetailInfoVO> interviewPlanList = hyPartnerInterviewPlanService.getInterviewPlanList(userId,dateTime);
        interviewScheduleInfoVO.setInterviewDetailInfoVOS(interviewPlanList);

        return interviewScheduleInfoVO;
    }

    @Override
    public StageCountVO getStageCountByType(String userId, String type) {
        if (CommonConstants.PENDING.equals(type)){
            return hyPartnerLineInfoService.selectStagePendingCount(userId);
        }
        if (CommonConstants.FOLLOW.equals(type)){
            return hyPartnerLineInfoService.selectStageFollowCount(userId);
        }
        return null;
    }

    @Override
    public List<UserCalendarsEventDTO> getUserCalendarsEvents(String userId, long startTime, long endTime) throws ApiException {
        List<UserCalendarsEventDTO> userCalendarsEvents = isvHttpRequest.getUserCalendarsEvents(userId, startTime, endTime);
        Collections.sort(userCalendarsEvents);
        return userCalendarsEvents;
    }

}
