package com.ai.da.common.utils;

import com.ai.da.common.constant.CommonConstant;
import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

@Slf4j
public class DateUtil {
    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public static final String YYYYMM = "yyyyMM";
    public static final String YYYY_MM_DD = "yyyyMMdd";
    public static final String YYYY_MM_DD_HH = "yyyyMMddHH";
    public static final String YYYY_MM_DD_hh_mm_ss = "yyyyMMddHHMMss";

    /**
     * LocalDate -> Date
     */
    public static Date asDate(LocalDate localDate) {
        return Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * LocalDateTime -> Date
     */
    public static Date asDate(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * date 装 String
     *
     * @param date
     * @param formatter
     * @return
     */
    public static String dateToStr(Date date, String formatter) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(formatter);
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zoneId);
        return dateTimeFormatter.format(localDateTime);
    }

    /**
     * 根据时区获取时间
     *
     * @param timeZone "Asia/Tokyo"
     * @return
     */
    public static Date getByTimeZone(String timeZone) {
        String dateStr = dateToStr(new Date(), YYYY_MM_DD_HH_MM_SS);
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        // 设置时区
        sdf.setTimeZone(TimeZone.getTimeZone(timeZone));
        Date date = null;
        try {
            date = sdf.parse(dateStr);
        } catch (ParseException parseException) {
            log.error("时间转换异常!", parseException);
        }
        return date;
    }

    /**
     * 获取指定时区的时间戳的前十位
     * @param timeZone 时区
     * @return 当前时间戳的前十位
     */
    public static String getTimeStamp(String timeZone){
        ZoneId zoneId = ZoneId.of(timeZone);

        long epochSecond = Instant.now().atZone(zoneId).toEpochSecond();

        return String.valueOf(epochSecond).substring(0, 10);
    }

    public static String changeTimeStampFormat(Long timeStamp, String type, String format){
        // 将秒级时间戳转换为毫秒级
        if (type.equals("seconds")){
            timeStamp = timeStamp * 1000;
        }
        // 输出格式
        SimpleDateFormat outputFormat = new SimpleDateFormat(format, Locale.ENGLISH);
        // 创建Date对象
        Date date = new Date(timeStamp);
        // 格式化输出
        return outputFormat.format(date);
    }

    public static String changeTimeStampFormat(LocalDateTime localDate){
        return localDate.format(DateTimeFormatter.ofPattern(CommonConstant.TIME_FORMAT_MMM_dd_yyyy_EEEE, Locale.US));
    }

    public static boolean isMoreThanOneDayApart(LocalDateTime givenDateTime) {
        LocalDateTime now = LocalDateTime.now();
        Duration duration = Duration.between(givenDateTime, now);
        return duration.toHours() >= 24;
    }

}
