package com.bootdo.util;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * 时间小工具
 */
@Component
public class DateTool {
    private static final Logger logger = LoggerFactory.getLogger(DateTool.class);

    private static final long SECOND = 24 * 60 * 60 * 1000L;
    private static final SimpleDateFormat ym = new SimpleDateFormat("yyyyMM");
    private static final SimpleDateFormat ymd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static final SimpleDateFormat ymdhms = new SimpleDateFormat("yyyyMMddHHmmss");
    private static final SimpleDateFormat yyyy = new SimpleDateFormat("yyyy");
    private static final SimpleDateFormat yMMdd = new SimpleDateFormat("MM-dd");
    private static final SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyyMMdd");
    private static final SimpleDateFormat HHmmss = new SimpleDateFormat("HH:mm:ss");
    private static final SimpleDateFormat YYYYMMDD = new SimpleDateFormat("yyyy-MM-dd");

    public static final SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
    public static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static final SimpleDateFormat ymdDir = new SimpleDateFormat("yyyyMMdd");


    /**
     * 年
     * 2022
     */
    public int getYear(Date date) {
        int rel = 0;
        if (null == date) {
            return 0;
        }
        try {
            rel = Integer.parseInt(yyyy.format(date));
        } catch (Exception e) {
            return rel;
        }
        return rel;
    }

    /**
     * 年月
     * 202205
     */
    public String yearMonth() {
        return ym.format(new Date());
    }

    /**
     * 当日开始时间
     */
    public String getStartTime() {
        long current = System.currentTimeMillis();//当前时间毫秒数
        //今天零点零分零秒的毫秒数
        long zero = ((current / SECOND) * SECOND - TimeZone.getDefault().getRawOffset());
        return ymd.format(zero);
    }

    /**
     * 带T时间处理
     *
     * @param time
     * @return
     */
    public String getDateTimeT(long time) {
        String ymd = YYYYMMDD.format(new Date(time * 1000));
        String hms = HHmmss.format(new Date(time * 1000));
        return (ymd + "T" + hms);
    }

    /**
     * 今日0点的绝对秒
     */
    public Long getStartTimeLong() {
        long current = System.currentTimeMillis();//当前时间毫秒数
        //今天零点零分零秒的毫秒数
        long zero = ((current / SECOND) * SECOND - TimeZone.getDefault().getRawOffset());
        return zero / 1000;
    }

    /**
     * 当日结束时间
     */
    public String getEndTime() {
        long current = System.currentTimeMillis();//当前时间毫秒数
        //今天零点零分零秒的毫秒数
        long zero = (current / SECOND) * SECOND - TimeZone.getDefault().getRawOffset();
        //今天23点59分59秒的毫秒数
        long twelve = (zero + SECOND - 1);
        return ymd.format(twelve);
    }

    /**
     * 今天23点59分59秒 绝对秒
     */
    public Long getEndTimeLong() {
        long current = System.currentTimeMillis();//当前时间毫秒数
        //今天零点零分零秒的毫秒数
        long zero = (current / SECOND) * SECOND - TimeZone.getDefault().getRawOffset();
        //今天23点59分59秒的毫秒数
        long twelve = (zero + SECOND - 1);
        return twelve / 1000;
    }

    /**
     * 获取日期格式字符串时间
     */
    public String getDateTime(long time) {
        if (time == 0L) {
            return "";
        }
        String format = ymd.format(new Date(time * 1000));
        if (StringUtils.isNotBlank(format)) {
            return format;
        } else {
            return "";
        }
    }

