package com.huiju.estateagents.common;

import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class DateUtils {
    public static LocalDateTime date2LocalDateTime(Date date) {
        Instant instant = date.toInstant();
        ZoneId zone = ZoneId.systemDefault();
        return LocalDateTime.ofInstant(instant, zone);
    }

    public static boolean sameDay(LocalDateTime d1, LocalDateTime d2) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        return d1.format(formatter).equals(d2.format(formatter));
    }

    public static String cutSecond(LocalDateTime dt) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        return dt.format(formatter);
    }

    public static String today() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        return LocalDateTime.now().format(formatter);
    }

    public static String todayCompact() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        return LocalDateTime.now().format(formatter);
    }

    public static String localDateTimeToString(LocalDateTime d) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return d.format(formatter);
    }

    public static LocalDateTime day2LocalDateime(String day) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return LocalDateTime.parse(day + " 00:00:00", df);
    }

    public static LocalDateTime dayToLocalDateime(String day) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return LocalDateTime.parse(day, df);
    }

    public static boolean checkTimeRange(LocalDateTime start, LocalDateTime end, LocalDateTime dt) {
        if (dt == null) {
            dt = LocalDateTime.now();
        }

        if (dt.isAfter(start) && dt.isBefore(end)) {
            return true;
        }
        return false;
    }

    /**
     * 当前时间是否早于指定时间
     *
     * @param target
     * @return
     */
    public static boolean isBefore(String target) {
        LocalDateTime dt = LocalDateTime.now();
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime targetTime = LocalDateTime.parse(target, df);
        if (dt.isBefore(targetTime)) {
            return true;
        }
        return false;
    }

    /**
     * 当前时间是否晚于指定时间
     *
     * @param target
     * @return
     */
    public static boolean isAfter(String target) {
        LocalDateTime dt = LocalDateTime.now();
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime targetTime = LocalDateTime.parse(target, df);
        if (dt.isAfter(targetTime)) {
            return true;
        }
        return false;
    }

    /**
     * 获取时间差
     *
     * @param dt1
     * @param dt2 目标时间
     * @return
     */
    public static String diffTime(LocalDateTime dt1, LocalDateTime dt2) {
        Duration duration = Duration.between(dt1, dt2);
        long minutes = duration.toMinutes();
        int hours = (int) Math.floor(minutes / 60);
        int minute = (int) minutes % 60;
        int day = (int) Math.floor(hours / 24);
        int hour = hours % 24;

        return day + "天" + hour + "小时" + minute + "分钟";
    }

    public static LocalDateTime timeToLocalDateTime(String target){
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return LocalDateTime.parse(target, df);
    }
}
