package com.example.zxweb.utils;

import com.example.zxweb.common.constant.enums.WeekEnum;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description 简单型Cron
 * @Author ZhouWenTao
 * @Date 2023/9/14 10:35
 */
public class CronLowUtil {
    public static void main(String[] args) {
        System.out.println(cronToText("* * * * * 1,2 "));
        /*String timeType="everyDay";
        String exeTime="08:30";
        String weeklyDay="1,2,3";//周一，周二，周三
        String day="1,31";
        String startDate="2023-09-10";//开始时间
        String endDate="2023-09-30";//结束时间
        System.out.println(getCron(timeType,exeTime,weeklyDay,day,startDate,endDate));
        //cron转 显示文本
        System.out.println(cronToText(getCron(timeType,exeTime,weeklyDay,day,startDate,endDate)));*/
    }

    /**
     * @param timeType  时间类型(everyDay:每天,weekly:每周,day:每天,date:日期段)
     * @param exeTime   执行时间(HH:mm 例：08:30)
     * @param weeklyDay 1,2,3：周一,周二，周三
     * @param day       每月的第几天,1,31  每月1日，31日
     * @param startDate 范围型，开始日期 2023-09-10
     * @param endDate   范围型，截止日期 2023-09-30
     * @return cron
     */
    public static String getCron(String timeType, String exeTime, String weeklyDay, String day, String startDate, String endDate) {
        String[] time = exeTime.split(":");
        Integer hour = Integer.parseInt(time[0]);//小时
        Integer minute = Integer.parseInt(time[1]);//分钟
        Integer seconds = 0;//Integer.parseInt(time[2]);//秒
        String cron = null;
        //每天
        if ("everyDay".equals(timeType)) {
            cron = String.format("%s %s %s * * ? *", seconds, minute, hour);
        } else if ("weekly".equals(timeType)) {
            //每周
            cron = String.format("0 %s %s ? * %s", minute, hour, weeklyDay);
        } else if ("day".equals(timeType)) {
            //单日
            cron = String.format("0 %s %s ? * ?", minute, hour/*, day*/);
        } else if ("date".equals(timeType)) {
            //日期段
            System.out.println("在" + startDate + "到" + endDate);
            cron = String.format("0 %s %s * * ? *", minute, hour);
        }
        return cron;
    }

