package com.casic.missiles.utils;

import cn.hutool.core.date.DateUtil;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Description: 日期辅助工具类
 * @Author: wangpeng
 * @Date: 2023/6/14 10:25
 */
public class DateUtils {
    private static final String DATE_PATTERN = "yyyy-MM-dd";
    private static final String DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

    /**
     * 获取两个日期相差的天数
     * @param startDate
     * @param endDate
     * @return
     */
    public static long subDaysByDateTime(String startDate, String endDate) {
        long sub;
        try {
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DATETIME_PATTERN);
            LocalDateTime start = LocalDateTime.parse(startDate, dateTimeFormatter);
            LocalDateTime end = LocalDateTime.parse(endDate, dateTimeFormatter);
            sub = Duration.between(start, end).toDays();
        } catch (DateTimeParseException e) {
            sub = -1;
        }
        return sub;
    }

    /**
     * 获取两个日期相差的天数
     * @param startDate
     * @param endDate
     * @return
     */
    public static long subDaysByDate(String startDate, String endDate) {
        long sub;
        try {
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DATE_PATTERN);
            LocalDate start = LocalDate.parse(startDate, dateTimeFormatter);
            LocalDate end = LocalDate.parse(endDate, dateTimeFormatter);
            sub = Duration.between(LocalDateTime.of(start, LocalTime.of(0,0,0)),
                    LocalDateTime.of(end, LocalTime.of(0,0,0))).toDays();
        } catch (DateTimeParseException e) {
            sub = -1;
        }
        return sub;
    }

    /**
     * 今年每月
     * @return
     */
    public static List<String> getCurrentDateMonthList() {
        List<String> list = new ArrayList<>();
        int year = cn.hutool.core.date.DateUtil.thisYear();
        for (int i = 1; i < 13; i++) {
            if (i < 10) {
                list.add("" + year + "-0" + i);
            } else {
                list.add("" + year + "-" + i);
            }
        }
        return list;
    }

    /**
     * 本月每天
     * @return
     */
    public static List<String> getDayListOfMonth(String yearMonth) {
        if (yearMonth.length() != 7) {
            return null;
        }

        List<String> list = new ArrayList<String>();
        String ystr = yearMonth.substring(0, 4);
        String mstr = yearMonth.substring(5,7);

        Calendar a = Calendar.getInstance();
        a.set(Calendar.YEAR, Integer.parseInt(ystr));//年份
        a.set(Calendar.MONTH,Integer.parseInt(mstr) - 1);//月份
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1);

        int maxDate = a.get(Calendar.DATE);

        for (int i = 0; i < maxDate; i++) {
            int d = i+1;
            String dstr = "";
            if (d < 10) {
                dstr = "0"+String.valueOf(d);
            }else {
                dstr = String.valueOf(d);
            }
            String day = ystr + "-" + mstr + "-" + dstr;
            list.add(day);
        }
        return list;
    }

    /**
     * 本月1日0点
     */
    public static String getThisMonthFirst() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, 1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        Date m = c.getTime();
        String startTime = DateUtil.format(m, "yyyy-MM-dd HH:mm:ss");
        return startTime;
    }

    /**
     * 上月1日0点
     */
    public static String getLastMonthFirst() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, -1);
        c.set(Calendar.DAY_OF_MONTH, 1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        Date m = c.getTime();
        String startTime = DateUtil.format(m, "yyyy-MM-dd HH:mm:ss");
        return startTime;
    }

    /**
     * 去年同月一日0点
     */
    public static String getLastYearThisMonthFirst() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.YEAR, -1);
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, 1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        Date m = c.getTime();
        String startTime = DateUtil.format(m, "yyyy-MM-dd HH:mm:ss");
        return startTime;
    }

    /**
     * 去年同月最后一日12点
     */
    public static String getLastYearThisMonthEnd() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.YEAR, -1);
        //去年相对今年本月的下一个月的第一天的0点
        c.add(Calendar.MONTH, 1);
        c.set(Calendar.DAY_OF_MONTH, 1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        Date m = c.getTime();
        String startTime = DateUtil.format(m, "yyyy-MM-dd HH:mm:ss");
        return startTime;
    }
}
