package com.xueyi.common.web.utils;

import com.google.common.collect.Lists;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

public class DateUtils extends org.apache.commons.lang.time.DateUtils {
	public DateUtils() {
	}

	public static Date getPushForwardDate(Date date, Integer num) {
		if (date == null) {
			return null;
		} else {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.add(5, -(num - 1));
			calendar.set(11, 0);
			calendar.set(12, 0);
			calendar.set(13, 0);
			calendar.set(14, 0);
			return calendar.getTime();
		}
	}

	public static Date getFixedPushForwardDate(Date date, Integer num) {
		if (date == null) {
			return null;
		} else {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.add(5, -(num));
			calendar.set(11, 0);
			calendar.set(12, 0);
			calendar.set(13, 0);
			calendar.set(14, 0);
			return calendar.getTime();
		}
	}

	public static Date getOfDayFirst(Date date) {
		if (date == null) {
			return null;
		} else {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.set(11, 0);
			calendar.set(12, 0);
			calendar.set(13, 0);
			calendar.set(14, 0);
			return calendar.getTime();
		}
	}

	public static Date getOfDayLast(Date date) {
		if (date == null) {
			return null;
		} else {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.set(11, 23);
			calendar.set(12, 59);
			calendar.set(13, 59);
			calendar.set(14, 999);
			return calendar.getTime();
		}
	}

	public static Date getPageDayLast(Date date) {
		if (date == null) {
			return null;
		} else {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.add(5, -1);
			calendar.set(11, 23);
			calendar.set(12, 59);
			calendar.set(13, 59);
			calendar.set(14, 999);
			return calendar.getTime();
		}
	}

	public static Date getFirstDayOfMonth(Date date) {
		new SimpleDateFormat("yyyy-MM-dd ");
		GregorianCalendar gcLast = (GregorianCalendar)Calendar.getInstance();
		gcLast.setTime(date);
		gcLast.set(Calendar.DAY_OF_MONTH, 1);
		return gcLast.getTime();
	}

