package cn.exploring.engine.server.db.util;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

public class DateTimeUtil {
    public static final String DATE_FORMAT1 = "yyyy-MM-dd";
    public static final String DATE_FORMAT2 = "yyyy-MM";

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

    public static final DateTimeFormatter DATE_TIME_FORMATTER3 = DateTimeFormatter.ofPattern(DATE_FORMAT3);

    /**
     * 得到DATE_FORMAT1格式的时间字段串
     * @param time
     * @return
     */
    public static String getDateStrFormat1(LocalDateTime time) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(DATE_FORMAT1);
        return dtf.format(time);
    }

    /**
     * 获取两个日期的差  field参数为ChronoUnit.*
     *
     * @param startTime
     * @param endTime
     * @param field     单位(年月日时分秒)
     * @return
     */
    public static long betweenTwoTime(LocalDateTime startTime, LocalDateTime endTime, ChronoUnit field) {
        Period period = Period.between(LocalDate.from(startTime), LocalDate.from(endTime));
        if (field == ChronoUnit.YEARS) return period.getYears();
        if (field == ChronoUnit.MONTHS) return period.getYears() * 12 + period.getMonths();
        return field.between(startTime, endTime);
    }

    public static void main(String[] args) {
        LocalDateTime ss = LocalDateTime.now();
        LocalDateTime fs = ss.plusDays(10);

        int s = dayPassed(fs);

        System.out.println(fs);
        System.out.println(s);
    }

    /**
     * 获取time跟当前时间相比是几天前
     *
     * @param time
     * @return
     */
    public static int dayPassed(LocalDateTime time) {
        return Period.between(LocalDate.from(time), LocalDate.now()).getDays();
    }

    /**
     * 获取time跟time1
     *
     * @param startTime 和 endTime 之间的差几天
     * @return
     */
    public static int dayPassed(LocalDateTime startTime, LocalDateTime endTime) {
        return Period.between(LocalDate.from(startTime), LocalDate.from(endTime)).getDays();
    }


    public static int checkRaceStarted() {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return DateTimeUtil.dayPassed(LocalDateTime.parse("2020-05-01 00:00:00", df));
    }

    //获取一天的开始时间，2017,7,22 00:00
    public static LocalDateTime getDayStart(LocalDateTime time) {
        if (time == null) {
            time = LocalDateTime.now();
        }
        return time.withHour(0)
                .withMinute(0)
                .withSecond(0)
                .withNano(0);
    }

    //获取一天的结束时间，2017,7,22 23:59:59.999999999
    public static LocalDateTime getDayEnd(LocalDateTime time) {
        if (time == null) {
            time = LocalDateTime.now();
        }
        return time.withHour(23)
                .withMinute(59)
                .withSecond(59)
                .withNano(999999999);
    }

    public static Boolean isNextDay(LocalDateTime previousDate, LocalDateTime lastDate) {
        return (previousDate.toLocalDate().plusDays(1).equals(lastDate.toLocalDate()));
    }

    public static Boolean isSameDay(LocalDateTime previousDate, LocalDateTime lastDate) {
        return (previousDate.toLocalDate().equals(lastDate.toLocalDate()));
    }

    public static Boolean isSameDayAndHour(LocalDateTime previousDate, LocalDateTime lastDate) {
        return (previousDate.toLocalDate().equals(lastDate.toLocalDate())) && (previousDate.getHour() == lastDate.getHour());
    }

    /**
     * 获取输入时间所在周的周一的凌晨时间
     *
     * @param dateTime
     * @return
     */
    public static LocalDateTime getWeeklyStartDate(LocalDateTime dateTime) {
        if (dateTime == null) {
            dateTime = LocalDateTime.now();
        }
        Integer diffDays = dateTime.getDayOfWeek().getValue() - 1;
        return getDayStart(dateTime.plusDays(-diffDays));
    }

    /**
     * 获取输入时间所在周周末的最后一秒时间
     *
     * @param dateTime
     * @return
     */
    public static LocalDateTime getWeeklyEndDate(LocalDateTime dateTime) {
        if (dateTime == null) {
            dateTime = LocalDateTime.now();
        }
        Integer diffDays = 7 - dateTime.getDayOfWeek().getValue();
        return getDayEnd(dateTime.plusDays(diffDays));
    }

    /**
     * 获取输入时间所在月的月初的凌晨
     *
     * @param dateTime
     * @return
     */
    public static LocalDateTime getMonthlyStartDate(LocalDateTime dateTime) {
        if (dateTime == null) {
            dateTime = LocalDateTime.now();
        }
        LocalDate monthSatrtDate = dateTime.toLocalDate().withDayOfMonth(1);
        return LocalDateTime.of(monthSatrtDate, LocalTime.MIN);
    }


    /**
     * 返回输入时间所在月的月末的最后一秒时刻
     *
     * @param dateTime
     * @return
     */
    public static LocalDateTime getMonthlyEndDate(LocalDateTime dateTime) {
        if (dateTime == null) {
            dateTime = LocalDateTime.now();
        }
        LocalDate monthEndDate = dateTime.plusMonths(1).toLocalDate().withDayOfMonth(1);
        return LocalDateTime.of(monthEndDate.plusDays(-1), LocalTime.MAX);
    }

}
