package com.sonic.lion.utils;

import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.zone.ZoneRules;
import java.util.Calendar;
import java.util.Date;

/**
 * @Author code
 * @Date 2025/8/11 17:30
 * @Version 1.0
 */
@Slf4j
public class DateConvertUtils {

    public static String getNow(String pattern) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
        return dateTimeFormatter.format(LocalDateTime.now());
    }

    public static String format(LocalDateTime localDateTime, String pattern) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
        String date = dateTimeFormatter.format(localDateTime.minusHours(getLaHours()));
        return date;
    }

    public static String format(LocalDateTime localDateTime) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String date = dateTimeFormatter.format(localDateTime.minusHours(getLaHours()));
        return date;
    }

    public static String format() {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String date = dateTimeFormatter.format(ZonedDateTime.now(ZoneId.of("America/Los_Angeles")).toLocalDateTime());
        return date;
    }

    public static Integer formatToInt(LocalDateTime localDateTime) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        String date = dateTimeFormatter.format(localDateTime.minusHours(getLaHours()));
        return Integer.valueOf(date);
    }


    public static Integer formatYearMonthToInt(LocalDateTime localDateTime) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMM");
        String date = dateTimeFormatter.format(localDateTime.minusHours(getLaHours()));
        return Integer.valueOf(date);
    }

    public static Integer formatToInt(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        return Integer.valueOf(simpleDateFormat.format(date));
    }

    public static Date getYesterdayDate(){
        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.DATE,-1);
        Date d=cal.getTime();
        return d;
    }

    /**
     * 获取洛杉矶 夏令时、冬令时 和北京时间的小时差
     * @return
     */
    public static int getLaHours() {
        boolean bl = false;
        try {
            bl = isDaylightTime(LocalDateTime.now(), ZoneId.of(ZoneId.SHORT_IDS.get("PST")));
        } catch (Exception e) {
            log.info("处理异常情况 : {}", e);
        }
        int hours = bl ? 15 : 16;
        return hours;
    }

    /**
     * 传入指定时间和时区
     * @param localDateTime
     * @param zoneId
     * @return true 表示是夏令时，false表示是冬令时
     */
    public static boolean isDaylightTime(LocalDateTime localDateTime, ZoneId zoneId) {
        ZonedDateTime zonedDateTime = localDateTime.atZone(zoneId);
        ZoneRules rules = zoneId.getRules();
        boolean flag = rules.isDaylightSavings(zonedDateTime.toInstant());
        return flag;
    }

    /**
     * 根据时间戳获取时间对象
     * @param timestamp
     * @return
     */
    public static LocalDateTime getDate(Long timestamp) {
        if(timestamp == null) {
            return null;
        }
        Instant instant = Instant.ofEpochSecond(timestamp);
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        return localDateTime;
    }

}
