package com.lutao.cloud.oil.util;

import org.springframework.stereotype.Component;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAdjusters;

@Component
public class DateUtils {

    public static LocalDateTime getStartPeriod(String cycleType) {
        LocalDate now = LocalDate.now();
        LocalDate startPeriod;

        switch (cycleType) {
            case "day":
                startPeriod = now;
                break;
            case "week":
                startPeriod = now.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
                break;
            case "month":
                startPeriod = now.with(TemporalAdjusters.firstDayOfMonth());
                break;
            case "year":
                startPeriod = now.with(TemporalAdjusters.firstDayOfYear());
                break;
            default:
                throw new IllegalArgumentException("Unsupported cycle type: " + cycleType);
        }
        return startPeriod.atStartOfDay();
    }

    public static LocalDateTime getEndPeriod(String cycleType) {
        LocalDate now = LocalDate.now();
        LocalDate endPeriod;

        switch (cycleType) {
            case "day":
                endPeriod = now.plusDays(1);
                break;
            case "week":
                endPeriod = now.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)).plusDays(1);
                break;
            case "month":
                endPeriod = now.with(TemporalAdjusters.lastDayOfMonth()).plusDays(1);
                break;
            case "year":
                endPeriod = now.with(TemporalAdjusters.lastDayOfYear()).plusDays(1);
                break;
            default:
                throw new IllegalArgumentException("Unsupported cycle type: " + cycleType);
        }
        return endPeriod.atStartOfDay();
    }

    public static LocalDateTime getPreviousStartPeriod(String cycleType) {
        LocalDate now = LocalDate.now();
        LocalDate startPeriod, endPeriod, previousStartPeriod, previousEndPeriod;


        switch (cycleType) {
            case "day":
                previousStartPeriod = now.minusDays(1);
                break;
            case "week":
                startPeriod = now.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
                endPeriod = now.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)).plusDays(1);
                previousStartPeriod = startPeriod.minusWeeks(1);
                previousEndPeriod = startPeriod;
                break;
            case "month":
                startPeriod = now.with(TemporalAdjusters.firstDayOfMonth());
                endPeriod = now.with(TemporalAdjusters.lastDayOfMonth()).plusDays(1);
                previousStartPeriod = startPeriod.minusMonths(1);
                previousEndPeriod = startPeriod;
                break;
            default:
                throw new IllegalArgumentException("Unsupported cycle type: " + cycleType);
        }

        return previousStartPeriod.atStartOfDay();
    }

    public static LocalDateTime getPreviousEndPeriod(String cycleType) {
        LocalDate now = LocalDate.now();
        LocalDate startPeriod, endPeriod, previousStartPeriod, previousEndPeriod;

        switch (cycleType) {
            case "day":
                startPeriod = now;
                endPeriod = now.plusDays(1);
                previousStartPeriod = now.minusDays(1);
                previousEndPeriod = now;
                break;
            case "week":
                startPeriod = now.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
                endPeriod = now.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)).plusDays(1);
                previousStartPeriod = startPeriod.minusWeeks(1);
                previousEndPeriod = startPeriod;
                break;
            case "month":
                startPeriod = now.with(TemporalAdjusters.firstDayOfMonth());
                endPeriod = now.with(TemporalAdjusters.lastDayOfMonth()).plusDays(1);
                previousStartPeriod = startPeriod.minusMonths(1);
                previousEndPeriod = startPeriod;
                break;
            default:
                throw new IllegalArgumentException("Unsupported cycle type: " + cycleType);
        }

        return previousEndPeriod.atStartOfDay();
    }

}
