package com.bonus.sharedstation.common.utils;

import cn.hutool.core.exceptions.UtilException;

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

public class DateUtils {

    public static SimpleDateFormat YEAR_MONTH_DAY_HOUR_MINUTE_SECOND = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static SimpleDateFormat YEAR_MONTH_DAY = new SimpleDateFormat("yyyy-MM-dd");
    public static SimpleDateFormat YEAR_MONTH = new SimpleDateFormat("yyyy-MM");
    public static SimpleDateFormat YEAR = new SimpleDateFormat("yyyy");
    public static SimpleDateFormat CST;

    public DateUtils() {
    }

    public static Date parseString(String dateStr) {
        try {
            Date date = null;
            if (dateStr.toUpperCase().indexOf("CST") == 20) {
                date = CST.parse(dateStr);
            } else if (dateStr.trim().length() > 10) {
                date = YEAR_MONTH_DAY_HOUR_MINUTE_SECOND.parse(dateStr);
            } else if (dateStr.trim().length() <= 10 && dateStr.trim().length() > 7 && dateStr.indexOf("-") > 0) {
                date = YEAR_MONTH_DAY.parse(dateStr);
            } else if (dateStr.trim().length() <= 7 && dateStr.trim().length() > 4 && dateStr.indexOf("-") > 0) {
                date = YEAR_MONTH.parse(dateStr);
            } else {
                if (dateStr.trim().length() != 4) {
                    throw new UtilException("DATE_FORMAT_ERROR", "时间字符串格式化异常");
                }

                date = YEAR.parse(dateStr);
            }

            return date;
        } catch (Exception var2) {
            throw new UtilException("DATE_FORMAT_ERROR", "时间字符串格式化异常", var2);
        }
    }
    public static long  parseStringToTime(String dateStr) {
        try {
            Date date = null;
            if (dateStr.toUpperCase().indexOf("CST") == 20) {
                date = CST.parse(dateStr);
            } else if (dateStr.trim().length() > 10) {
                date = YEAR_MONTH_DAY_HOUR_MINUTE_SECOND.parse(dateStr);
            } else if (dateStr.trim().length() <= 10 && dateStr.trim().length() > 7 && dateStr.indexOf("-") > 0) {
                date = YEAR_MONTH_DAY.parse(dateStr);
            } else if (dateStr.trim().length() <= 7 && dateStr.trim().length() > 4 && dateStr.indexOf("-") > 0) {
                date = YEAR_MONTH.parse(dateStr);
            } else {
                if (dateStr.trim().length() != 4) {
                    throw new UtilException("DATE_FORMAT_ERROR", "时间字符串格式化异常");
                }

                date = YEAR.parse(dateStr);
            }

            return date.getTime();
        } catch (Exception var2) {
            throw new UtilException("DATE_FORMAT_ERROR", "时间字符串格式化异常", var2);
        }
    }

    public static Date parseString(String dateStr, String pattern) {
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
            Date date = dateFormat.parse(dateStr);
            return date;
        } catch (Exception var4) {
            throw new UtilException("DATE_FORMAT_ERROR", "时间字符串格式化异常", var4);
        }
    }

    public static Date addDay(int dayNum, Date date, boolean b) {
        if (date == null) {
            if (!b) {
                return null;
            }

            date = new Date();
        }

        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(5, dayNum);
        return c.getTime();
    }

    public static Date addDay(int dayNum, Date date) {
        return addDay(dayNum, date, true);
    }

    public Date addDay(int dayNum) {
        return addDay(dayNum, (Date)null);
    }

    public static int specifyNumDayMonth(Date date) {
        try {
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            int n = c.getActualMaximum(5);
            return n;
        } catch (Exception var3) {
            throw new UtilException("DATE_FORMAT_ERROR", "时间字符串格式化异常", var3);
        }
    }

    public static int specifyNumDayMonth(String dateStr) {
        try {
            Date date = parseString(dateStr);
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            int n = c.getActualMaximum(5);
            return n;
        } catch (Exception var4) {
            throw new UtilException("DATE_FORMAT_ERROR", "时间字符串格式化异常", var4);
        }
    }

    public static String formatDate(Date date) {
        String format = YEAR_MONTH_DAY_HOUR_MINUTE_SECOND.format(date);
        return format;
    }

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

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

    static {
        CST = new SimpleDateFormat("EEE MMM dd hh:mm:ss zzz yyyy", Locale.ENGLISH);
    }
}