	public static Date getLastDayOfMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		return calendar.getTime();
	}

	public static Date getAttendanceTime(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(5, -2);
		calendar.set(11, 23);
		return calendar.getTime();
	}

	public static String getStartTime() {
		Date dNow = new Date();
		new Date();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dNow);
		calendar.add(5, -1);
		Date dBefore = calendar.getTime();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(dBefore);
	}

	/*/**
	 * @Author yangkai
	 * @Description 根据字符串日期返回星期几
	 * @Param [datetime]
	 * @return java.lang.String
	 **/
	public static String dateToWeek(String datetime) {
		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
		String[] weekDays = new String[]{"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
		Calendar cal = Calendar.getInstance();
		Date datet = null;

		try {
			datet = f.parse(datetime);
			cal.setTime(datet);
		} catch (ParseException var6) {
			var6.printStackTrace();
		}

		int w = cal.get(7) - 1;
		if (w < 0) {
			w = 0;
		}

		return weekDays[w];
	}

	public static String getDatePoor(Date endDate, Date nowDate) {
		long nd = 86400000L;
		long nh = 3600000L;
		long nm = 60000L;
		long diff = endDate.getTime() - nowDate.getTime();
		long day = diff / nd;
		long hour = diff % nd / nh;
		long min = diff % nd % nh / nm;
		return day + ":" + hour;
	}

	/**
	 * 1  2  3  4  5  6  7
	 * 日 一 二  三 四 五  六
	 * @param date
	 * @return
	 */
	public static Integer getDayOfWeek(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.DAY_OF_WEEK);
	}

	public static List<String> generateNearlyDateString(Date currentDate, Integer nearlyDays) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(currentDate);
		List<String> dateStringList = Lists.newLinkedList();

		for(int i = 0; i < nearlyDays; ++i) {
			calendar.add(5, i == 0 ? 0 : -1);
			dateStringList.add(formatDate(calendar.getTime(), "yyyy-MM-dd"));
		}

		return dateStringList;
	}

	public static String dateDiff(Date startDate, Date endDate) {
		if (null != startDate && null != endDate) {
			Long mills = endDate.getTime() - startDate.getTime();
			Integer minuteDiff = mills.intValue() / '\uea60';
			Integer hours = minuteDiff / 60;
			Integer minutes = minuteDiff % 60;
			String hoursStr = hours + "h";
			String minutesStr = minutes == 0 ? "" : minutes + "m";
			return hoursStr + minutesStr;
		} else {
			return "0h";
		}
	}

	public static Integer dateDiffMin(Date startDate, Date endDate) {
		if (null != startDate && null != endDate) {
			Long mills = endDate.getTime() - startDate.getTime();
			Integer minuteDiff = mills.intValue() / '\uea60';
			Integer hours = minuteDiff / 60;
			Integer minutes = minuteDiff % 60;
			return hours * 60 + minutes;
		} else {
			return 0;
		}
	}

	public static String intToHourMin(Integer minuteDiff) {
		Integer hours = minuteDiff / 60;
		Integer minutes = minuteDiff % 60;
		return hours+"小时"+ minutes+"分";
	}

	public static String formatDate(Date date, String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(date);
	}

	public static Date praseStringToDate(String dateStr) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat();
		return sdf.parse(dateStr);
	}

	public static Date parseLongToDate(Long dateLong) throws ParseException {
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(dateLong);
		return calendar.getTime();
	}

	public static Date parseLongToDate(String dateLong) throws ParseException {
		BigDecimal number = new BigDecimal(dateLong);
		return parseLongToDate(number.doubleValue());
	}
	public static Date parseLongToDate(Double dateLong) throws ParseException {
		Calendar calendar = Calendar.getInstance();
		DecimalFormat df = new DecimalFormat("#"); // 创建DecimalFormat对象
		String str = df.format(dateLong);
		if (str.indexOf(".")>-1) {
			str = str.replace(".","");
		}
		str = str.substring(0,13);
		BigDecimal number = new BigDecimal(str);
		calendar.setTimeInMillis(number.longValue());
		return calendar.getTime();
	}

	public static String praseLongToDateString(Long dateLong) throws ParseException {
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(dateLong);
		Date date = calendar.getTime();
		return formatDate(date, "yyyy-MM-dd HH:mm:ss");
	}

	public static String getMillsSecondsDouble() {
		Long cutime = System.currentTimeMillis();
		String cutimeStr = String.valueOf(cutime);
		BigDecimal d = new BigDecimal(cutimeStr.substring(0, 10) + "." + cutimeStr.substring(10));
		return d.stripTrailingZeros().toPlainString();
	}

	/**
	 *
	 * @param time 	2018-07-06 19:35:23
	 * @param pattern  yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static Date parseStrToDate(String time, String pattern) {
		SimpleDateFormat formatter = new SimpleDateFormat(pattern);
		ParsePosition pos = new ParsePosition(0);
		return formatter.parse(time, pos);
	}


	//形如yyyy-MM-dd格式的时间串，赠加或减少几天
	//如2022-06-06 增加一天 返回 2022-06-07
	public static String dateStrAdd(String dateStr, int addDays){
		Date date1 = DateUtils.parseStrToDate(dateStr, "yyyy-MM-dd");
		Date date2 = DateUtils.addDays(date1, addDays);
		String dateStr2 = DateUtils.formatDate(date2, "yyyy-MM-dd");
		return dateStr2;
	}

	public static int dateStrDiff(String startDateStr, String endDateStr) {
		Date date1 = DateUtils.parseStrToDate(startDateStr, "yyyy-MM-dd");
		Date date2 = DateUtils.parseStrToDate(endDateStr, "yyyy-MM-dd");

		Calendar cal1 = Calendar.getInstance();
		cal1.setTime(date1);

		Calendar cal2 = Calendar.getInstance();
		cal2.setTime(date2);
		int day1= cal1.get(Calendar.DAY_OF_YEAR);
		int day2 = cal2.get(Calendar.DAY_OF_YEAR);

		int year1 = cal1.get(Calendar.YEAR);
		int year2 = cal2.get(Calendar.YEAR);
		if (year1 != year2) {//同一年
			int timeDistance = 0;
			for (int i = year1; i < year2; i++) {
				if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)    //闰年
				{
					timeDistance += 366;
				} else {
					timeDistance += 365;
				}
			}

			return timeDistance + (day2 - day1);
		} else {// 不同年
			return day2 - day1;
		}
	}


	public static LocalDateTime currentDateToLocalDateTime(){
		Date todayDate = new Date();
		return dateToLocalDateTime(todayDate);
	}

	public static LocalDateTime dateToLocalDateTime(Date date){

		LocalDateTime ldt = date.toInstant()
				.atZone( ZoneId.systemDefault() )
				.toLocalDateTime();

		return ldt;
	}

	public static String isAmPm(){
		//判断当前时间是上午还是下午，上午返回am，下午返回pm
		LocalTime currentTime = LocalTime.now();

		if (currentTime.isBefore(LocalTime.NOON)) {
			return "am";
		} else {
			return "pm";
		}
	}
}
