package org.linlinjava.jz.db.util.hm;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.commons.lang3.StringUtils;
import org.linlinjava.jz.db.util.JsonUtil;

import lombok.Data;

public class HolidayUtil {

	// http://timor.tech/api/holiday
	// http://timor.tech/api/holiday/year/2020
	public static String HOLIDAY_URL = "http://timor.tech/api/holiday/year/";

	// 年，每年的假期
	public static TreeMap<String, List<holidays>> jiaqi = new TreeMap<String, List<holidays>>();

	private static HolidayUtil instance = null;
	 
	/**
		 * 1.构造方法私有化，外部不能new
		 */
	private HolidayUtil() {
 
	}
 
	public static HolidayUtil getInstance() {
		if (instance == null) {
			instance = new HolidayUtil();
		}
		return instance;
	} 
	
	/**
	 * 获取某年的节假日 2020
	 * 
	 * @param year
	 */
	@SuppressWarnings("unchecked")
	public static void requestHoliday(String year) {
		if (null == year) {
			return;
		}
		String forObject = getRequest("http://timor.tech/api/holiday/year/"+year, null);
		if (StringUtils.isBlank(forObject)) {
			return;
		}
		// String forObject =
		// "{\"code\":0,\"holiday\":{\"01-01\":{\"holiday\":true,\"name\":\"元旦\",\"wage\":3,\"date\":\"2021-01-01\",\"rest\":6},\"01-02\":{\"holiday\":true,\"name\":\"元旦\",\"wage\":2,\"date\":\"2021-01-02\",\"rest\":1},\"01-03\":{\"holiday\":true,\"name\":\"元旦\",\"wage\":2,\"date\":\"2021-01-03\",\"rest\":1},\"02-07\":{\"holiday\":false,\"name\":\"春节前调休\",\"after\":false,\"wage\":1,\"target\":\"春节\",\"date\":\"2021-02-07\",\"rest\":35},\"02-11\":{\"holiday\":true,\"name\":\"除夕\",\"wage\":2,\"date\":\"2021-02-11\",\"rest\":39},\"02-12\":{\"holiday\":true,\"name\":\"初一\",\"wage\":3,\"date\":\"2021-02-12\",\"rest\":1},\"02-13\":{\"holiday\":true,\"name\":\"初二\",\"wage\":3,\"date\":\"2021-02-13\",\"rest\":1},\"02-14\":{\"holiday\":true,\"name\":\"初三\",\"wage\":3,\"date\":\"2021-02-14\",\"rest\":1},\"02-15\":{\"holiday\":true,\"name\":\"初四\",\"wage\":2,\"date\":\"2021-02-15\"},\"02-16\":{\"holiday\":true,\"name\":\"初五\",\"wage\":2,\"date\":\"2021-02-16\"},\"02-17\":{\"holiday\":true,\"name\":\"初六\",\"wage\":2,\"date\":\"2021-02-17\"},\"02-20\":{\"holiday\":false,\"name\":\"春节后调休\",\"after\":true,\"wage\":1,\"target\":\"春节\",\"date\":\"2021-02-20\"},\"04-03\":{\"holiday\":true,\"name\":\"清明节\",\"wage\":3,\"date\":\"2021-04-03\",\"rest\":1},\"04-04\":{\"holiday\":true,\"name\":\"清明节\",\"wage\":2,\"date\":\"2021-04-04\",\"rest\":1},\"04-05\":{\"holiday\":true,\"name\":\"清明节\",\"wage\":2,\"date\":\"2021-04-05\",\"rest\":1},\"04-25\":{\"holiday\":false,\"name\":\"劳动节前调休\",\"after\":false,\"wage\":1,\"target\":\"劳动节\",\"date\":\"2021-04-25\"},\"05-01\":{\"holiday\":true,\"name\":\"劳动节\",\"wage\":3,\"date\":\"2021-05-01\"},\"05-02\":{\"holiday\":true,\"name\":\"劳动节\",\"wage\":2,\"date\":\"2021-05-02\"},\"05-03\":{\"holiday\":true,\"name\":\"劳动节\",\"wage\":2,\"date\":\"2021-05-03\"},\"05-04\":{\"holiday\":true,\"name\":\"劳动节\",\"wage\":2,\"date\":\"2021-05-04\"},\"05-05\":{\"holiday\":true,\"name\":\"劳动节\",\"wage\":2,\"date\":\"2021-05-05\"},\"05-08\":{\"holiday\":false,\"name\":\"劳动节后调休\",\"after\":true,\"wage\":1,\"target\":\"劳动节\",\"date\":\"2021-05-08\"},\"06-12\":{\"holiday\":true,\"name\":\"端午节\",\"wage\":3,\"date\":\"2021-06-12\"},\"06-13\":{\"holiday\":true,\"name\":\"端午节\",\"wage\":2,\"date\":\"2021-06-13\"},\"06-14\":{\"holiday\":true,\"name\":\"端午节\",\"wage\":2,\"date\":\"2021-06-14\"},\"09-18\":{\"holiday\":false,\"after\":false,\"name\":\"中秋节前调休\",\"wage\":1,\"target\":\"中秋节\",\"date\":\"2021-09-18\"},\"09-19\":{\"holiday\":true,\"name\":\"中秋节\",\"wage\":3,\"date\":\"2021-09-19\"},\"09-20\":{\"holiday\":true,\"name\":\"中秋节\",\"wage\":2,\"date\":\"2021-09-20\"},\"09-21\":{\"holiday\":true,\"name\":\"中秋节\",\"wage\":2,\"date\":\"2021-09-21\"},\"09-26\":{\"holiday\":false,\"after\":false,\"name\":\"国庆节前调休\",\"wage\":1,\"target\":\"国庆节\",\"date\":\"2021-09-26\"},\"10-01\":{\"holiday\":true,\"name\":\"国庆节\",\"wage\":3,\"date\":\"2021-10-01\"},\"10-02\":{\"holiday\":true,\"name\":\"国庆节\",\"wage\":3,\"date\":\"2021-10-02\"},\"10-03\":{\"holiday\":true,\"name\":\"国庆节\",\"wage\":3,\"date\":\"2021-10-03\"},\"10-04\":{\"holiday\":true,\"name\":\"国庆节\",\"wage\":2,\"date\":\"2021-10-04\"},\"10-05\":{\"holiday\":true,\"name\":\"国庆节\",\"wage\":2,\"date\":\"2021-10-05\"},\"10-06\":{\"holiday\":true,\"name\":\"国庆节\",\"wage\":2,\"date\":\"2021-10-06\"},\"10-07\":{\"holiday\":true,\"name\":\"国庆节\",\"wage\":2,\"date\":\"2021-10-07\"},\"10-09\":{\"holiday\":false,\"name\":\"国庆节后调休\",\"after\":true,\"wage\":1,\"target\":\"国庆节\",\"date\":\"2021-10-09\"}}}";
		// forObject = "{\"code\":0,\"holiday\":{}}";

		Map<String, Map<String, holidays>> req = JsonUtil.getEntity(forObject, Map.class);
		Map<String, holidays> holiday = req.get("holiday");

		if (!holiday.isEmpty()) {
			List<holidays> holidayList = new ArrayList<holidays>();
			for (Map.Entry<String, holidays> h : holiday.entrySet()) {
				holidays hl = JsonUtil.getEntity(h.getValue(), holidays.class);
				holidayList.add(hl);
			}
			jiaqi.put(year, holidayList);
		}
		System.out.println(jiaqi.keySet());
	}

