package com.miya.fastcashier.utils;



import com.blankj.utilcode.util.StringUtils;

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

/**
 * 日期相关工具类
 */
public class DateUtils {

    public static final String DF_YYYYMMDD = "yyyyMMdd";
    public static final String DF_YYMMDD = "yyMMdd";
    public static final String DF_YYYY_MM_DD = "yyyy-MM-dd";
    public static final String DF_YYYY_MM_DD2 = "yyyy/MM/dd";
    public static final String DF_YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
    public static final String DF_YYYY_MM_DDHHMMSS = "yyyy-MM-dd HH:mm:ss";
    public static final String DF_HHMMSS = "HHmmss";
    public static final String DF_HHMMSS2 = "HH:mm:ss";
    public static final String DF_CN_YYYY_MM_DD = "yyyy年MM月dd日";
    public static final String DF_HHMM = "HH:mm";

    /**
     * 带ms的时间戳，与poshub对接时添加
     */
    public static final String DF_YYYY_MM_DD_HHMMSSSS = "yyyy-MM-dd HH:mm:ss.SSS";


    public static final String DF_YYYYMMDDHHMM = "yyyy/MM/dd HH:mm";

    private static final String TAG = "DateUtils";

    /**
     * 将日期转换成yyyy-MM-dd HH:mm:ss.SSS字符串
     *
     * @return yyyy-MM-dd HH:mm:ss.SSS
     */
    public static String format17(Date date) {
        return format(date, DF_YYYY_MM_DD_HHMMSSSS);
    }

    /**
     * 将日期转换成字符串
     *
     * @param date
     * @param format
     * @return
     */
    public static String format(Date date, String format) {
        if (date == null) {
            throw new IllegalArgumentException("Param date is null!");
        }
        if (StringUtils.isEmpty(format)) {
            throw new IllegalArgumentException("Param format is blank!");
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    /**
     * 将日期转换成yyyyMMddHHmmss字符串
     *
     * @param date
     * @return
     */
    public static String format14(Date date) {
        return format(date, DF_YYYYMMDDHHMMSS);
    }

    /**
     * 将日期转换成yyyy-MM-dd- HH:mm:ss字符串
     *
     * @param date
     * @return
     */
    public static String format18(Date date) {
        return format(date, DF_YYYY_MM_DDHHMMSS);
    }

    public static String format8(Date date) {
        return format(date, DF_YYYYMMDD);
    }

    public static String format6(Date date) {
        return format(date, DF_YYMMDD);
    }

    public static String format10(Date date) {
        return format(date, DF_YYYY_MM_DD);
    }

    public static String formatCN(Date date) {
        return format(date, DF_CN_YYYY_MM_DD);
    }

    public static String formatCustom(Date date, String style) {
        return format(date, style);
    }

    public static Date parse8(String dateStr) throws ParseException {
        return parse(dateStr, DF_YYYYMMDD);
    }

    public static Date parse10(String dateStr) throws ParseException {
        return parse(dateStr, DF_YYYY_MM_DD);
    }

    /**
     * 返回当前日期 yyyyMMdd格式 字符串
     *
     * @return
     */
    public static String getNow8() {
        return format8(new Date());
    }

    /**
     * 返回当前日期 yyyyMMdd格式 字符串
     *
     * @return
     */
    public static String getNow6() {
        return format6(new Date());
    }

    /**
     * 返回当前日期 yyyyMMdd格式 字符串
     *
     * @return
     */
    public static String getNow10() {
        return format10(new Date());
    }

    /**
     * 获取当天0点 date对象
     *
     * @return
     */
    public static Date getToday() {
        Date today = null;
        try {
            today = parse(getNow8(), DF_YYYYMMDD);
        } catch (ParseException e) {
            //unreachable
            today = new Date();
        }
        return today;
    }


    /**
     * 根据时间的字符串转成Date对象
     * yyyy-MM-dd HH:mm:ss
     *
     * @return
     */
    public static Date getDateByDateString(String dateStr) {
        Date today = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(DF_YYYY_MM_DDHHMMSS);
            return sdf.parse(dateStr);
        } catch (ParseException e) {
            //unreachable
            today = new Date();
        }
        return today;
    }


    /**
     * 将不同格式的 String 转换成 时间戳
     *
     * @param str
     * @param formatStyle
     * @return
     */
    public static long stringToLong(String str, String formatStyle) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(formatStyle);
        return sdf.parse(str).getTime();//毫秒
    }