    /**
     * 将Cron 解释成文本
     *
     * @param cron cron
     * @return
     */
    public static String cronToText(String cron) {
        if (StringUtils.isBlank(cron)) {
            return "未知";
        }

        StringBuilder sb = new StringBuilder();
        String[] cronSplit = cron.split(" ");

        List<String> years = null;//年
        List<String> weeklys = null;//周
        List<String> months = null;//月
        List<String> days = null;//日
        List<String> hours = null;//小时
        List<String> minutes = null;//分钟
        List<String> seconds = null;//秒

        List<String> nots = Arrays.asList("?", "*");
        if (cronSplit.length >= 7 && !nots.contains(cronSplit[6])) {
            years = Arrays.asList(cronSplit[6].split(","));
        }
        if (cronSplit.length >= 6 && !nots.contains(cronSplit[5])) {
            weeklys = Arrays.asList(cronSplit[5].split(","));
        }
        if (cronSplit.length >= 5 && !nots.contains(cronSplit[4])) {
            months = Arrays.asList(cronSplit[4].split(","));
        }
        if (cronSplit.length >= 4 && !nots.contains(cronSplit[3])) {
            days = Arrays.asList(cronSplit[3].split(","));
        }
        if (cronSplit.length >= 3 && !nots.contains(cronSplit[2])) {
            hours = Arrays.asList(cronSplit[2].split(","));
        }
        if (cronSplit.length >= 2 && !nots.contains(cronSplit[1])) {
            minutes = Arrays.asList(cronSplit[1].split(","));
        }
        if (cronSplit.length >= 1 && !nots.contains(cronSplit[0])) {
            seconds = Arrays.asList(cronSplit[0].split(","));
        }
        //年
        if (!CollectionUtils.isEmpty(years)) {
            StringBuilder sb2 = new StringBuilder();
            for (String year : years) {
                sb2.append(year).append("年,");
            }
            if (sb2.length() > 0) {
                sb.append(sb2.substring(0, sb2.length() - 1)).append(" ");
            }
        }
        //周
        if (!CollectionUtils.isEmpty(weeklys) ) {
            StringBuilder sb2 = new StringBuilder();
            for (String week : weeklys) {
                sb2.append("周").append(NumberToCnUtil.toChineseLower(Integer.parseInt(week))).append(",");
            }
            if (sb2.length() > 0) {
                sb.append(sb2.substring(0, sb2.length() - 1)).append(" ");
            }
        }
        //月
        if (!CollectionUtils.isEmpty(months)) {
            StringBuilder sb2 = new StringBuilder();
            for (String month : months) {
                sb2.append(month).append("月").append(",");
            }
            if (sb2.length() > 0) {
                sb.append(sb2.substring(0, sb2.length() - 1)).append(" ");
            }
        }
        //日
        if (!CollectionUtils.isEmpty(days)) {
            StringBuilder sb2 = new StringBuilder();
            for (String day : days) {
                sb2.append(day).append("日").append(",");
            }
            if (sb2.length() > 0) {
                sb.append(sb2.substring(0, sb2.length() - 1)).append(" ");
            }
        }

        List<String> hhmmssList=new ArrayList<>();
        //时
        if (!CollectionUtils.isEmpty(hours)) {
            for (String hour : hours) {
                String hhmm=new DecimalFormat("00").format(Integer.parseInt(hour));
                if (!CollectionUtils.isEmpty(minutes) && minutes.size()>0) {
                    for (String minute : minutes) {
                        hhmmssList.add(hhmm+":"+new DecimalFormat("00").format(Integer.parseInt(minute)));
                    }
                }else{
                    hhmmssList.add(hhmm+":00");
                }
            }
        }
        sb.append(String.join(",",hhmmssList));
        //判断是不是指定日
        //0 0 8 1 1 ? 2019-2019  2019-01-01 08:00
        /*if (cronSplit.length>=7) {
            String[] years = cronSplit[6].split(",");
            if (years.length==1) {
                //单年
                String year = years[0];
                String[] months = cronSplit[4].split(",");
                if (months.length==1) {
                    //月
                    String month = months[0];
                    String[] days = cronSplit[3].split(",");
                    if (days.length==1) {
                        String day = days[0];
                        String hour = cronSplit[2];
                    }
                }

            }else{
                //多年
            }
        }*/

        /*for (int i = cronSplit.length-1; i >= 0;i--) {
            String c = cronSplit[i];
            if (i==0) {
                //秒
            }else if(i==1){
                //分钟
                if (!"*".equals(c) && !"?".equals(c)) {
                    String[] split = c.split(",");
                    for (String s : split) {
                        sb.append(new DecimalFormat("00").format(Integer.parseInt(s)));
                    }
                }else{
                    sb.append("00");
                }
            }else if(i==2){
                //单天
                if (sb.length()==0) {
                    sb.append("每天");
                }
                //小时
                if (!"*".equals(c) && !"?".equals(c)) {
                    String[] split = c.split(",");
                    for (String s : split) {
                        sb.append(new DecimalFormat("00").format(Integer.parseInt(s))).append(":");
                    }
                }
            }else if(i==3){
                //天
                StringBuilder daySb=new StringBuilder();
                if (!"*".equals(c) && !"?".equals(c)) {
                    String[] split = c.split(",");
                    for (String s : split) {
                        daySb.append(s).append("日").append(",");
                    }
                }
                if (StringUtils.isNotBlank(daySb)) {
                    sb.append(daySb.toString().substring(0,daySb.length()-1));
                }
            }else if(i==4){//目前不需要做月
            }else if(i==5){
                //周
                if (!"*".equals(c) && !"?".equals(c)) {
                    String[] split = c.split(",");
                    //sb.append("每周的 ");
                    for (String s : split) {
                        sb.append("周").append(NumberToCnUtil.toChineseLower(Integer.parseInt(s)));
                    }
                }
            }
        }*/
        return sb.toString();
    }

}
