/*
 * @Author       : WuWei
 * @LastEditors  : WuWei
 * @Date         : 2021-05-18 16:45:38
 * @LastEditTime : 2023-08-16 17:05:17
 * @FilePath     : /wisdom-huiguanjia-app/wisdom_cli/lib/src/utils/cron.dart
 * @Description  : Do not edit
 */
class CronUtil {
  static List<Map<String, String>> weekArr = [
    {'day': '1', 'label': '周日'},
    {'day': '2', 'label': '周一'},
    {'day': '3', 'label': '周二'},
    {'day': '4', 'label': '周三'},
    {'day': '5', 'label': '周四'},
    {'day': '6', 'label': '周五'},
    {'day': '7', 'label': '周六'}
  ];

  ///Convert to cron expression
  static String parseCron(CronBean cronBean) {
    String cronString =
        "0 ${cronBean.startMinute},${cronBean.endMinute} ${cronBean.startHour},${cronBean.endHour} ${cronBean.moreDay} 0 ${cronBean.moreWeek}";
    return cronString;
  }

  static String cron2hourAndMinutes(String? cronString) {
    if (cronString == null) {
      return '';
    }
    String content = '';
    final cronArr = cronString.split(' ');

    content += cronArr[2] != '*' && cronArr[2] != '?' ? cronArr[2] : '00';
    content +=
        cronArr[1] != '*' && cronArr[1] != '?' ? ':${cronArr[1]}' : ':00';
    return content;
  }

  static String cron2week(String? cronString) {
    if (cronString == null) {
      return '';
    }
    String content = '';

    final cronArr = cronString.split(' ');
    if (cronArr[5] != '*' && cronArr[5] != '?') {
      final weeks = cronArr[5].split(',');
      weeks.sort((a, b) => int.parse(a).compareTo(int.parse(b)));
      weeks.forEach((week) {
            if (week != '1')
              {
                content += '${[
                  '1',
                  '2',
                  '3',
                  '4',
                  '5',
                  '6',
                  '7'
                ].contains(week) ? '每${weekArr[weekArr.indexWhere((w) => w['day'] == week)]['label']}' : ''},';
              };
          });
      content += weeks.contains('1') ? '每周日，' : '';
      content = content.substring(0, content.length - 1);
    }
    return content;
  }

  ///"0 30 10 ? * 3"
  ///return 每周二10:30:00
  static String cron2String(String? cronString) {
    if (cronString == null) {
      return '';
    }
    String content = '';

    _fillZero(val) {
      return int.parse(val) < 10 ? '0$val' : val;
    }

    final cronArr = cronString.split(' ');
    if (cronArr[5] != '*' && cronArr[5] != '?') {
      final weeks = cronArr[5].split(',');
      weeks.forEach((week) {
            content += '${[
              '1',
              '2',
              '3',
              '4',
              '5',
              '6',
              '7'
            ].contains(week) ? '每${weekArr[weekArr.indexWhere((w) => w['day'] == week)]['label']}' : ''},';
          });
      content = content.substring(0, content.length - 1);
    }

    if (cronArr[4] != '*') {
      final months = cronArr[4].split(',');
      for (var month in months) {
        content += '$month月${cronArr[3]}日,';
      }
      content = content.substring(0, content.length - 1);
    } else if (cronArr[3] != '*' && cronArr[3] != '?') {
      content += '每月${cronArr[3]}日';
    }

    if (cronArr[2] != '*') {
      if (cronArr[2].indexOf('0/') == 1) {
        content = '每${cronArr[2].replaceFirst('0/', '')}小时';
      } else {
        if (cronArr[2].indexOf('0/') == 1) {
          final hours = cronArr[2].split(',');
          for (final hour in hours) {
            content += '${_fillZero(hour)}:${_fillZero(cronArr[1])},';
          }
          content = content.substring(0, content.length - 1);
        } else {
          content += '${_fillZero(cronArr[2])}:${_fillZero(cronArr[1])}:00';
        }
      }
    }

    if (cronArr[1].indexOf('0/') == 1) {
      content = '每${cronArr[1].replaceFirst('0/', '')}分钟';
    }

    return content;
  }

  static String number2WeekString(String number) {
    String str = "";
    if (number == "1") {
      str = "周一";
    } else if (number == "2") {
      str = "周二";
    } else if (number == "3") {
      str = "周三";
    } else if (number == "4") {
      str = "周四";
    } else if (number == "5") {
      str = "周五";
    } else if (number == "6") {
      str = "周六";
    } else if (number == "7") {
      str = "周日";
    }
    return str;
  }
}

class CronStringBean {
  String? startTime;
  String? endTime;
  int? type;
  String? weekString;
  String? dayString;
  List<int>? weekDataList;
  List<int>? dayDataList;
  int? startMinute;
  int? endMinute;
  int? startHour;
  int? endHour;
}

class CronBean {
  int? startMinute;
  int? endMinute;
  int? startHour;
  int? endHour;
  String? moreDay;
  String? moreWeek;
  CronBean(this.startMinute, this.endMinute, this.startHour, this.endHour,
      this.moreDay, this.moreWeek);
}
