package com.ruoyi.zhushi.util;

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

public class TimeUtils {

    private static final DateTimeFormatter FORMAT_DEFAULT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    public static String getStartTime(String dayDate) {
        // 定义一天的开始和结束时间
        LocalDate date = LocalDate.parse(dayDate, DateTimeFormatter.ISO_LOCAL_DATE);
        LocalTime startTime = LocalTime.of(0, 0, 0); // 00:00:00
        LocalDateTime startOfDay = date.atTime(startTime);
        // 格式化输出
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String startOfDayStr = startOfDay.format(formatter);
        return startOfDayStr;
    }

    public static String  getEndTime(String dayDate) {
        LocalDate today = LocalDate.now();
        // 定义一天的开始和结束时间
        LocalDate date = LocalDate.parse(dayDate, DateTimeFormatter.ISO_LOCAL_DATE);
        LocalTime endTime = LocalTime.of(23, 59, 59); // 23:59:59
        LocalDateTime endOfDay = date.atTime(endTime);
        // 格式化输出
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String endOfDayStr  = endOfDay.format(formatter);
        return endOfDayStr ;
    }

    // 获取当月开始时间
    public static Date getMonthStart() {
        LocalDateTime startOfMonth = LocalDateTime.now()
            .withDayOfMonth(1)
            .with(LocalTime.MIN);

        return Date.from(startOfMonth.atZone(ZoneId.systemDefault()).toInstant());
    }

    // 获取当月结束时间
    public static Date getMonthEnd() {
        LocalDateTime endOfMonth = LocalDateTime.now()
            .plusMonths(1)
            .withDayOfMonth(1)
            .with(LocalTime.MIN)
            .minusSeconds(1);

        return Date.from(endOfMonth.atZone(ZoneId.systemDefault()).toInstant());
    }

    // 格式化日期
    private static String formatDate(Date date, DateTimeFormatter formatter) {
        LocalDateTime localDateTime = date.toInstant()
            .atZone(ZoneId.systemDefault())
            .toLocalDateTime();

        return localDateTime.format(formatter);
    }

    public static LocalDateTime getFirstDayOfMonth(String monthDate) {
//        Date monthStart = getMonthStart();
//        // 格式化输出
//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//        return formatDate(monthStart, formatter);
//        LocalDateTime startOfMonth = monthDate.atStartOfDay(); // 2025-06-01T00:00
//        monthDate.atStartOfDay();
//        LocalDate date = LocalDate.parse(monthDate, DateTimeFormatter.ISO_LOCAL_DATE);
        YearMonth yearMonth = YearMonth.parse(monthDate);
        LocalDate localDate = yearMonth.atDay(1);
        int year = localDate.getYear();
        int month = localDate.getMonthValue();
//        LocalDateTime startOfMonth = date.atTime(LocalTime.MIN);
        return YearMonth.of(year, month).atDay(1).atStartOfDay();
    }

    public static LocalDateTime getLastDayOfMonth(String monthDate) {
//        Date monthEnd = getMonthEnd();
//        // 格式化输出
//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//        return formatDate(monthEnd, formatter);
        YearMonth yearMonth = YearMonth.parse(monthDate);
        LocalDate localDate = yearMonth.atEndOfMonth();
        int year = localDate.getYear();
        int month = localDate.getMonthValue();
        return YearMonth.of(year, month).atEndOfMonth().atTime(LocalTime.MAX);
    }

    public static LocalDateTime parseStr2LocalDateTime(String timeStr) {
        return LocalDateTime.parse(timeStr, FORMAT_DEFAULT);
    }
}
