package com.jeequan.jeepay.core.utils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.core.exception.BizException;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * TODO
 * 节假日工具类型
 * @author crystal
 * @date 2024/3/7 17:04
 */
@Slf4j
public class HolidayUtil {

    public static final int SUCCESS = 0;

    /**
     * 接口请求地址
     */
    public static final String url = "https://timor.tech/api/holiday/info/";


    /**
     * 判断日期是否是节假日
     * "type": enum(0, 1, 2, 3), / 节假日类型，分别表示 工作日、周末、节日、调休。
     * @param date
     * @return false 表示工作日 true 表示节假日 节日或者是周末
     * {"code":0,"type":{"type":0,"name":"周一","week":1},"holiday":null}
     * {
     *     "code": 0,
     *     "type": {
     *         "type": 2,
     *         "name": "劳动节",
     *         "week": 3
     *     },
     *     "holiday": {
     *         "holiday": true,
     *         "name": "劳动节",
     *         "wage": 3,
     *         "date": "2024-05-01",
     *         "rest": 24
     *     }
     * }
     */
    public static boolean isWorkDay(Date date) {
        String queryDate = DateUtil.formatDate(date);
        String reqUrl = url + queryDate;
        HttpResponse response = HttpRequest.get(reqUrl).execute();
        if(!response.isOk()){
            throw new BizException("节假日接口请求异常");
        }
        JSONObject body = JSON.parseObject(response.body());
        log.info("节假日接口返回参数：{}", body);

        JSONObject typeInfo = body.getJSONObject("type");
        String type = typeInfo.getString("type");

        return "0".equals(type) || "3".equals(type);
    }

    public static List<String> getSettleDate(Date date){
        List<String> dateList = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            String queryDate = DateUtil.formatDate(DateUtil.offsetDay(date,-i));
            String reqUrl = url + queryDate;
            HttpResponse response = HttpRequest.get(reqUrl).execute();
            if(!response.isOk()){
                throw new BizException("节假日接口请求异常");
            }
            JSONObject body = JSON.parseObject(response.body());
            log.info("接口返回参数：{}",body);
            if(body.getIntValue("code") != SUCCESS){
                throw new BizException("服务器异常");
            }
            JSONObject type = body.getJSONObject("type");
            int typeValue = type.getIntValue("type");
            //如果当前为节假日 则直接不返回数据
            if((typeValue == 1 || typeValue == 2) && i == 0){
                break;
            }
            if((typeValue == 1 || typeValue == 2) && i > 0){
                continue;
            }
            if(dateList.size() < 2){
                if(dateList.isEmpty()){
                    queryDate = DateUtil.formatDate(DateUtil.offsetDay(DateUtil.parseDate(queryDate),-1));
                }
                dateList.add(queryDate);
            }else{
                break;
            }
        }
        Collections.sort(dateList);
        return dateList;
    }
}
