let dateUtil = {};

dateUtil.today = () => {
  return new Date();
};
/**
 * 日期字符串转日期
 * 参数1:日期字符串
 */
dateUtil.stringToDate = (dateString) => {
  return new Date(dateString);
};
/**
 * 日期转日期数组[年,月,日,时,分,秒]
 * 参数1:日期
 */
dateUtil.dateToArrays = (date) => {
  return [
    date.getFullYear(),
    dateUtil.addZero(date.getMonth() + 1),
    dateUtil.addZero(date.getDate()),
    dateUtil.addZero(date.getHours()),
    dateUtil.addZero(date.getMinutes()),
    dateUtil.addZero(date.getSeconds())
  ];
};
/**
 * 日期增加
 * 参数1:增加的是s-秒 n-分 h-小时 d-天 w-周 m-月 y-年
 * 参数2:增加的数量
 * 参数3:时间的毫秒数
 */
dateUtil.dateAdd = (date, numDay, formatPatten) => {
  let dateArrays = dateUtil.dateToArrays(date);
  switch (formatPatten) {
    case 's':
      return new Date(dateArrays[0], dateArrays[1] - 1, dateArrays[2],
        dateArrays[3], dateArrays[4], dateArrays[5] - 0 + numDay);
    case 'n':
      return new Date(dateArrays[0], dateArrays[1] - 1, dateArrays[2],
        dateArrays[3], dateArrays[4] - 0 + numDay, dateArrays[5]);
    case 'h':
      return new Date(dateArrays[0], dateArrays[1] - 1, dateArrays[2],
        dateArrays[3] - 0 + numDay, dateArrays[4], dateArrays[5]);
    case 'd':
      return new Date(dateArrays[0], dateArrays[1] - 1, dateArrays[2] - 0 + numDay,
        dateArrays[3], dateArrays[4], dateArrays[5]);
    case 'w':
      return new Date(dateArrays[0], dateArrays[1] - 1, dateArrays[2] - 0 + numDay * 7,
        dateArrays[3], dateArrays[4], dateArrays[5]);
    case 'm':
      return new Date(dateArrays[0], dateArrays[1] - 1 + numDay,
        dateArrays[2], dateArrays[3], dateArrays[4], dateArrays[5]);
    case 'y':
      return new Date(dateArrays[0] + numDay, dateArrays[1] - 1,
        dateArrays[2], dateArrays[3], dateArrays[4], dateArrays[5]);
  }
};

dateUtil.ShowDate = (date) => {
  var num = date;
  let n = num;
  let d = new Date();
  let year = d.getFullYear();
  let mon = d.getMonth() + 1;
  let day = d.getDate();
  if (day <= n) {
    if (mon > 1) {
      mon = mon - 1;
    } else {
      year = year - 1;
      mon = 12;
    }
  }
  d.setDate(d.getDate() - n);
  year = d.getFullYear();
  mon = d.getMonth() + 1;
  day = d.getDate();
  let s = year + '-' + (mon < 10 ? ('0' + mon) : mon) + '-' + (day < 10 ? ('0' + day) : day);
  return s;
};

/**
 *  返回零开头的数字
 * 参数一 时间
 */
dateUtil.addZero = (temp) => {
  return temp < 10 ? '0' + temp : temp;
};
/**
 * 获取当天属于星期几
 * 参数1:时间的毫秒数
 */
dateUtil.getDayOrWeek = (date) => {
  let dayWeek = [];
  let year = date.getFullYear();
  let month = date.getMonth() + 1;
  let day = date.getDate();
  let week = date.getDay();
  switch (week) {
    case 1:
      week = '星期一';
      break;
    case 2:
      week = '星期二';
      break;
    case 3:
      week = '星期三';
      break;
    case 4:
      week = '星期四';
      break;
    case 5:
      week = '星期五';
      break;
    case 6:
      week = '星期六';
      break;
    case 7:
      week = '星期日';
  }
  dayWeek.push(year);
  dayWeek.push(dateUtil.addZero(month));
  dayWeek.push(dateUtil.addZero(day));
  dayWeek.push(week);
  return dayWeek;
};
/**
 * 是否闰年
 * 参数1:当前日期是否是闰年
 */
