/**
 * Copyright (c) 2015-2016, Chill Zhuang 庄骞 (smallchill@163.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.enilu.flash.utils;


import org.apache.commons.lang3.time.DateFormatUtils;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class DateUtil {

	public static  final String DATE_TIME_FMT = "yyyy-MM-dd HH:mm:ss";
	public static  final String DATE_FMT = "yyyy-MM-dd";

	private static final Object LOCK = new Object();

	private static final Map<String, ThreadLocal<SimpleDateFormat>> POOL = new HashMap<String, ThreadLocal<SimpleDateFormat>>();
	/**
	 * 获取YYYY格式
	 *
	 * @return
	 */
	public static String getYear() {
		return formatDate(new Date(), "yyyy");
	}

	/**
	 * 获取YYYY格式
	 *
	 * @return
	 */
	public static String getYear(Date date) {
		return formatDate(date, "yyyy");
	}

	/**
	 * 获取YYYY-MM-DD格式
	 *
	 * @return
	 */
	public static String getDay() {
		return formatDate(new Date(), DATE_FMT);
	}

	/**
	 * 获取YYYY-MM-DD格式
	 *
	 * @return
	 */
	public static String getDay(Date date) {
		return formatDate(date, DATE_FMT);
	}

	/**
	 * 获取YYYYMMDD格式
	 *
	 * @return
	 */
	public static String getDays() {
		return formatDate(new Date(), "yyyyMMdd");
	}

	/**
	 * 获取YYYYMMDD格式
	 *
	 * @return
	 */
	public static String getDays(Date date) {
		return formatDate(date, "yyyyMMdd");
	}

	/**
	 * 获取YYYY-MM-DD HH:mm:ss格式
	 *
	 * @return
	 */
	public static String getTime() {
		return formatDate(new Date(), DATE_TIME_FMT);
	}

	/**
	 * 获取YYYY-MM-DD HH:mm:ss.SSS格式
	 *
	 * @return
	 */
	public static String getMsTime() {
		return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS");
	}

	/**
	 * 获取YYYYMMDDHHmmss格式
	 *
	 * @return
	 */
	public static String getAllTime() {
		return formatDate(new Date(), "yyyyMMddHHmmss");
	}
	public static String getAllTime2() {
		return formatDate(new Date(), "yyyyMMddHHmmssSSS");
	}

	/**
	 * 获取YYYY-MM-DD HH:mm:ss格式
	 *
	 * @return
	 */
	public static String getTime(Date date) {
		return formatDate(date, DATE_TIME_FMT);
	}

	public static String formatDate(Date date, String pattern) {
		String formatDate = null;
		if (StringUtil.isNotEmpty(pattern)) {
			formatDate = DateFormatUtils.format(date, pattern);
		} else {
			formatDate = DateFormatUtils.format(date, DATE_FMT);
		}
		return formatDate;
	}

	/**
	 * @Title: compareDate
	 * @Description:(日期比较，如果s>=e 返回true 否则返回false)
	 * @param s
	 * @param e
	 * @return boolean
	 * @throws
	 * @author luguosui
	 */
	public static boolean compareDate(String s, String e) {
		if (parseDate(s) == null || parseDate(e) == null) {
			return false;
		}
		return parseDate(s).getTime() >= parseDate(e).getTime();
	}

	/**
	 * 格式化日期
	 *
	 * @return
	 */
	public static Date parseDate(String date) {
		return parse(date,DATE_FMT);
	}

	/**
	 * 格式化日期
	 *
	 * @return
	 */
	public static Date parseTime(String date) {
		return parse(date,DATE_TIME_FMT);
	}

	/**
	 * 格式化日期
	 *
	 * @return
	 */
	public static Date parse(String date, String pattern) {
		if (date != null) {
			if (pattern == null || "".equals(pattern)) {
				return null;
			}
			DateFormat format = getDFormat(pattern);
			try {
				return format.parse(date);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	public static SimpleDateFormat getDFormat(String pattern) {
		ThreadLocal<SimpleDateFormat> tl = POOL.get(pattern);
		if (tl == null) {
			synchronized (LOCK) {
				tl = POOL.get(pattern);
				if (tl == null) {
					final String p = pattern;
					tl = new ThreadLocal<SimpleDateFormat>() {
						@Override
						protected synchronized SimpleDateFormat initialValue() {
							return new SimpleDateFormat(p);
						}
					};
					POOL.put(p, tl);
				}
			}
		}
		return tl.get();
	}

	public static  String formatDate(Date date){
		return format(date,DATE_FMT);
	}
	public static  String formatTime(Date date){
		return format(date,DATE_TIME_FMT);
	}
	/**
	 * 格式化日期
	 *
	 * @return
	 */
	public static String format(Date date, String pattern) {
		return DateFormatUtils.format(date, pattern);
	}

	/**
	 * 把日期转换为Timestamp
	 *
	 * @param date
	 * @return
	 */
	public static Timestamp format(Date date) {
		return new Timestamp(date.getTime());
	}

	/**
	 * 校验日期是否合法
	 *
	 * @return
	 */
	public static boolean isValidDate(String s) {
		return parse(s, DATE_TIME_FMT) != null;
	}

	/**
	 * 校验日期是否合法
	 *
	 * @return
	 */
	public static boolean isValidDate(String s, String pattern) {
        return parse(s, pattern) != null;
	}

	public static int getDiffYear(String startTime, String endTime) {
		DateFormat fmt = new SimpleDateFormat(DATE_FMT);
		try {
			int years = (int) (((fmt.parse(endTime).getTime() - fmt.parse(
					startTime).getTime()) / (1000 * 60 * 60 * 24)) / 365);
			return years;
		} catch (Exception e) {
			// 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
			return 0;
		}
	}

	/**
	 * 获取昨天
	 * @return
	 */
	public static Date getPreday() {
		return  getPreday(1);
	}

	/**
	 * 获取之前N天
	 * @param day
	 * @return
	 */
	public static Date getPreday(Integer day) {
		return  getAfterDay(-1*day);
	}

	/**
	 * 获取之后N天
	 * @param day
	 * @return
	 */
	public static Date getAfterDay(Integer day) {
		Calendar today = Calendar.getInstance();
		today.add(Calendar.DATE, day);
		return today.getTime();
	}
	/**
	 * <li>功能描述：时间相减得到天数
	 *
	 * @param beginDateStr
	 * @param endDateStr
	 * @return long
	 * @author Administrator
	 */
	public static long getDaySub(String beginDateStr, String endDateStr) {
		long day = 0;
		SimpleDateFormat format = new SimpleDateFormat(
				DATE_FMT);
		Date beginDate = null;
		Date endDate = null;

		try {
			beginDate = format.parse(beginDateStr);
			endDate = format.parse(endDateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		day = (endDate.getTime() - beginDate.getTime()) / (24 * 60 * 60 * 1000);

		return day;
	}

	/**
	 * 得到n天之后的日期
	 *
	 * @param days
	 * @return
	 */
	public static Date getAfterDayDate(String days) {
		int daysInt = Integer.parseInt(days);

		Calendar canlendar = Calendar.getInstance(); // java.util包
		canlendar.add(Calendar.DATE, daysInt); // 日期减 如果不够减会将月变动
		Date date = canlendar.getTime();
		return date;

	}

	public static  Date getDateBefore(Integer num,Integer dateType){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.set(dateType, -num);
		return calendar.getTime();
	}

	/**
	 * 得到n天之后是周几
	 *
	 * @param days
	 * @return
	 */
	public static String getAfterDayWeek(String days) {
		int daysInt = Integer.parseInt(days);

		Calendar canlendar = Calendar.getInstance(); // java.util包
		canlendar.add(Calendar.DATE, daysInt); // 日期减 如果不够减会将月变动
		Date date = canlendar.getTime();

		SimpleDateFormat sdf = new SimpleDateFormat("E");
		String dateStr = sdf.format(date);

		return dateStr;
	}

	/**
	 * 获取本月1号00:00:00
	 * @return
	 */
	public static Date getCurrentMonth(){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTime();
	}

	/**
	 * 获取本年一月一日00:00:00
	 * @return
	 */
	public static Date getCurrentYear(){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.set(Calendar.MONTH, Calendar.JANUARY);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTime();
	}


	/**
	 * 根据自定义日期范围获取精确的日期范围
	 *
	 * @param date 自定义日期范围包括：today(今天),yesterday(昨天),seven(最近7天),thirty(最近30天),month(本月),year(本年)
	 * @return
	 */
	public static  Date[] getDateRange(String date){
		Date start = null;
		Date end = null;
		Date now = new Date();
		String nowFormat = formatDate(now);
		if(StringUtil.isEmpty(date)){
			return null;
		}
		switch (date){
			case "today":
				start = parseTime(nowFormat+" 00:00:00");
				end = parseTime(nowFormat+" 23:59:59");
				break;
			case "yesterday":
				Date yesterday = getPreday();
				String yesterdayFmt = formatDate(yesterday);
				start = parseTime(yesterdayFmt+" 00:00:00");
				end = parseTime(yesterdayFmt+ " 23:59:59");
				break;
			case "seven":
				Date last7Date = getPreday(7);
				start = parseTime(format(last7Date)+" 00:00:00");
				end = parseTime(nowFormat+" 23:59:59");
				break;
			case "thirty":
				Date last30Date = getPreday(30);
				start = parseTime(format(last30Date)+" 00:00:00");
				end = parseTime(nowFormat+" 23:59:59");
				break;
			case "month":
				start = getCurrentMonth();
				end = parseTime(nowFormat+" 23:59:59");
				break;
			case "year":
				start = getCurrentYear();
				end = parseTime(nowFormat+" 23:59:59");
				break;

			default:
					break;

		}
		return new Date[]{start,end};

	}


	public static void main(String[] args) {
		System.out.println(getTime(new Date()));
		System.out.println(getAfterDayWeek("3"));
	}

}