    /**
     * 根据时间的字符串转成Date对象
     * yyyy-MM-dd HH:mm:ss
     *
     * @return
     */
    public static Date getDateByDateString2(String dateStr) {
        Date today = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(DF_YYYYMMDDHHMMSS);
            return sdf.parse(dateStr);
        } catch (ParseException e) {
            //unreachable
            today = new Date();
        }
        return today;
    }


    public static String getDateStringByTimeStamp(long timestamp) {

        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(timestamp);
        SimpleDateFormat sdf = new SimpleDateFormat(DF_YYYY_MM_DDHHMMSS);
        return sdf.format(c.getTime());

    }

    public static String getDateStringByTimeStamp(long timestamp, String pattern) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(timestamp);
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(c.getTime());
    }

    /**
     * 获取当年1月1日0点 date对象
     *
     * @return
     */
    public static Date getNowYear() {
        Date year = null;
        try {
            String format = "yyyy";
            year = parse(format(new Date(), format), format);
        } catch (ParseException e) {
            //unreachable
            year = new Date();
        }
        return year;
    }

    /**
     * 获取当月1号0点 date对象
     *
     * @return
     */
    public static Date getNowMonth() {
        Date month = null;
        try {
            String format = "yyyy-MM";
            month = parse(format(new Date(), format), format);
        } catch (ParseException e) {
            //unreachable
            month = new Date();
        }
        return month;
    }

    public static int getMonthSpace(Date date1, Date date2) {
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(date1);
        c2.setTime(date2);
        int result = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR);
        return result == 0 ? 1 : Math.abs(result);
    }

    public static int getYearSpace4Now(String date) throws ParseException {

        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        c1.setTime(new Date());
        c2.setTime(sdf.parse(date));
        int result = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR);
        return result == 0 ? 1 : Math.abs(result);
    }

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

    /**
     * @return 获取当前月第一天：
     */
    public static Date getFirstDateOfCurrentMonth() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        try {
            return parse(format8(c.getTime()), DF_YYYYMMDD);
        } catch (ParseException e) {
            return null;
        }

    }

    /**
     * @return 获取下月第一天：
     */
    public static Date getFirstDateOfNextMonth() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 1);
        c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        try {
            return parse(format8(c.getTime()), DF_YYYYMMDD);
        } catch (ParseException e) {
            return null;
        }

    }

    /**
     * @return 获取当前月最后一天
     */
    public static Date getListDateOfCurrentMonth() {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        try {
            return parse(format8(c.getTime()), DF_YYYYMMDD);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * @return 当月的总天数
     */
    public static int getCurrentMonthDay() {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1);
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }

    /**
     * 计算时间间隔
     *
     * @param fDate 上次时间
     * @param oDate 本次时间
     * @return 精确到天
     */
    public static int getIntervalDays(Date fDate, Date oDate) {
        if (fDate == null || oDate == null) {
            return -1;
        }
        try {
            fDate = parse(format8(fDate), DF_YYYYMMDD);
            oDate = parse(format8(oDate), DF_YYYYMMDD);
        } catch (ParseException e) {
        }
        long nd = 1000 * 24 * 60 * 60;//一天的毫秒数
        //获得两个时间的毫秒时间差异
        long diff = oDate.getTime() - fDate.getTime();
        Long day = diff / nd;//计算差多少天
        return day.intValue();

    }

    /**
     * @param date
     * @return
     * @throws ParseException
     */
    public static Date getDate8(Date date) {
        try {
            return parse8(format8(date));
        } catch (ParseException e) {
            //unreachable
        }
        return null;
    }

    /**
     * 时间加days天
     *
     * @param date
     * @param days
     * @return
     */
    public static Date addDate(Date date, int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, days);
        return calendar.getTime();
    }

    /**
     * 日期加法运算
     *
     * @param sourceDate
     * @param months     增加的月数，可为负数
     * @return
     */
    public static Date addMonths(Date sourceDate, int months) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(sourceDate);
        calendar.add(Calendar.MONTH, months);
        return calendar.getTime();
    }


    /**
     * 时间戳转换成日期格式字符串
     *
     * @param mseconds 精确到毫秒的字符串
     * @param format
     * @return
     */
    public static String timeStamp2Date(String mseconds, String format) {
        if (mseconds == null || mseconds.isEmpty() || mseconds.equals("null")) {
            return "";
        }
        if (format == null || format.isEmpty()) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(mseconds)));
    }


    /**
     * @param startDate 开始时间
     * @param endDate   结束时间
     * @return true在时间段内，false不在时间段内
     */
    public static boolean hourMinuteBetween(String startDate, String endDate) {

        try {
            SimpleDateFormat format = new SimpleDateFormat("HH:mm");
            Date now = format.parse(format(new Date(),DF_HHMM));
            Date start = format.parse(startDate);
            Date end = format.parse(endDate);
            long nowTime = now.getTime();
            long startTime = start.getTime();
            long endTime = end.getTime();
            return nowTime >= startTime && nowTime <= endTime;
        } catch (Exception e) {
            return false;
        }

    }

}
