package com.ywt.core.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
    private static final String[] WEEK_DAYS = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};

    private static final String[] WEEK_DAYS_SHORT = {"日", "一", "二", "三", "四", "五", "六"};

    /**
     * 将时间戳转换成日期字符串格式
     *
     * @param timestamp 时间戳
     * @return
     */
    public static String convertTimestampToDateString(long timestamp) {
        Date date = new Date(timestamp);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    /**
     * 将时间戳转换成日期字符串格式
     *
     * @param timestamp 时间戳
     * @param pattern   日期格式
     * @return
     */
    public static String convertTimestampToDateString(long timestamp, String pattern) {
        Date date = new Date(timestamp);

        if (StringHelper.isNullOrEmpty(pattern)) {
            pattern = "yyyy-MM-dd HH:mm:ss";
        }

        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    /**
     * 将时间戳转换成日期类型
     *
     * @param timestamp 时间戳
     * @return
     */
    public static Date convertTimestampToDate(long timestamp) {
        return new Date(timestamp);
    }

    /**
     * 格式化日期
     *
     * @param date
     * @param pattern
     * @return
     */
    public static String formatDate(Date date, String pattern) {
        if (StringHelper.isNullOrEmpty(pattern)) {
            pattern = "yyyy-MM-dd HH:mm:ss";
        }

        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    /**
     * 当前日期是星期几
     *
     * @param date
     * @return
     */
    public static String getWeekOfDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
        return sdf.format(date);
    }

    /**
     * 当前日期是星期几
     *
     * @param calendar
     * @return
     */
    public static String getDayOfWeek(Calendar calendar) {
        int index = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (index < 0) {
            index = 0;
        }
        return WEEK_DAYS[index];
    }

    /**
     * 当前日期是星期（缩略）
     *
     * @param calendar
     * @return
     */
    public static String getDayOfWeekShort(Calendar calendar) {
        int index = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (index < 0) {
            index = 0;
        }
        return WEEK_DAYS_SHORT[index];
    }

    public static Date parseDate(String str, String pattern) {
        if (StringHelper.isNullOrWhiteSpace(str)) {
            return null;
        }

        if (StringHelper.isNullOrWhiteSpace(pattern)) {
            pattern = "yyyy-MM-dd HH:mm:ss";    //默认日期格式
        }

        SimpleDateFormat format = new SimpleDateFormat(pattern);
        try {
            format.setLenient(false);
            return format.parse(str);
        } catch (ParseException e) {
            return null;
        }
    }

    public static String convertToString(Date date) {
        return convertToString(date, null);
    }

    public static String convertToString(Date date, String pattern) {
        if (Checker.isNone(date)) {
            return "";
        }

        if (StringHelper.isNullOrEmpty(pattern)) {
            pattern = "yyyy-MM-dd HH:mm:ss";
        }

        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    /**
     * 得到相对时间
     *
     * @param num 天数，大于0表示几天之后的时间，小于0表示几天之前的时间
     * @return 时间
     */
    public static Date getRelativeDate(int num) {
        return getRelativeDate(new Date(), num);
    }

    /**
     * 得到相对时间
     *
     * @param date 传入的时间
     * @param num  前几天，或者后几天
     * @return 改变之后的时间
     */
    public static Date getRelativeDate(Date date, int num) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, num);
        return calendar.getTime();
    }

    /**
     * 判断当前时间是否过了传入时间的次日00:00:00
     *
     * @param date
     * @return true: 是， false: 否
     */
    public static boolean compareNextDay(Date date) {
        if (date == null) {
            return false;
        }
        //  得到1天之后的时间
        date = getRelativeDate(date, 1);
        String pattern = "yyyy-MM-dd 00:00:00";
        String newDateStr = convertToString(date, pattern);
        Date newDate = parseDate(newDateStr, pattern);
        return new Date().after(newDate);
    }

    /**
     * 得到今天凌晨的时间
     *
     * @return {@link Date}
     */
    public static Date getCurrentDay() {
        String pattern = "yyyy-MM-dd 00:00:00";
        String newDateStr = convertToString(new Date(), pattern);
        return parseDate(newDateStr, pattern);
    }

    public static Date stringToDate(String strDate) {
        return stringToDate(strDate, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * String转Date
     *
     * @param strDate
     * @return
     */
    public static Date stringToDate(String strDate, String pattern) {
        if (null == strDate) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            return sdf.parse(strDate);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
