package com.spider.quartz.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

public class UsRecallTimeUtils {
    private static final Map<String, String> MONTH_MAP = new HashMap<>();
    private static final SimpleDateFormat RECALL_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");

    static {
        // 英文月份映射
        MONTH_MAP.put("January", "01");
        MONTH_MAP.put("February", "02");
        MONTH_MAP.put("March", "03");
        MONTH_MAP.put("April", "04");
        MONTH_MAP.put("May", "05");
        MONTH_MAP.put("June", "06");
        MONTH_MAP.put("July", "07");
        MONTH_MAP.put("August", "08");
        MONTH_MAP.put("September", "09");
        MONTH_MAP.put("October", "10");
        MONTH_MAP.put("November", "11");
        MONTH_MAP.put("December", "12");

        // 中文月份映射
        MONTH_MAP.put("一月", "01");
        MONTH_MAP.put("二月", "02");
        MONTH_MAP.put("三月", "03");
        MONTH_MAP.put("四月", "04");
        MONTH_MAP.put("五月", "05");
        MONTH_MAP.put("六月", "06");
        MONTH_MAP.put("七月", "07");
        MONTH_MAP.put("八月", "08");
        MONTH_MAP.put("九月", "09");
        MONTH_MAP.put("十月", "10");
        MONTH_MAP.put("十一月", "11");
        MONTH_MAP.put("十二月", "12");

        // 英文月份缩写映射
        MONTH_MAP.put("Jan", "01");
        MONTH_MAP.put("Feb", "02");
        MONTH_MAP.put("Mar", "03");
        MONTH_MAP.put("Apr", "04");
        MONTH_MAP.put("May", "05");
        MONTH_MAP.put("Jun", "06");
        MONTH_MAP.put("Jul", "07");
        MONTH_MAP.put("Aug", "08");
        MONTH_MAP.put("Sep", "09");
        MONTH_MAP.put("Oct", "10");
        MONTH_MAP.put("Nov", "11");
        MONTH_MAP.put("Dec", "12");
    }

    /**
     * 解析召回日期字符串为Date对象
     * 支持格式："一月 30, 2025", "January 30, 2025", "2025-01-30", "01/30/2025"等
     *
     * @param dateStr 日期字符串
     * @return Date对象，解析失败返回null
     */
    public static Date parseRecallDate(String dateStr) {
        if (dateStr == null || dateStr.trim().isEmpty()) {
            return null;
        }

        try {
            String cleanedDateStr = dateStr.trim();

            // 1. 尝试直接解析标准格式 yyyy-MM-dd
            try {
                return RECALL_DATE_FORMAT.parse(cleanedDateStr);
            } catch (ParseException e) {
                // 继续尝试其他格式
            }

            // 2. 处理中英文月份格式："一月 30, 2025" 或 "January 30, 2025"
            if (cleanedDateStr.matches("^[\\u4e00-\\u9fa5]+\\s+\\d{1,2},\\s+\\d{4}$") ||
                    cleanedDateStr.matches("^[a-zA-Z]+\\s+\\d{1,2},\\s+\\d{4}$")) {

                String[] parts = cleanedDateStr.split("\\s+");
                if (parts.length >= 3) {
                    String monthStr = parts[0].trim();
                    String dayStr = parts[1].replace(",", "").trim();
                    String yearStr = parts[2].trim();

                    // 映射月份
                    String monthNum = MONTH_MAP.get(monthStr);
                    if (monthNum != null) {
                        // 补全日份为两位数
                        if (dayStr.length() == 1) {
                            dayStr = "0" + dayStr;
                        }
                        String formattedDate = yearStr + "-" + monthNum + "-" + dayStr;
                        return RECALL_DATE_FORMAT.parse(formattedDate);
                    }
                }
            }

            // 3. 处理其他常见日期格式
            String[] patterns = {
                    "MM/dd/yyyy",
                    "MMMM dd, yyyy",
                    "MMM dd, yyyy",
                    "yyyy/MM/dd",
                    "dd-MM-yyyy",
                    "dd/MM/yyyy"
            };

            for (String pattern : patterns) {
                try {
                    SimpleDateFormat sdf = new SimpleDateFormat(pattern, Locale.US);
                    Date date = sdf.parse(cleanedDateStr);
                    // 确保返回的日期格式统一为 yyyy-MM-dd
                    return RECALL_DATE_FORMAT.parse(RECALL_DATE_FORMAT.format(date));
                } catch (ParseException e) {
                    // 继续尝试下一个格式
                }
            }

            System.err.println("UsRecallTimeUtils - 无法解析召回日期: " + dateStr);
            return null;

        } catch (Exception e) {
            System.err.println("UsRecallTimeUtils - 解析召回日期异常: " + dateStr + ", 错误: " + e.getMessage());
            return null;
        }
    }

    /**
     * 格式化日期为字符串
     *
     * @param date 日期对象
     * @return 格式化的日期字符串 (yyyy-MM-dd)
     */
    public static String formatRecallDate(Date date) {
        if (date == null) {
            return null;
        }
        return RECALL_DATE_FORMAT.format(date);
    }

    /**
     * 检查日期字符串是否可以被解析
     *
     * @param dateStr 日期字符串
     * @return 是否可以解析
     */
    public static boolean isParseableDate(String dateStr) {
        if (dateStr == null || dateStr.trim().isEmpty()) {
            return false;
        }
        return parseRecallDate(dateStr) != null;
    }

    /**
     * 获取月份映射（用于测试或其他用途）
     */
    public static Map<String, String> getMonthMap() {
        return new HashMap<>(MONTH_MAP);
    }
}