dateUtil.isLeapYear = (date) => {
  let year = date.getFullYear();
  return !!((year & 3) === 0 && (year % 100 || (year % 400 === 0 && year)));
};
/**
 * 某月有几天
 * 参数1:日期
 */
dateUtil.getDaysInMonth = (date) => {
  let daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  let m = date.getMonth();
  return m === 1 && dateUtil.isLeapYear() ? 29 : daysInMonth[m];
};
/**
 *获取某月的第一天
 *参数1:日期
 */
dateUtil.getFirstDayOfMonth = (date) => {
  return new Date(date.getFullYear(), date.getMonth(), 1);
};
/**
 *获取某月的最后一天
 *参数1:日期
 */
dateUtil.getLastDayOfMonth = (date) => {
  return new Date(date.getFullYear(), date.getMonth(), dateUtil.getDaysInMonth());
};
/**
 * 返回日期的字符串
 * 参数1:日期的毫秒数
 * 参数2:精确到:s-秒 n-分 h-小时 d-天 m-月 y-年
 * 参数3:是否中文 true 或者 false 默认false
 */
dateUtil.dateToString = (date, formatPatten, isChn = false) => {
  let dateArrays = dateUtil.dateToArrays(date);
  let pattenArrays = [];
  if (isChn) {
    pattenArrays = ['年', '月', '日 ', '时', '分', '秒'];
  } else {
    pattenArrays = ['-', '-', ' ', ':', ':', ''];
  }
  switch (formatPatten) {
    case 's':
      return dateArrays[0] + pattenArrays[0] + dateArrays[1] + pattenArrays[1] +
        dateArrays[2] + pattenArrays[2] + dateArrays[3] + pattenArrays[3] +
        dateArrays[4] + pattenArrays[4] + dateArrays[5] + pattenArrays[5];
    case 'n':
      return dateArrays[0] + pattenArrays[0] + dateArrays[1] + pattenArrays[1] +
        dateArrays[2] + pattenArrays[2] + dateArrays[3] + pattenArrays[3] +
        dateArrays[4] + (isChn ? pattenArrays[4] : '');
    case 'h':
      return dateArrays[0] + pattenArrays[0] + dateArrays[1] + pattenArrays[1] +
        dateArrays[2] + pattenArrays[2] + dateArrays[3] + (isChn ? pattenArrays[3] : '');
    case 'd':
      return dateArrays[0] + pattenArrays[0] + dateArrays[1] + pattenArrays[1] +
        dateArrays[2] + (isChn ? pattenArrays[2] : '');
    case 'm':
      return dateArrays[0] + pattenArrays[0] + dateArrays[1] + (isChn ? pattenArrays[1] : '');
    case 'y':
      return dateArrays[0] + (isChn ? pattenArrays[0] : '');
    default:
      return date;
  }
};
/**
 * 计算两日期相差多少天
 * 参数1:开始时间字符串
 * 参数2:结束时间字符串
 */
dateUtil.getDateNum = (leaveStartTime, leaveEndTime) => {
  if (!leaveStartTime || !leaveEndTime) {
    return;
  }
  let beginTime = null;
  let endTime = null;
  if (leaveStartTime instanceof Date) {
    beginTime = leaveStartTime;
  } else {
    let begin = leaveStartTime.substring(0, 10);
    let checkBegin = begin.split('-');
    beginTime = new Date(Number(checkBegin[0]), Number(checkBegin[1] - 1), Number(checkBegin[2]));
  }
  if (leaveEndTime instanceof Date) {
    endTime = leaveEndTime;
  } else {
    let end = leaveEndTime.substring(0, 10);
    let checkEnd = end.split('-');
    endTime = new Date(Number(checkEnd[0]), Number(checkEnd[1] - 1), Number(checkEnd[2]));
  }
  let sDate = endTime.getTime() - beginTime.getTime();
  return Math.floor(sDate / (24 * 3600 * 1000));
};

dateUtil.formatMonth = (date) => {
  let year = date.getFullYear();
  let month = (date.getMonth() + 1).toString().padStart(2, '0');
  return `${year}-${month}`;
};

export default dateUtil;
