package com.cool.store.utils;

import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.util.Calendar;
import java.util.Date;

/**
 * @Author suzhuhong
 * @Date 2023/6/8 17:17
 * @Version 1.0
 */
@Slf4j
public class CoolDateUtils {

    public static final String DATE_FORMAT_DAY = "yyyy-MM-dd";
    public static final String DATE_FORMAT_DAY_2 = "yyyy/MM/dd";
    public static final String TIME_FORMAT_SEC = "HH:mm:ss";
    public static final String TIME_FORMAT_SEC2 = "HH:mm";
    public static final String DATE_FORMAT_SEC = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_FORMAT_SEC1 = "yyyy-MM-dd HH:mm";
    public static final String DATE_FORMAT_SEC_2 = "yyyy/MM/dd HH:mm:ss";
    public static final String DATE_FORMAT_SEC_3 = "yyyy.MM.dd HH:mm:ss";
    public static final String DATE_FORMAT_SEC_4 = "yyyy.MM.dd HH:mm";
    public static final String DATE_FORMAT_SEC_5 = "yyyy.MM.dd HH:mm";
    public static final String DATE_FORMAT_SEC_6 = "yyyy.MM.dd";
    public static final String DATE_FORMAT_SEC_7 = "yyyy/MM/dd HH:mm";
    /**
     * 几天后的当前
     * @param d
     * @param day
     * @return
     */
    public static Date getDateBefore(Date d, int day) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
        return now.getTime();
    }

    /**
     * n天后 的最小时间    20230613 2天后最小时间---->2023-06-15 00:00:00
     * @param day
     * @return
     */
    public static Date getDateFormatDayMinTime(Date date,int day){
        LocalDate today = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        LocalDate thirdDay = today.plusDays(day);
        LocalDateTime thirdDayMidnight = LocalDateTime.of(thirdDay, LocalTime.MIDNIGHT);
        LocalDateTime thirdDay235959 = thirdDayMidnight.with(LocalTime.MIN);
        ZonedDateTime zonedDateTime = ZonedDateTime.of(thirdDay235959, ZoneId.systemDefault());
        return  Date.from(zonedDateTime.toInstant());
    }

    public static void main(String[] args) {
        // 创建一个Date对象
        Date date = new Date();

        // 将Date对象转换为LocalDate对象
        LocalDate today = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

        System.out.println("LocalDate: " + today);
    }

    /**
     * 时间格式转换
     * @param newFormat
     * @param oldFormat
     * @param data
     * @return
     */
    public static String parseStrToStr(String newFormat,String oldFormat,String data){
        if (StringUtils.isEmpty(data)){
            return null;
        }
        Date date = null;
        try {
            date = DateUtils.parseDate(data, oldFormat);
        } catch (ParseException e) {
            log.error("时间转换异常 {}",e);
        }
        return  DateUtil.format(date,newFormat);
    }


    /**
     * n天后 的最大时间    20230613 2天后最大时间---->2023-06-15 23:59:59
     * @param day
     * @return
     */
    public static Date getDateFormatDay(Date date,int day){
        LocalDate today = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        LocalDate thirdDay = today.plusDays(day);
        LocalDateTime thirdDayMidnight = LocalDateTime.of(thirdDay, LocalTime.MIDNIGHT);
        LocalDateTime thirdDay235959 = thirdDayMidnight.with(LocalTime.MAX);
        ZonedDateTime zonedDateTime = ZonedDateTime.of(thirdDay235959, ZoneId.systemDefault());
        return  Date.from(zonedDateTime.toInstant());
    }


    public static final Date parseDate(String dateString ,String format){
        if (StringUtils.isBlank(dateString)){
            return null;
        }
        DateFormat dateFormat = new SimpleDateFormat(format);
        Date date = null;
        try {
            date = dateFormat.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
    public static final String DateFormat(Date date,String format){
        if (StringUtils.isBlank(format)){
            return null;
        }
        DateFormat dateFormat = new SimpleDateFormat(format);
        String str = dateFormat.format(date);
        return str;
    }


}
