package com.ruoyi.cms.domain.query;

import lombok.Data;

import java.time.Year;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Data
public class Staticsquery {
    //月/季度/年
    private String timeDimension;
    //岗位发布数量 招聘增长率
    private String type;
    private String startTime;
    private String endTime;

    public List<String> generateTimeRange() {
        switch (timeDimension) {
            case "月":
                return generateMonthRange();
            case "季度":
                return generateQuarterRange();
            case "年":
                return generateYearRange();
            default:
                throw new IllegalArgumentException("Invalid time dimension: " + timeDimension);
        }
    }

    private List<String> generateMonthRange() {
        List<String> months = new ArrayList<>();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");

        try {
            YearMonth start = YearMonth.parse(startTime, formatter);
            YearMonth end = YearMonth.parse(endTime, formatter);

            if (start.isAfter(end)) {
                YearMonth temp = start;
                start = end;
                end = temp;
            }

            YearMonth current = end;
            while (!current.isBefore(start)) {
                months.add(current.format(formatter));
                current = current.minusMonths(1);
            }

            if (!start.isAfter(end)) {
                Collections.reverse(months);
            }

        } catch (DateTimeParseException e) {
            throw new IllegalArgumentException("Invalid month format. Please use yyyy-MM format.");
        }

        return months;
    }

    private List<String> generateQuarterRange() {
        List<String> quarters = new ArrayList<>();

        try {
            Quarter start = Quarter.parse(startTime);
            Quarter end = Quarter.parse(endTime);

            if (start.isAfter(end)) {
                Quarter temp = start;
                start = end;
                end = temp;
            }

            Quarter current = end;
            while (!current.isBefore(start)) {
                quarters.add(current.toString());
                current = current.previous();
            }

            if (!start.isAfter(end)) {
                Collections.reverse(quarters);
            }

        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException("Invalid quarter format. Please use yyyy-第x季度 format (e.g. 2024-第一季度).");
        }

        return quarters;
    }

    private List<String> generateYearRange() {
        List<String> years = new ArrayList<>();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy");

        try {
            Year start = Year.parse(startTime, formatter);
            Year end = Year.parse(endTime, formatter);

            if (start.isAfter(end)) {
                Year temp = start;
                start = end;
                end = temp;
            }

            Year current = end;
            while (!current.isBefore(start)) {
                years.add(current.format(formatter));
                current = current.minusYears(1);
            }

            if (!start.isAfter(end)) {
                Collections.reverse(years);
            }

        } catch (DateTimeParseException e) {
            throw new IllegalArgumentException("Invalid year format. Please use yyyy format.");
        }

        return years;
    }

    // Helper class for quarter handling with Chinese characters
    private static class Quarter implements Comparable<Quarter> {
        private final int year;
        private final int quarter;
        private static final String[] CHINESE_QUARTERS = {"第一季度", "第二季度", "第三季度", "第四季度"};

        private Quarter(int year, int quarter) {
            if (quarter < 1 || quarter > 4) {
                throw new IllegalArgumentException("Quarter must be between 1 and 4");
            }
            this.year = year;
            this.quarter = quarter;
        }

        public static Quarter parse(String quarterStr) {
            String[] parts = quarterStr.split("-");
            if (parts.length != 2 || !parts[1].startsWith("第") || !parts[1].endsWith("季度")) {
                throw new IllegalArgumentException("Invalid quarter format");
            }

            int year = Integer.parseInt(parts[0]);
            String quarterStrPart = parts[1];

            // Find which quarter this matches
            int quarter = -1;
            for (int i = 0; i < CHINESE_QUARTERS.length; i++) {
                if (CHINESE_QUARTERS[i].equals(quarterStrPart)) {
                    quarter = i + 1;
                    break;
                }
            }

            if (quarter == -1) {
                throw new IllegalArgumentException("Invalid quarter name: " + quarterStrPart);
            }

            return new Quarter(year, quarter);
        }

        public Quarter previous() {
            if (quarter == 1) {
                return new Quarter(year - 1, 4);
            } else {
                return new Quarter(year, quarter - 1);
            }
        }

        public boolean isBefore(Quarter other) {
            if (this.year < other.year) return true;
            if (this.year > other.year) return false;
            return this.quarter < other.quarter;
        }

        public boolean isAfter(Quarter other) {
            return other.isBefore(this);
        }

        @Override
        public String toString() {
            return year + "-" + CHINESE_QUARTERS[quarter - 1];
        }

        @Override
        public int compareTo(Quarter other) {
            if (this.year != other.year) {
                return Integer.compare(this.year, other.year);
            }
            return Integer.compare(this.quarter, other.quarter);
        }
    }

}
