package com.happy.Until;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.happy.Model.Holiday;
import com.happy.service.HouseNumberStatusService;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.text.ParseException;
import java.util.*;

/**
 * @Author: binguo
 * @Date: 2023/9/20 星期三 9:49
 * @Description: com.happy.Until
 * @Version: 1.0
 */
public class HolidayUtil {

    /**
     * @param date 日期参数 格式‘yyyy’，不传参则默认当前日期
     * @return
     */
    public static List<Holiday> getYearHoliday(String date) {
        List<Holiday> days = new ArrayList<>();
        //获取免费api地址
        String httpUrl = "https://timor.tech/api/holiday/year/" + date;
        BufferedReader reader = null;
        String result = null;
        StringBuffer sbf = new StringBuffer();

        try {
//            URL url = new URL(httpUrl);
//            URLConnection connection = url.openConnection();
//            //connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
//            connection.setRequestProperty("User-Agent", "Mozilla/4.76");
//            //使用Get方式请求数据
//            //connection.setRequestMethod("GET");
//            //connection.connect();
//            InputStream is = connection.getInputStream();
//            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
//
//            String strRead = null;
//            while ((strRead = reader.readLine()) != null) {
//                sbf.append(strRead);
//                sbf.append("\r\n");
//            }
//            reader.close();

            String sss = "{\"code\":0,\"holiday\":{\"01-01\":{\"holiday\":true,\"name\":\"元旦\",\"wage\":3,\"date\":\"2023-01-01\",\"rest\":1},\"01-02\":{\"holiday\":true,\"name\":\"元旦\",\"wage\":2,\"date\":\"2023-01-02\",\"rest\":1},\"01-21\":{\"holiday\":true,\"name\":\"除夕\",\"wage\":3,\"date\":\"2023-01-21\",\"rest\":2},\"01-22\":{\"holiday\":true,\"name\":\"初一\",\"wage\":3,\"date\":\"2023-01-22\",\"rest\":1},\"01-23\":{\"holiday\":true,\"name\":\"初二\",\"wage\":3,\"date\":\"2023-01-23\",\"rest\":1},\"01-24\":{\"holiday\":true,\"name\":\"初三\",\"wage\":3,\"date\":\"2023-01-24\",\"rest\":1},\"01-25\":{\"holiday\":true,\"name\":\"初四\",\"wage\":2,\"date\":\"2023-01-25\",\"rest\":1},\"01-26\":{\"holiday\":true,\"name\":\"初五\",\"wage\":2,\"date\":\"2023-01-26\",\"rest\":1},\"01-27\":{\"holiday\":true,\"name\":\"初六\",\"wage\":2,\"date\":\"2023-01-27\",\"rest\":1},\"01-28\":{\"holiday\":false,\"name\":\"春节后补班\",\"wage\":1,\"after\":true,\"target\":\"春节\",\"date\":\"2023-01-28\",\"rest\":1},\"01-29\":{\"holiday\":false,\"name\":\"春节后补班\",\"wage\":1,\"after\":true,\"target\":\"春节\",\"date\":\"2023-01-29\",\"rest\":1},\"04-05\":{\"holiday\":true,\"name\":\"清明节\",\"wage\":3,\"date\":\"2023-04-05\",\"rest\":67},\"04-23\":{\"holiday\":false,\"name\":\"劳动节前补班\",\"wage\":1,\"target\":\"劳动节\",\"after\":false,\"date\":\"2023-04-23\",\"rest\":1},\"04-29\":{\"holiday\":true,\"name\":\"劳动节\",\"wage\":2,\"date\":\"2023-04-29\",\"rest\":7},\"04-30\":{\"holiday\":true,\"name\":\"劳动节\",\"wage\":2,\"date\":\"2023-04-30\",\"rest\":1},\"05-01\":{\"holiday\":true,\"name\":\"劳动节\",\"wage\":3,\"date\":\"2023-05-01\",\"rest\":1},\"05-02\":{\"holiday\":true,\"name\":\"劳动节\",\"wage\":3,\"date\":\"2023-05-02\",\"rest\":1},\"05-03\":{\"holiday\":true,\"name\":\"劳动节\",\"wage\":3,\"date\":\"2023-05-03\",\"rest\":1},\"05-06\":{\"holiday\":false,\"name\":\"劳动节后补班\",\"after\":true,\"wage\":1,\"target\":\"劳动节\",\"date\":\"2023-05-06\",\"rest\":2},\"06-22\":{\"holiday\":true,\"name\":\"端午节\",\"wage\":3,\"date\":\"2023-06-22\",\"rest\":2},\"06-23\":{\"holiday\":true,\"name\":\"端午节\",\"wage\":3,\"date\":\"2023-06-23\",\"rest\":1},\"06-24\":{\"holiday\":true,\"name\":\"端午节\",\"wage\":2,\"date\":\"2023-06-24\",\"rest\":1},\"06-25\":{\"holiday\":false,\"name\":\"端午节后补班\",\"wage\":1,\"target\":\"端午节\",\"after\":true,\"date\":\"2023-06-25\",\"rest\":1},\"09-29\":{\"holiday\":true,\"name\":\"中秋节\",\"wage\":3,\"date\":\"2023-09-29\",\"rest\":39},\"09-30\":{\"holiday\":true,\"name\":\"中秋节\",\"wage\":3,\"date\":\"2023-09-30\",\"rest\":1},\"10-01\":{\"holiday\":true,\"name\":\"国庆节\",\"wage\":3,\"date\":\"2023-10-01\",\"rest\":1},\"10-02\":{\"holiday\":true,\"name\":\"国庆节\",\"wage\":3,\"date\":\"2023-10-02\",\"rest\":1},\"10-03\":{\"holiday\":true,\"name\":\"国庆节\",\"wage\":2,\"date\":\"2023-10-03\",\"rest\":1},\"10-04\":{\"holiday\":true,\"name\":\"国庆节\",\"wage\":2,\"date\":\"2023-10-04\",\"rest\":1},\"10-05\":{\"holiday\":true,\"name\":\"国庆节\",\"wage\":2,\"date\":\"2023-10-05\",\"rest\":1},\"10-06\":{\"holiday\":true,\"name\":\"国庆节\",\"wage\":2,\"date\":\"2023-10-06\",\"rest\":1},\"10-07\":{\"holiday\":false,\"after\":true,\"wage\":1,\"name\":\"国庆节后补班\",\"target\":\"国庆节\",\"date\":\"2023-10-07\",\"rest\":1},\"10-08\":{\"holiday\":false,\"after\":true,\"wage\":1,\"name\":\"国庆节后补班\",\"target\":\"国庆节\",\"date\":\"2023-10-08\",\"rest\":1},\"12-31\":{\"holiday\":true,\"name\":\"元旦\",\"wage\":2,\"date\":\"2023-12-31\",\"rest\":85}}}";
            //字符串转json
            JSONObject json = JSON.parseObject(sss);
            //根据holiday字段获取jsonObject内容
            JSONObject holiday = json.getJSONObject("holiday");
            if (holiday.size() == 0) {
                return null;
            }
            for (Map.Entry<String, Object> entry : holiday.entrySet()) {
                String value = entry.getValue().toString();
                JSONObject jsonObject = JSONObject.parseObject(value);
                String hoBool = jsonObject.getString("holiday");
                String extra = jsonObject.getString("date");
                String name = jsonObject.getString("name");
                Holiday day = new Holiday();
                day.setId(String.valueOf(UUID.randomUUID()));
                day.setYear(date);
                day.setDate(extra);
                day.setName(name);
                //判断不是假期后调休的班
                if (hoBool.equals("true")) {
                    day.setIsRest(1);
                } else {
                    day.setIsRest(2);
                }

                days.add(day);
            }

            return days;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return days;
    }
}
