import { format } from 'date-fns'
import dayjs from 'dayjs'

const DATE_TIME_FORMAT = 'YYYY-MM-DD HH:mm'
const DATE_FORMAT = 'YYYY-MM-DD '

export function formatToDateTime(date: any, formatStr = DATE_TIME_FORMAT): string {
  return format(date, formatStr)
}

export function formatToDate(date: any, formatStr = DATE_FORMAT): string {
  return format(date, formatStr)
}

export const getTimes = (times: any, keys: Array<string> = [], format: string = 'YYYY-MM-DD') => {
  if (times && times.length) {
    return format == 'YYYY-MM-DD'
      ? {
          [keys[0] || 'start']: dayjs(times[0]).isValid()
            ? dayjs(times[0]).format(format) + ' 00:00:00'
            : '',
          [keys[1] || 'end']: dayjs(times[1]).isValid()
            ? dayjs(times[1]).format(format) + ' 23:59:59'
            : ''
        }
      : {
          [keys[0] || 'start']: dayjs(times[0]).isValid() ? dayjs(times[0]).format(format) : '',
          [keys[1] || 'end']: dayjs(times[1]).isValid() ? dayjs(times[1]).format(format) : ''
        }
  }
  return {}
}
export const filterTimes = (
  times: any,
  keys: Array<string> = [],
  format: string = 'YYYY-MM-DD'
) => {
  if (times && times.length) {
    return {
      [keys[0] || 'start']: dayjs(times[0]).isValid() ? dayjs(times[0]).format(format) : '',
      [keys[1] || 'end']: dayjs(times[1]).isValid() ? dayjs(times[1]).format(format) : ''
    }
  }
  return {}
}

export function addFormMinute(timerStr: any, time = 0) {
  const timer = dayjs('2021-12-17' + ' ' + timerStr).add(time, 'minute')
  return timer.format('HH:mm:ss')
}
export function addFormMinuteAddS(timerStr: any, time = 0) {
  const timer = dayjs('2021-12-17' + ' ' + timerStr)
    .add(time, 'minute')
    .add(1, 'second')
  return timer.format('HH:mm:ss')
}
export function addFormMinuteMS(timerStr: any, time = 0) {
  const timer = dayjs('2021-12-17' + ' ' + timerStr)
    .add(time, 'minute')
    .subtract(1, 'second')
  return timer.format('HH:mm:ss')
}
export function reduceFormMinute(timerStr: any, time = 0) {
  const timer = dayjs('2021-12-17' + ' ' + timerStr).subtract(time, 'minute')
  return timer.format('HH:mm:ss')
}
export function reduceFormMinuteAddS(timerStr: any, time = 0) {
  const timer = dayjs('2021-12-17' + ' ' + timerStr)
    .subtract(time, 'minute')
    .add(1, 'second')
  return timer.format('HH:mm:ss')
}
export function reduceFormMinuteMS(timerStr: any, time = 0) {
  const timer = dayjs('2021-12-17' + ' ' + timerStr)
    .subtract(time, 'minute')
    .subtract(1, 'second')
  return timer.format('HH:mm:ss')
}

// 秒转时分秒
export function formateSeconds(endTime: string) {
  let secondTime = parseInt(endTime) //将传入的秒的值转化为Number
  if (secondTime === 0) {
    return '0秒'
  }
  let min = 0 // 初始化分
  let h = 0 // 初始化小时
  let result = ''
  if (secondTime >= 60) {
    //如果秒数大于等于60，将秒数转换成整数
    min = parseInt(secondTime / 60 + '') //获取分钟，除以60取整数，得到整数分钟
    secondTime = parseInt((secondTime % 60) + '') //获取秒数，秒数取佘，得到整数秒数
    if (min >= 60) {
      //如果分钟大于等于60，将分钟转换成小时
      h = parseInt(min / 60 + '') //获取小时，获取分钟除以60，得到整数小时
      min = parseInt((min % 60) + '') //获取小时后取佘的分，获取分钟除以60取佘的分
    }
  }
  if (h) {
    result = `${h.toString().padStart(2, '0')}时${min.toString().padStart(2, '0')}分${secondTime
      .toString()
      .padStart(2, '0')}秒`
  } else if (min) {
    result = `${min.toString().padStart(2, '0')}分${secondTime.toString().padStart(2, '0')}秒`
  } else {
    result = `${secondTime.toString().padStart(2, '0')}秒`
  }
  return result
}
