package com.persagy.calendar.controller;

import com.persagy.calendar.pojo.dto.Holiday;
import com.persagy.calendar.pojo.vo.HolidayQueryVO;
import com.persagy.calendar.pojo.vo.HolidayVO;
import com.persagy.calendar.service.HolidayService;
import com.persagy.calendar.utils.TimeUtil;
import com.persagy.common.enums.ResponseCode;
import com.persagy.common.utils.ResponseResult;
import com.persagy.common.utils.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@RestController
@RequestMapping(value = "/holiday", method = RequestMethod.POST)
@Slf4j
public class HolidayController {

    @Autowired
    private HolidayService holidayService;

    @RequestMapping(value = "query")
    public ResponseResult query(@RequestBody @Valid HolidayQueryVO holidayQueryVO) {
        try {
            //1.校验
            List<String> betweenDays = TimeUtil.getBetweenDays(holidayQueryVO.getStartTime(), holidayQueryVO.getEndTime());
            if (betweenDays.size() > 365) {
                return ResponseUtil.errorResult(ResponseCode.A0400.getCode(),"超过可查询的最大时间范围");
            }

            //2.查询
            List<Holiday> holidayList = holidayService.queryHoliday(holidayQueryVO);

            //3.转换
            List<HolidayVO> holidayVOList = convertVoList(holidayList);

            //4.升序排序
            sortByDateAsc(holidayVOList);

            //5.返回
            return ResponseUtil.successResult(holidayVOList, Long.valueOf(holidayList.size()));
        }catch (Exception e) {
            log.error("查询节假日异常", e);
            return ResponseUtil.errorResult();
        }
    }

    private List<HolidayVO> convertVoList(List<Holiday> list) {
        List<HolidayVO> voList = new ArrayList<>();
        for (Holiday holiday : list) {
            HolidayVO holidayVO = convertVo(holiday);
            voList.add(holidayVO);
        }
        return voList;
    }

    private HolidayVO convertVo(Holiday po) {
        HolidayVO workCalendarVO = new HolidayVO();
        workCalendarVO.setWorkDate(TimeUtil.getStringDate(po.getWorkDate()));
        workCalendarVO.setWorkdayCode(po.getWorkdayCode());
        workCalendarVO.setWorkdayName(po.getWorkdayName());
        workCalendarVO.setWeekCode(po.getWeekCode());
        workCalendarVO.setWeekName(po.getWeekName());
        workCalendarVO.setWeekendCode(po.getWeekendCode());
        workCalendarVO.setWeekendName(po.getWeekendName());
        workCalendarVO.setHolidayCode(po.getHolidayCode());
        workCalendarVO.setHolidayName(po.getHolidayName());
        return workCalendarVO;
    }

    private void sortByDateAsc(List<HolidayVO> list) {
        Collections.sort(list, new Comparator<HolidayVO>() {
            @Override
            public int compare(HolidayVO o1, HolidayVO o2) {
                if (TimeUtil.dateToLong(o1.getWorkDate()) >
                        TimeUtil.dateToLong(o2.getWorkDate())) {
                    return 1;
                }
                if (TimeUtil.dateToLong(o1.getWorkDate()) ==
                        TimeUtil.dateToLong(o2.getWorkDate())) {
                    return 0;
                }
                return -1;
            }
        });
    }
}
