package com.tzld.piaoquan.growth.common.utils;


import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.*;

public class DateUtil {

    public static String getCurrentDateStr(String format) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        Date currentDate = new Date();
        return dateFormat.format(currentDate);
    }

    //获取当天的日期
    public static Date getThatDayDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }
    public static Date getDaysAgoDate(int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_YEAR, -day);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    public static String getBeforeDayDateString(String format) {
        DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern(format);
        LocalDate today = LocalDate.now();
        LocalDate yesterday = today.minusDays(1);
        return dateFormat.format(yesterday);
    }

    public static String getBeforeDayDateString(int day, String format) {
        DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern(format);
        LocalDate today = LocalDate.now();
        LocalDate yesterday = today.minusDays(day);
        return dateFormat.format(yesterday);
    }

    public static String getBeforeDayDateString1() {
        DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate today = LocalDate.now();
        LocalDate yesterday = today.minusDays(1);
        return dateFormat.format(yesterday);
    }

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

    public static String getNextDayDateString() {
        DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate tomorrow = LocalDate.now().minusDays(-1);
        return dateFormat.format(tomorrow);
    }


    public static String getDayDateString(String pattern) {
        DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern(pattern);
        LocalDate today = LocalDate.now();
        return dateFormat.format(today);
    }

    public static String getDateString(Long timestamp) {
        // 创建日期时间格式化器
        DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 将时间戳转换为 LocalDateTime
        LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault());
        // 格式化日期时间并返回
        return dateTime.format(dateFormat);
    }

    public static String getDateString(Long timestamp, String pattern) {
        // 创建日期时间格式化器
        DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern(pattern);
        // 将时间戳转换为 LocalDateTime
        ZoneId zone = ZoneId.of("Asia/Shanghai");
        LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), zone);
        // 格式化日期时间并返回
        return dateTime.format(dateFormat);
    }

    public static Long dateDifference(Date date1, Date date2) {
        return Math.abs(date1.getTime() - date2.getTime());
    }

    public static Integer getHourOfDay() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.HOUR_OF_DAY);
    }

    public static boolean isValidDate(String dateString) {
        // 定义日期格式
        DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        try {
            // 尝试解析日期字符串
            LocalDate date = LocalDate.parse(dateString, dateFormatter);
            return true; // 如果解析成功，返回 true
        } catch (DateTimeParseException e) {
            return false; // 如果解析失败，返回 false
        }
    }

    public static long dateStrToTimestamp(String dateStr, String format) {
        // 定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);

        // 解析输入的日期字符串为LocalDate对象
        LocalDate date = LocalDate.parse(dateStr, formatter);

        // 将LocalDate转换为LocalDateTime，时间部分设为00:00:00
        LocalDateTime dateTime = date.atStartOfDay();

        // 定义北京时区
        ZoneId zone = ZoneId.of("Asia/Shanghai");

        // 将LocalDateTime转换为时间戳（秒数），使用北京时区
        return dateTime.toEpochSecond(zone.getRules().getOffset(dateTime));
    }

    public static Long getBeforeDayStart(int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.add(Calendar.DAY_OF_MONTH, -days);
        return calendar.getTime().getTime() / 1000;
    }

    public static Date getTodayStartDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    public static Date getDate(String dateString) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate localDate = LocalDate.parse(dateString, formatter);
        ZoneId zone = ZoneId.of("Asia/Shanghai");
        return Date.from(localDate.atStartOfDay(zone).toInstant());
    }

    public static Date getDatePlusDays(String dateString, int plusDays) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate localDate = LocalDate.parse(dateString, formatter);
        ZoneId zone = ZoneId.of("Asia/Shanghai");
        localDate = localDate.plusDays(plusDays);
        return Date.from(localDate.atStartOfDay(zone).toInstant());
    }

    public static List<String> getBeforeDays(int days) {
        return getBeforeDays(null, null, days);
    }

    public static List<String> getBeforeDays(String beginDate, String endDate, int days) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        // 获取今天的日期
        LocalDate today = LocalDate.now();
        if (!StringUtils.hasText(beginDate)) {
            beginDate = today.format(formatter);
        }
        if (!StringUtils.hasText(endDate)) {
            endDate = today.format(formatter);
        }
        // 解析输入的日期字符串为LocalDate对象
        LocalDate date = LocalDate.parse(beginDate, formatter);
        LocalDate end = LocalDate.parse(endDate, formatter);
        // 获取从输入日期前三天的日期
        LocalDate startDate = date.minusDays(days);
        // 存储所有日期的列表
        List<String> datesList = new ArrayList<>();
        // 从startDate到today遍历日期
        while (startDate.isBefore(today) && !startDate.isAfter(end)) {
            // 将当前日期格式化为"yyyyMMdd"并添加到列表中
            datesList.add(startDate.format(formatter));
            // 日期加1天
            startDate = startDate.plusDays(1);
        }
        return datesList;
    }

}
