/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.common.utils;

import cn.hutool.core.codec.Base64;
import org.springblade.core.tool.utils.DateUtil;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 通用工具类
 *
 * @author Chill
 */
public class CommonUtil {

	public static final String pattern = "yyyy-MM-dd";

	/**
	 * 获取传入时间的第二天
	 * @param today
	 * @return
	 */
	public static String getNextDay(String today){
		Calendar cd = Calendar.getInstance();
		cd.setTime(DateUtil.parse(today, pattern));
		cd.add(Calendar.DATE, 1);
		String nextDay = DateUtil.format(cd.getTime(), pattern);
		return nextDay;
	}

	/**
	 * 计算传入时间的相对位移时间，后几天或者前几天
	 * @param theDay
	 * @param interval
	 * @return
	 */
	public static String getDay(String theDay, int interval){
		Calendar cd = Calendar.getInstance();
		cd.setTime(DateUtil.parse(theDay, pattern));
		cd.add(Calendar.DATE, interval);
		String ThatDay = DateUtil.format(cd.getTime(), pattern);
		return ThatDay;
	}

	/**
	 * 计算两个日期相差天数
	 * @param beginDate
	 * @param endDate
	 * @return
	 */
	public static long getInterval(String beginDate, String endDate){
		Date begin = DateUtil.parse(beginDate, pattern);
		Date end = DateUtil.parse(endDate, pattern);
		return (end.getTime() - begin.getTime()) / (24 * 60 * 60 * 1000);
	}

	/**
	 * 判断日期是否为周五
	 * @param theDay
	 * @return
	 */
	public static boolean isWeekLastDay(String theDay){
		Calendar cal = Calendar.getInstance();
		cal.setTime(DateUtil.parse(theDay, pattern));
		int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
		int[] weekDays = { 7, 1, 2, 3, 4, 5, 6 };
		return weekDays[w] == 5;
	}

	/**
	 * 判断日期是否为周五
	 * @param theDay
	 * @return
	 */
	public static boolean isWeekend(String theDay){
		Calendar cal = Calendar.getInstance();
		cal.setTime(DateUtil.parse(theDay, pattern));
		int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
		int[] weekDays = { 7, 1, 2, 3, 4, 5, 6 };
		return weekDays[w] == 6 || weekDays[w] == 7;
	}

	/**
	 * 根据传入时间，算出这周开始日期和结束日期
	 * @param theDay
	 * @return
	 */
	public static String[] getWeekRange(String theDay){
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		Calendar cal = Calendar.getInstance();
		cal.setTime(DateUtil.parse(theDay, pattern));
		int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
		int[] weekDays = { 7, 1, 2, 3, 4, 5, 6 };
		int today = weekDays[w];
		int begin = today == 7 ? 0 :  -today;
		int end = today == 7 ? 6 : 6 - today;

		Calendar beginDate = Calendar.getInstance();
		beginDate.setTime(DateUtil.parse(theDay, pattern));
		Calendar endDate = Calendar.getInstance();
		endDate.setTime(DateUtil.parse(theDay, pattern));
		beginDate.add(Calendar.DAY_OF_MONTH, begin);
		endDate.add(Calendar.DAY_OF_MONTH, end);

		String beginDateStr = format.format(beginDate.getTime());
		String endDateStr = format.format(endDate.getTime());
		return new String[]{beginDateStr, endDateStr};
	}

	/**
	 * 根据传入时间，算出这个月的开始日期和结束日期
	 * @param theDay
	 * @return
	 */
	public static String[] getMonthRange(String theDay){
		String[] split = theDay.split("-");
		String beginDateStr = split[0] + "-" + split[1] + "-" + "01";

		Calendar endDate = Calendar.getInstance();
		endDate.setTime(DateUtil.parse(theDay, pattern));
		int actualMaximum = endDate.getActualMaximum(Calendar.DAY_OF_MONTH);
		String endDateStr = split[0] + "-" + split[1] + "-" + actualMaximum;

		return new String[]{beginDateStr, endDateStr};
	}

	/**
	 * 判断日期是否为周五
	 * @param theDay
	 * @return
	 */
	public static boolean isMonthLastDay(String theDay){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(DateUtil.parse(theDay, pattern));
		return calendar.get(Calendar.DAY_OF_MONTH) == calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 解密方法
	 * @param data 要解密的数据
	 * @param key  解密key
	 * @param iv 解密iv
	 * @return 解密的结果
	 * @throws Exception
	 */
	private static String desEncryptByAes(String data, String key, String iv) {
		try {
			byte[] encrypted1 = new Base64().decode(data);

			Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
			SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
			IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());

			cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);

			byte[] original = cipher.doFinal(encrypted1);
			String originalString = new String(original);
			return originalString.trim();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static String myAesDecrypt(String data){
		String key = "cyzh20211126java";
		String iv = "cyzh20211126java";
		try {
			return desEncryptByAes(data, key, iv);
		}catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

}
