package com.hotwj.platform.common.utils;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;

/**
 * 时间范围工具类
 */
public class TimeRangeUtil {

    public static class TimeRange {
        private final Date startTime;
        private final Date endTime;

        public TimeRange(Date startTime, Date endTime) {
            this.startTime = startTime;
            this.endTime = endTime;
        }

        public Date getStartTime() {
            return startTime;
        }

        public Date getEndTime() {
            return endTime;
        }
    }

    public static TimeRange getCurrentWeek() {
        LocalDate today = LocalDate.now();
        LocalDate startOfWeek = today.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
        LocalDate endOfWeek = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
        return new TimeRange(toDate(startOfWeek), toDate(endOfWeek));
    }

    public static TimeRange getCurrentMonth() {
        LocalDate today = LocalDate.now();
        LocalDate startOfMonth = today.with(TemporalAdjusters.firstDayOfMonth());
        LocalDate endOfMonth = today.with(TemporalAdjusters.lastDayOfMonth());
        return new TimeRange(toDate(startOfMonth), toDate(endOfMonth));
    }

    public static TimeRange getCurrentQuarter() {
        LocalDate today = LocalDate.now();
        int month = today.getMonthValue();
        int quarterStartMonth = month - (month - 1) % 3;
        LocalDate startOfQuarter = today.withMonth(quarterStartMonth).with(TemporalAdjusters.firstDayOfMonth());
        LocalDate endOfQuarter = startOfQuarter.plusMonths(2).with(TemporalAdjusters.lastDayOfMonth());
        return new TimeRange(toDate(startOfQuarter), toDate(endOfQuarter));
    }

    public static TimeRange getCurrentHalfYear() {
        LocalDate today = LocalDate.now();
        int month = today.getMonthValue();
        LocalDate startOfHalfYear;
        LocalDate endOfHalfYear;
        if (month <= 6) {
            startOfHalfYear = LocalDate.of(today.getYear(), 1, 1);
            endOfHalfYear = LocalDate.of(today.getYear(), 6, 30);
        } else {
            startOfHalfYear = LocalDate.of(today.getYear(), 7, 1);
            endOfHalfYear = LocalDate.of(today.getYear(), 12, 31);
        }
        return new TimeRange(toDate(startOfHalfYear), toDate(endOfHalfYear));
    }

    public static TimeRange getCurrentYear() {
        LocalDate today = LocalDate.now();
        LocalDate startOfYear = today.with(TemporalAdjusters.firstDayOfYear());
        LocalDate endOfYear = today.with(TemporalAdjusters.lastDayOfYear());
        return new TimeRange(toDate(startOfYear), toDate(endOfYear));
    }

    public static int getCurrentQuarterNumber() {
        return (LocalDate.now().getMonthValue() - 1) / 3 + 1;
    }

    public static String getCurrentHalfYearDescription() {
        return LocalDate.now().getMonthValue() <= 6 ? "上半年" : "下半年";
    }

    public static String formatDate(Date date, String pattern) {
        if (date == null) return "";
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate().format(DateTimeFormatter.ofPattern(pattern));
    }

    private static Date toDate(LocalDate localDate) {
        return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
    }
}