	/**
	 * 某一天属于什么假期
	 * 
	 * @param ymd 年月日2021-01-01
	 */
	public static holidays getHoliday(String ymd) {
		if (ymd == null) {
			return null;
		}

		String year = ymd.substring(0, 4);
		List<holidays> list = jiaqi.get(year);

		if (null == list) {
			requestHoliday(year);
		}

		list = jiaqi.get(year);
		if (null == list) {
			return null;
		}

		for (holidays h : list) {
			if (h.getDate().equals(ymd)) {
				return h;
			}
		}
		return null;
	}

	public static String getRequest(String url, String param) {
		String result = "";
		BufferedReader in = null;
		try {
			String urlNameString = url + "?" + param;
			if (StringUtils.isBlank(param)) {
				urlNameString = url;
			}
			// System.out.println(urlNameString);
			URL realUrl = new URL(urlNameString);
			// 打开和URL之间的连接
			URLConnection connection = realUrl.openConnection();
			// 设置通用的请求属性
			connection.setRequestProperty("accept", "*/*");
			connection.setRequestProperty("connection", "Keep-Alive");
			connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
			// 建立实际的连接
			connection.connect();
			// 获取所有响应头字段
			Map<String, List<String>> map = connection.getHeaderFields();
			// 遍历所有的响应头字段
			/*
			 * for (String key : map.keySet()) { System.out.println(key + "--->" +
			 * map.get(key)); }
			 */
			// 定义 BufferedReader输入流来读取URL的响应
			in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			String line;
			while ((line = in.readLine()) != null) {
				result += line;
			}
		} catch (Exception e) {
			System.out.println("发送GET请求出现异常！" + e);
			e.printStackTrace();
		}
		// 使用finally块来关闭输入流
		finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		return result;
	}

	public static void main(String[] args) throws Exception {
		// requestHoliday("2020");
		holidays holiday = getHoliday("2021-10-01");
		System.out.println(holiday);

	}

	@Data
	public static class holidays {
		/**
		 * "workday": { // 如果节假日前没调休，则此字段为null "holiday": false, // 此字段一定为false "name":
		 * "国庆前调休", // 调休的中文名 "wage": 1, // 薪资倍数，3表示是3倍工资 "after": false, //
		 * true表示放完假后调休，false表示先调休再放假 "target": '国庆节', // 表示调休的节假日 "date": "2018-09-29"
		 * // 表示要调休的日期 "rest": 1 // 同上。表示当前时间距离目标还有多少天。 },
		 */
		private boolean holiday;
		private String name;
		private boolean after;
		private int wage;
		private String target;
		private String date;
		private int rest;
	}

}
