import dayjs from 'dayjs';

export const cronToChinese = (cronExpression) => {
  const parts = cronExpression.split(' ');
  if (parts.length < 5 || parts.length > 6) {
    return "输入的Cron表达式格式不正确";
  }
  const [second, minute, hour, dayOfMonth, month, dayOfWeek] = parts;
  let result = "";
  // 处理秒
  result += translateField(second, 0, 59, "秒") + " ";
  // 处理分
  result += translateField(minute, 0, 59, "分") + " ";
  // 处理时
  result += translateField(hour, 0, 23, "时") + " ";
  // 处理日
  result += translateDayOfMonth(dayOfMonth, 1, 31) + " ";
  // 处理月
  result += translateMonth(month) + " ";
  // 如果有周几字段（六个字段的情况）
  if (parts.length === 6) {
    result += translateDayOfWeek(dayOfWeek);
  }
  return result.trim();
}

const translateField = (field, min, max, unit) => {
  if (field === "*") {
    return `每${unit}`;
  } else if (field.indexOf("/") > -1) {
    const [start, interval] = field.split("/");
    if (start === "*") {
      return `每${interval}${unit}`;
    }
  } else if (field.indexOf("-") > -1) {
    const [start, end] = field.split("-");
    return `${start}到${end}${unit}`;
  } else if (field.indexOf(",") > -1) {
    const values = field.split(",");
    return values.map(v => v + unit).join("、");
  }
  return "";
}

const translateDayOfMonth = (field, min, max) => {
  if (field === "L") {
    return "每月最后一天";
  } else if (field.indexOf("L-") === 0) {
    const num = parseInt(field.slice(2));
    return `每月倒数第${num}天`;
  } else if (field.indexOf("W") > -1) {
    if (field === "W") {
      return "最接近给定日期的工作日";
    } else if (field.indexOf("-") > -1) {
      const [start, end] = field.split("-");
      return `${start}到${end}的最接近给定日期的工作日`;
    }
  }
  return translateField(field, min, max, "日");
}

const translateMonth = (month) => {
  const months = ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"];
  if (month === "*") {
    return "每月";
  } else if (month.indexOf("-") > -1) {
    const [start, end] = month.split("-");
    const startIndex = parseInt(start) - 1;
    const endIndex = parseInt(end) - 1;
    return `${months[startIndex]}到${months[endIndex]}月`;
  } else if (month.indexOf(",") > -1) {
    const values = month.split(",");
    return values.map(v => months[parseInt(v) - 1]).join("、");
  }
  return months[parseInt(month) - 1] + "月";
}

const translateDayOfWeek = (field) => {
  const days = ["周六", "周日", "周一", "周二", "周三", "周四", "周五"];
  if (field === "*") {
    return "每周";
  } else if (field === "L") {
    return "每月最后一个星期";
  } else if (field.indexOf("#") > -1) {
    const [day, num] = field.split("#");
    return `每月第${num}个${days[parseInt(day)]}`;
  } else if (field.indexOf("-") > -1) {
    const [start, end] = field.split("-");
    const startIndex = parseInt(start);
    const endIndex = parseInt(end);
    return `${days[startIndex]}到${days[endIndex]}日`;
  } else if (field.indexOf(",") > -1) {
    const values = field.split(",");
    return values.map(v => days[parseInt(v)]).join("、");
  }
  return days[parseInt(field)] + "日";
}
// 使用dayjs，计算日期是周几
export const parseDateToWeekday = (dateString) => {
  const date = dayjs(dateString);
  const dayOfWeek = date.day();
  const weekDays = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
  return {
    dayOfWeek,
    weekDayName: weekDays[dayOfWeek]
  };
}