    /**
     * 今天 2022-08-18 00:00:00
     */
    public String getDayStart() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        String startDate = ymd.format(calendar.getTime());
        startDate = startDate.substring(0, 10) + " 00:00:00";
        return startDate;
    }

    /**
     * 今天 2022-08-18 23:59:59
     */
    public String getDayEnd() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        String endDate = ymd.format(calendar.getTime());
        endDate = endDate.substring(0, 10) + " 23:59:59";
        return endDate;
    }

    /**
     * 获取前一天 2022-08-15 00:00:00
     */
    public static String getLastDayStart() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        String startDate = ymd.format(calendar.getTime());
        startDate = startDate.substring(0, 10) + " 00:00:00";
        return startDate;
    }

    /**
     * 获取前一天 时间戳
     */
    public static String getLastDayStartTimestamp() {
        try {
            String format = "yyyy-MM-dd HH:mm:ss";
            String dateStr = getLastDayStart();
            SimpleDateFormat ft = new SimpleDateFormat(format);
            Date date = ft.parse(dateStr);
            long resL = date.getTime();
            String res = StringKit.toString(resL / 1000);
            return StringKit.toString(res);
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * 获取前一天 2022-08-15 23:59:59
     */
    public String getLastDayEnd() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        String endDate = ymd.format(calendar.getTime());
        endDate = endDate.substring(0, 10) + " 23:59:59";
        return endDate;
    }

    /**
     * 获取前一天 年月日
     */
    public String getDayStr() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        String dayStr = yyyyMMdd.format(calendar.getTime());
        return dayStr;
    }

    /**
     * 获取年月日时间
     * yyyyMMdd
     */
    public String getDayStr(long longStr) {
        String rel = "";
        try {
            rel = yyyyMMdd.format(longStr * 1000);
        } catch (Exception e) {

        }
        return rel;
    }

    /**
     * 获取年月日时间
     * yyyyMMdd
     */
    public String getDayStr2(String date) {
        String rel = "";
        try {
            rel = yyyyMMdd.format(ymd.parse(date));
        } catch (Exception e) {

        }
        return rel;
    }

    /**
     * 日期字符串 计算 绝对秒
     */
    public long getDayLong(String date) {
        try {
            Date parse = ymd.parse(date);
            return parse.getTime() / 1000;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0L;
    }

    /**
     * 获取当前年份
     */
    public String getYear() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        String endDate = ymd.format(calendar.getTime());
        endDate = endDate.substring(0, 10) + " 23:59:59";
        return endDate;
    }

    /**
     * 获取每个月的第一天
     */
    public String getMonthFirst() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DATE, 1);
        calendar.roll(Calendar.DATE, -1);

        SimpleDateFormat sdfTwo = new SimpleDateFormat("yyyy-MM-");
        String startTime = sdfTwo.format(new Date()) + "01 00:00:00";
        return startTime;
    }

    /**
     * 获取每个月的最后一天
     */
    public String getMonthLast() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DATE, 1);
        calendar.roll(Calendar.DATE, -1);

        int maxDate = calendar.get(Calendar.DATE);
        SimpleDateFormat sdfTwo = new SimpleDateFormat("yyyy-MM-");
        String endTime = sdfTwo.format(new Date()) + maxDate + " 23:59:59";
        return endTime;
    }


    /**
     * 获取前n天 2022-08-15 23:59:59
     */
    public String getDayStart(int n) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, -n);
        String startDate = ymd.format(calendar.getTime());
        startDate = startDate.substring(0, 10) + " 00:00:00";
        return startDate;
    }

    /**
     * 获取前n天 2022-08-15 23:59:59
     */
    public String getDayStartYYYYMMDD(int n) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, -n);
        String startDate = ymd.format(calendar.getTime());
        startDate = startDate.substring(0, 10);
        return startDate;
    }

    /**
     * 获取前n天 2022-08-15 23:59:59
     */
    public String getDayEnd(int n) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, -n);
        String endDate = ymd.format(calendar.getTime());
        endDate = endDate.substring(0, 10) + " 23:59:59";
        return endDate;
    }

    /**
     * 获取n天前的指定日期
     */
    public String getYMD(int n) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, -n);
        String ymd = yyyyMMdd.format(calendar.getTime());
        return ymd;
    }

    /**
     * 获取当前ymdhms日期格式
     */
    public String getYmdhms() {
        return ymdhms.format(new Date());
    }

    /**
     * yyyyMMdd转mm.dd
     */
    public String timeConversion(String yyyyMMdd) {
        String rel = "";
        try {
            if (StringUtils.isBlank(yyyyMMdd)) {
                return rel;
            }
            if (yyyyMMdd.length() != 8) {
                return rel;
            }
            String mm = yyyyMMdd.substring(4, 6);
            String dd = yyyyMMdd.substring(6, 8);
            rel = String.valueOf(Integer.parseInt(mm)) + "." + String.valueOf(Integer.parseInt(dd));
        } catch (Exception e) {

        }
        return rel;
    }

    /**
     * 年月
     * m=0表示当月
     * m=1表示前一月
     * m=2表示前两月
     */
    public String yearMonth(int m) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.MONTH, -m);
        return ym.format(calendar.getTime());
    }

    public String getDayStr3(String date) {
        String rel = "";
        try {
            Date parse = yyyyMMdd.parse(date);
        } catch (ParseException e) {

        }
        return rel;
    }

    public long daysBetween(String one, String two) {
        long difference = 0L;
        try {
            Date parseOne = yyyyMMdd.parse(one);
            Date parseTwo = yyyyMMdd.parse(two);
            difference = (parseOne.getTime() - parseTwo.getTime()) / 86400000;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return Math.abs(difference) + 1;
    }


    /**
     * 获取前N天  MMdd
     */
    public static String getLastDayMmdd(int num) {
        if (num == 0) {
            num = 1;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, -num);
        String startDate = yMMdd.format(calendar.getTime());
        return startDate;
    }

    /**
     * n个月前的1号开始时间
     */
    public String getMonthFirst(int n) {
        SimpleDateFormat sdfTwo = new SimpleDateFormat("yyyy-MM");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.MONTH, -n);
        Date m = calendar.getTime();
        String format = sdfTwo.format(m);
        return format + "-01 00:00:00";
    }

    /**
     * n个月前的最后一天结束时间
     */
    public String getMonthLast(int n) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MONTH, 1 - n + 2);
        calendar.set(Calendar.DATE, 1);
        calendar.roll(Calendar.DATE, -1);
        int maxDate = calendar.get(Calendar.DATE);
        SimpleDateFormat sdfTwo = new SimpleDateFormat("yyyy-MM-");
        String endTime = sdfTwo.format(calendar.getTime()) + maxDate + " 23:59:59";
        return endTime;
    }


    /**
     * 时间转换为时间戳
     */
    public static String dateToStamp(String s) {

        try {
            String res;
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            Date date = simpleDateFormat.parse(s);
            long ts = date.getTime();
            ts = ts / 1000;
            res = StringKit.toString(ts);
            return res;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 时间转换为时间戳
     */
    public static String getNowTimeStr() {

        try {
            String currentTime = df.format(new Date());
            return currentTime;
        } catch (Exception e) {
            logger.info(StringKit.getTrace(e));
            e.printStackTrace();
        }
        return "";
    }

    public static String getTime() {
        return ymdhms.format(new Date());
    }


    /**
     * 时间戳转换成 标准时间
     *
     * @param seconds
     * @param format
     * @return
     */
    public static String timeStamp2Date(String seconds, String format) {
        if (seconds == null || seconds.isEmpty() || seconds.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(seconds + "000")));
    }

    /**
     * 上个月 时间戳转换成 标准时间
     *
     * @param format
     * @return
     */
    public static String getLastMonth(String format) {
        Calendar calendar = Calendar.getInstance();//日历对象
        calendar.setTime(new Date());//设置当前日期
        calendar.add(Calendar.MONTH, -1);//月份减一
        Date lastMonth = calendar.getTime();

        if (format == null || format.isEmpty()) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(lastMonth);
    }

    public static String getDateStr(String sendTime) {
        String dateStr;
        String dateFir = StringKit.toString(sendTime);
        Date date = null;
        if ("".equals(dateFir)) {
            date = new Date(System.currentTimeMillis());
        } else {
            Long dateFirL = Long.valueOf(dateFir);
            date = new Date(dateFirL * 1000);
        }
        dateStr = sdf.format(date);
        return dateStr;
    }

    public static String convertTimeToString(String longTime) {
        String format = "yyyyMMddHHmmss";
        try {
            Timestamp t = new Timestamp(Long.parseLong(longTime) * 1000L);
            SimpleDateFormat sDateFormat = new SimpleDateFormat(format);
            return sDateFormat.format(t);
        } catch (Exception ex) {
            throw new RuntimeException((new StringBuilder()).append("Can't format the time by format[").append(format).append("]!").toString());
        }
    }

    /**
     * 30天前
     *
     * @return
     */
    public static String readRecordStart() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, -30);
        String startDate = sdf.format(calendar.getTime());
        startDate = startDate.substring(0, 10) + " 00:00:00";
        return startDate;
    }

    public static String readRecordDayStart() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, 0);
        String startDate = sdf.format(calendar.getTime());
        startDate = startDate.substring(0, 10) + " 00:00:00";
        return startDate;
    }

    /**
     * 今天
     *
     * @return
     */
    public static String readRecordEnd() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        String endDate = sdf.format(calendar.getTime());
        endDate = endDate.substring(0, 10) + " 23:59:59";
        return endDate;
    }

    /**
     * 根据绝对秒 计算年月 yyyyMM
     *
     * @param tempLongTime
     * @return
     */
    public static int yearMonth(String tempLongTime) {
        if (StringUtils.isBlank(tempLongTime) || tempLongTime.length() != 10) {
            return 0;
        }
        String format = ym.format(new Date(Long.parseLong(tempLongTime) * 1000L));
        return Integer.parseInt(format);
    }

    public static String yearMonthDay(String tempLongTime) {
        if (StringUtils.isBlank(tempLongTime) || tempLongTime.length() != 10) {
            return null;
        }
        String format = ymd.format(new Date(Long.parseLong(tempLongTime) * 1000L));
        return format;
    }


    /**
     * 特殊场景
     * 通过jpg获取时间
     *
     * @param tempLongTime
     * @return
     */
    public static long ymdhms(String tempLongTime) {
        long rel = 0;
        try {
            rel = ymdhms.parse(tempLongTime).getTime() / 1000;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return rel;
    }

    public static long getLongTime(String tempLongTime) {
        long rel = 0;
        try {
            rel = ymd.parse(tempLongTime).getTime() / 1000;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return rel;
    }

    public static String ymdhmsDate() {
        return ymdhms.format(new Date());
    }

    /**
     * 开始时间
     *
     * @return
     */
    public String getStart() {
        long time = System.currentTimeMillis() / 1000 - 60 * 60 * 24 * 180;
        return ymd.format(new Date(time * 1000));
    }

    /**
     * 当前日期
     *
     * @return
     */
    public String getEnd() {
        long time = System.currentTimeMillis();
        return ymd.format(new Date(time));
    }


    /**
     * 时间戳对应日期目录
     */
    public static String dateDir(long longTime) {
        String tempLongTime = String.valueOf(longTime);
        String format = ymdDir.format(new Date(Long.parseLong(tempLongTime) * 1000L));
        return format;
    }

}
