package com.zanxiang.common.utils;

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

import java.lang.management.ManagementFactory;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAccessor;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * 时间工具类
 *
 * @author ruoyi
 */
public class DateUtils extends org.apache.commons.lang.time.DateUtils {
    public static String YYYY_MM_DD = "yyyy-MM-dd";

    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    private static String[] parsePatterns = {"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM", "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

    public static final DateTimeFormatter FORMAT_DATETIME = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    public static final DateTimeFormatter FORMAT_FOR_KEY = DateTimeFormatter.ofPattern("yyyy-MM-dd_HH:mm");
    public static final DateTimeFormatter FORMAT_DATE = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    public static final DateTimeFormatter FORMAT_TIME = DateTimeFormatter.ofPattern("HH:mm:ss");

    public static long localDateToSecond(LocalDate localDate) {
        return localDate.atStartOfDay(ZoneOffset.ofHours(8)).toEpochSecond();
    }

    public static long localDateToMilli(LocalDate localDate) {
        return localDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant().toEpochMilli();
    }

    public static Date localDateToDate(LocalDate localDate) {
        return Date.from(localDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant());
    }

    public static LocalDateTime localDateToLocalDateTime(LocalDate localDate) {
        return LocalDateTime.of(localDate, LocalTime.MIN);
    }

    public static long localDateTimeToSecond(LocalDateTime localDateTime) {
        return localDateTime.toEpochSecond(ZoneOffset.ofHours(8));
    }

    public static long localDateTimeToMilli(LocalDateTime localDateTime) {
        return localDateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
    }

    public static Date localDateTimeToDate(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneOffset.ofHours(8)).toInstant());
    }

    public static LocalDate secondToLocalDate(long second) {
        return Instant.ofEpochSecond(second).atZone(ZoneOffset.ofHours(8)).toLocalDate();
    }

    public static LocalDate milliToLocalDate(long milli) {
        return Instant.ofEpochMilli(milli).atZone(ZoneOffset.ofHours(8)).toLocalDate();
    }

    public static long milliToSecond(long milli) {
        return milli / 1000;
    }

    public static int milliToSecond2(long milli) {
        return ((Long) (milli / 1000)).intValue();
    }

    public static boolean equals(LocalDateTime date1, LocalDateTime date2) {
        if (date1 == null && date2 == null) {
            return true;
        }
        if (date1 == null || date2 == null) {
            return false;
        }
        return date1.compareTo(date2) == 0;
    }

    public static boolean equals(LocalDate date1, LocalDate date2) {
        if (date1 == null && date2 == null) {
            return true;
        }
        if (date1 == null || date2 == null) {
            return false;
        }
        return date1.compareTo(date2) == 0;
    }

    public static LocalDateTime secondToLocalDateTime(long second) {
        return Instant.ofEpochSecond(second).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
    }

    public static long secondToMilli(int second) {
        return second * 1000;
    }

    public static long secondToMilli(long second) {
        return second * 1000;
    }

    public static LocalDateTime milliToLocalDateTime(long milli) {
        return Instant.ofEpochMilli(milli).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
    }

    public static LocalDate dateToLocalDate(Date date) {
        return date.toInstant().atZone(ZoneOffset.ofHours(8)).toLocalDate();
    }

    public static LocalDateTime dateToLocalDateTime(Date date) {
        return date.toInstant().atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
    }

    public static String format(TemporalAccessor temporal, DateTimeFormatter formatter) {
        return formatter.format(temporal);
    }

    public static String formatLocalDate(LocalDate localDate) {
        return FORMAT_DATE.format(localDate);
    }

    public static String formatLocalDateTime(LocalDateTime localDateTime) {
        return FORMAT_DATETIME.format(localDateTime);
    }

    /**
     * 2个日期的时间间隔
     *
     * @param beginDate
     * @param endDate
     * @return
     */
    public static long intervalOfDays(LocalDate beginDate, LocalDate endDate) {
        return endDate.toEpochDay() - beginDate.toEpochDay();
    }

    /**
     * 2个日期的时间间隔
     *
     * @param beginDate
     * @param endDate
     * @return
     */
    public static long intervalOfHour(LocalDateTime beginDate, LocalDateTime endDate) {
        return ChronoUnit.HOURS.between(beginDate, endDate);
    }

    /**
     * 2个日期的时间间隔
     *
     * @param beginDate
     * @param endDate
     * @return
     */
    public static long intervalOfMinute(LocalDateTime beginDate, LocalDateTime endDate) {
        return ChronoUnit.MINUTES.between(beginDate, endDate);
    }

    public static List<LocalDate> splitByWeek(LocalDate startLocalDate, LocalDate endLocalDate) {
        if (startLocalDate.compareTo(endLocalDate) > 0) {
            throw new RuntimeException("startLocalDate must be less than endLocalDate");
        }
        List<LocalDate> localDates = new ArrayList<>();
        LocalDate lastMonday = startLocalDate.with(DayOfWeek.MONDAY).plusDays(7);// 下周一
        do {
            localDates.add(startLocalDate);
            startLocalDate = lastMonday;
            lastMonday = lastMonday.plusDays(7);
        } while (startLocalDate.compareTo(endLocalDate) < 0);
        localDates.add(endLocalDate);
        return localDates;
    }

    /**
     * 将时间段按照月份切割
     * <p>
     * ex:2020-01-30  ~  2020-01-30   =>   ["2020-01-30","2020-01-30"]
     * ex:2020-01-30  ~  2020-02-01   =>   ["2020-01-30","2020-02-01"]
     * ex:2020-01-30  ~  2020-03-03   =>   ["2020-01-30","2020-02-01","2020-03-01","2020-03-03"]
     *
     * @param startLocalDate
     * @param endLocalDate
     * @return
     */
    public static List<LocalDate> splitByMonth(LocalDate startLocalDate, LocalDate endLocalDate) {
        if (startLocalDate.compareTo(endLocalDate) > 0) {
            throw new RuntimeException("startLocalDate must be less than endLocalDate");
        }
        List<LocalDate> localDates = new ArrayList<>();
        LocalDate lastMonth = startLocalDate.withDayOfMonth(1).plusMonths(1);// 下月 1号
        do {
            localDates.add(startLocalDate);
            startLocalDate = lastMonth;
            lastMonth = startLocalDate.plusMonths(1);
        } while (startLocalDate.compareTo(endLocalDate) < 0);
        localDates.add(endLocalDate);
        return localDates;
    }

    /**
     * 将时间段按天切割
     *
     * @param startLocalDate
     * @param endLocalDate
     * @return
     */
    public static List<LocalDate> splitByDay(LocalDate startLocalDate, LocalDate endLocalDate) {
        if (startLocalDate.compareTo(endLocalDate) > 0) {
            throw new RuntimeException("startLocalDate must be less than endLocalDate");
        }
        List<LocalDate> localDates = new ArrayList<>();
        long day = endLocalDate.toEpochDay() - startLocalDate.toEpochDay();
        for (int i = 0; i <= day; i++) {
            localDates.add(startLocalDate.plusDays(i));
        }
        return localDates;
    }

    /**
     * 获取指定年月的天数
     *
     * @param year
     * @param month
     * @return
     */
    public static long daysOfYearMonth(int year, int month) {
        LocalDate start = LocalDate.of(year, month, 1);
        if (month == 12) {
            year += 1;
            month = 1;
        } else {
            month += 1;
        }
        LocalDate end = LocalDate.of(year, month, 1);
        return intervalOfDays(start, end);
    }

    public String localDateFormat(LocalDate localDate, String formatStr) {
        return localDate.format(DateTimeFormatter.ofPattern(formatStr));
    }

    public String localDateFormat(LocalDate localDate, DateTimeFormatter formatter) {
        return localDate.format(formatter);
    }

    /**
     * 获取当前日期, 默认格式为yyyy-MM-dd
     *
     * @return String
     */
    public static String getDate() {
        return dateTimeNow(YYYY_MM_DD);
    }

    public static final String getTime() {
        return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
    }

    public static final String dateTimeNow() {
        return dateTimeNow(YYYYMMDDHHMMSS);
    }

    public static final String dateTimeNow(final String format) {
        return parseDateToStr(format, new Date());
    }

    public static final String dateTime(final Date date) {
        return parseDateToStr(YYYY_MM_DD, date);
    }

    public static final String parseDateToStr(final String format, final Date date) {
        return new SimpleDateFormat(format).format(date);
    }

    public static final Date dateTime(final String format, final String ts) {
        try {
            return new SimpleDateFormat(format).parse(ts);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 日期路径 即年/月/日 如2018/08/08
     */
    public static final String datePath() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyy/MM/dd");
    }

    /**
     * 日期路径 即年/月/日 如20180808
     */
    public static final String dateTime() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyyMMdd");
    }

    /**
     * 日期型字符串转化为日期 格式
     */
    public static Date parseDate(Object str) {
        if (str == null) {
            return null;
        }
        try {
            return parseDate(str.toString(), parsePatterns);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 获取服务器启动时间
     */
    public static Date getServerStartDate() {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

    /**
     * 计算两个时间差
     */
    public static String getDatePoor(Date endDate, Date nowDate) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return day + "天" + hour + "小时" + min + "分钟";
    }

    /**
     * String转换成LocalDate,标准时间字符串
     *
     * @param date "2021-01-01"
     * @return
     */
    public static LocalDateTime string2LocalDate(String date) {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return LocalDateTime.parse(date, fmt);
    }

    /**
     * 特殊日期字符串处理
     *
     * @param dateTime “2021-01-01T00:00:00+08:00”
     * @return
     */
    public static LocalDateTime string2LocalDateTime(String dateTime) throws ParseException {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        Date date = df.parse(dateTime);
        SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK);
        date = sdf.parse(date.toString());
        df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return string2LocalDate(df.format(date));
    }

    /**
     * 获得某天最小时间 2020-08-19 00:00:00
     *
     * @param oneDayTime : 某天的时间
     * @return : 返回某天零点时间13位时间戳
     */
    public static long getStartOfDay(Long oneDayTime) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(oneDayTime), ZoneId.systemDefault());
        LocalDateTime startOfDay = localDateTime.with(LocalTime.MIN);
        return Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant()).getTime();
    }

    /**
     * 获得某天最大时间 2021-08-19 23:59:59
     *
     * @param oneDayTime : 某天的时间
     * @return : 返回某天23:59:59的13位时间戳
     */
    public static long getEndOfDay(Long oneDayTime) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(oneDayTime), ZoneId.systemDefault());
        LocalDateTime endOfDay = localDateTime.with(LocalTime.MAX);
        return Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant()).getTime();
    }
}