import { format, parse } from "fecha";

/**
 * @export 格式日期
 */
export function fmtDate (date, fmt) {
  return format(date, fmt || "isoDate");
}

/**
 * @export 格式时间
 */
export function fmtTime (date, fmt) {
  return format(date, fmt || "HH:mm:ss");
}

/**
 * @export 格式日期+时间
 */
export function fmtDateTime (date, fmt) {
  return format(date, fmt || "YYYY-MM-DD HH:mm:ss");
}

/**
 * @export 解析日期
 */
export function parseDate (dateStr, fmt) {
  return parse(dateStr, fmt || "isoDate");
}

/**
 * @export 解析时间
 */
export function parseTime (dateStr, fmt) {
  return parse(dateStr, fmt || "HH:mm:ss");
}

/**
 * @export 解析日期+时间
 */
export function parseDateTime (dateStr, fmt) {
  return parse(dateStr, fmt || "YYYY-MM-DD HH:mm:ss");
}

// ------------- 业务逻辑 ------------- //

/**
 * @export 返回时间间隔:yyyy-MM-dd
 * @param dateNow :Date类
 * @param intervalDays ：间隔天数
 * @param bolPastTime  ：Boolean,判断在参数date之前，还是之后，
 */
export function rangeDate (dateNow, intervalDays, bolPastTime) {
  let oneDayTime = 24 * 60 * 60 * 1000;
  let list = [];
  let lastDay;
  if (bolPastTime == true) {
    lastDay = new Date(dateNow.getTime() - intervalDays * oneDayTime);
    list.push(fmtDate(lastDay));
    list.push(fmtDate(dateNow));
  } else {
    lastDay = new Date(dateNow.getTime() + intervalDays * oneDayTime);
    list.push(fmtDate(dateNow));
    list.push(fmtDate(lastDay));
  }
  return list;
}

/**
 * @export 返回指定日期的间隔日期对象:非时间戳
 * @param days: 时间间隔, 往前可为负数
 * @param date: 指定日期, 默认当前日期
 */
export function rangeDays (days = 0, date = new Date()) {
  date = new Date(date); // setTime 会修改对象, 深拷贝
  return new Date(date.setTime(date.getTime() + 3600 * 1000 * 24 * days));
}

/** @exports 计算时间差:day */
export function calcCeilDay (timestampStart, timestampEnd) {
  let start = new Date(timestampStart);
  let end = new Date(timestampEnd);
  start = format(start, "YYYY-MM-DD");
  end = format(end, "YYYY-MM-DD");
  start = parse(`${start} 00:00:00`, "YY-MM-DD HH:mm:ss");
  end = parse(`${end} 23:59:59`, "YY-MM-DD HH:mm:ss");
  const day = (end.getTime() - start.getTime()) / 1000 / 60 / 60 / 24;
  return Math.ceil(day);
}

/** @exports 获取指定年月的最后一天 */
export function getLastDateFromYM (year, month) {
  if (!year) year = new Date().getFullYear();
  if (!month) month = new Date().getMonth() + 1;
  // 使用new Date(year,month,0)的方式,可以获取该月的最后一天
  return new Date(year, month, 0);
}

/** @exports 获取指定年月的第一天 */
export function getFirstDateFromYM (year, month) {
  if (!year) year = new Date().getFullYear();
  if (!month) month = new Date().getMonth();
  return new Date(year, month, 1);
}

/** @exports 获取自指定日期或者今天是周几 */
export function getWeekFromDate (date = new Date(), fmt = "天一二三四五六") {
  // 使用new Date(year,month,0)的方式,可以获取该月的最后一天
  return fmt.charAt(date.getDay());
}

/** @exports 获取当前日期本周的周日-周六,返回-Date */
export function getCurrentWeekFromDate (date = new Date()) {
  let start = new Date(date.getTime() - date.getDay() * 86400000);
  let end = new Date(date.getTime() + (6 - date.getDay()) * 86400000);
  start = format(start, "YYYY-MM-DD");
  start = parse(`${start} 00:00:00`, "YY-MM-DD HH:mm:ss");
  end = format(end, "YYYY-MM-DD");
  end = parse(`${end} 23:59:59`, "YY-MM-DD HH:mm:ss");
  return [start, end];
}

/**
 * @export Element-ui:返回指定日期的周一日期对象
 * @param date: 指定日期, 默认当前日期
 */

export function weekFirstElementUI (date = new Date()) {
  date = new Date(date); // setTime 会修改对象, 深拷贝
  var day = date.getDay();
  var weeks = new Array(5, -1, 0, 1, 2, 3, 4);
  var week = weeks[day];
  return new Date(date.setTime(date.getTime() - 3600 * 1000 * 24 * week));
}

/**
 * @export Element-ui:返回指定日期的周一和周天日期字符的集合
 * @param date: 指定日期, element-ui 日期组件周模式绑定值
 */
export function weekRangeElementUI (date) {
  date = new Date(date); // setTime 会修改对象, 深拷贝
  const begin = date.setTime(date.getTime() - 3600 * 1000 * 24);
  const end = date.setTime(date.getTime() + 3600 * 1000 * 24 * 6);
  return [fmtDate(begin) + " 00:00:00", fmtDate(end) + " 23:59:59"];
}

/**
 * @export Element-ui:返回指定日期的周一和周天日期字符的集合
 * @param date: 指定日期, element-ui 日期组件周模式绑定值
 */

export default {
  format, parse,
  fmtDate, fmtTime, fmtDateTime, parseDate, parseTime, parseDateTime,
  rangeDate, rangeDays, calcCeilDay, getLastDateFromYM, getFirstDateFromYM, getWeekFromDate, getCurrentWeekFromDate,
  weekFirstElementUI, weekRangeElementUI
}
