package com.heyu.api.schedule;

import com.heyu.api.data.utils.DateUtils;
import org.springframework.scheduling.support.CronSequenceGenerator;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public enum TimeLevelEnums {
    years("年"),
    months("月"),
    days("日"),
    hours("小时"),
    minutes("分钟"),
    seconds("秒"),
    ;

    private String level;

    TimeLevelEnums(String level) {
        this.level = level;
    }

    public String getLevel() {
        return level;
    }

    public void setLevel(String level) {
        this.level = level;
    }



    public static TimeLevelEnums getTimeLevel(String cronExpression) {
        try {
            Date nextTunTime = CronTriggerUtils.getCurrentExecTime(cronExpression);
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            CronSequenceGenerator cronSequenceGenerator = new CronSequenceGenerator(cronExpression);
            List<String> nextTimes = new ArrayList<>();
            for (int i = 0; i < 10; i++) {
                nextTimes.add( df.format(nextTunTime));
                Date nextTriggerTime = cronSequenceGenerator.next(nextTunTime);//lastMinute 我这里是上一分钟的date类型对象
                nextTunTime = nextTriggerTime;
            }

            String aStr = nextTimes.get(0);
            String bStr = nextTimes.get(1);
            char arrayA []= aStr.toCharArray();
            char arrayB[] = bStr.toCharArray();
            int eq = 0;
            for (int i = 0; i < arrayA.length; i++) {
                char a = arrayA[i];
                char b = arrayB[i];
                if (a == b) {
                    eq++;
                }else{
                    break;
                }
            }
            //2025-10-20 18:27:00
            if(eq < 4){
                return TimeLevelEnums.years;
            }else if(eq < 7){
                return TimeLevelEnums.months;
            }else if(eq < 10){
                return TimeLevelEnums.days;
            }else if(eq < 13){
                return TimeLevelEnums.hours;
            }else if(eq < 16){
                return TimeLevelEnums.minutes;
            }else if (eq>=17){
                return TimeLevelEnums.seconds;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getDfCurrentDate(TimeLevelEnums enums){
        if(TimeLevelEnums.years.equals(enums)){
            SimpleDateFormat sdf = new SimpleDateFormat(DateUtils.YYYY);
            return sdf.format(new Date());
        }else if (TimeLevelEnums.months.equals(enums)){
            SimpleDateFormat sdf = new SimpleDateFormat(DateUtils.YYYY_MM);
            return sdf.format(new Date());
        }else if (TimeLevelEnums.days.equals(enums)){
            SimpleDateFormat sdf = new SimpleDateFormat(DateUtils.YYYY_MM_DD);
            return sdf.format(new Date());
        }else if (TimeLevelEnums.hours.equals(enums)){
            SimpleDateFormat sdf = new SimpleDateFormat(DateUtils.YYYY_MM_DD_HH);
            return sdf.format(new Date());
        }else if (TimeLevelEnums.minutes.equals(enums)){
            SimpleDateFormat sdf = new SimpleDateFormat(DateUtils.YYYY_MM_DD_HH_MM);
            return sdf.format(new Date());
        }
        SimpleDateFormat sdf = new SimpleDateFormat(DateUtils.YYYY_MM_DD_HH_MM_SS);
        return sdf.format(new Date());
    }
}
