/*
	Version: 1.0
*/

export const oneDayTime = 24 * 60 * 60 * 1000
export const oneHourTime = 60 * 60 * 1000
// 每个月的日期数                1   2   3   4   5   6   7   8   9   10  11  12
export const daysInMonth = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

// 格式化数字为两位字符表示
export function format(n) {
  const m = '' + n
  if (n < 10) {
    return m.padStart(2, '0')
  } else {
    return m
  }
}

// 获取据今日相距offset的日期
export function getDate(offset, level) {
  const today = new Date()
  const targetVal = today.getTime() + (offset * 1000 * 3600 * 24)
  return formatToString(targetVal, level)
}

// 获取上个月
export function getLastMonth() {
  const today = new Date()
  const year = today.getFullYear()
  const month = today.getMonth()
  if (month === 0) {
    return (year - 1) + '-' + '12'
  } else {
    return year + '-' + format(month)
  }
}

// 获取上个月同期
export function getLastMonthDate() {
  const date = new Date()
  const year = date.getFullYear()
  let day = date.getDate()
  let month = date.getMonth()

  // 一、解决闰年平年的二月份天数
  // 平年28天、闰年29天
  // 能被4整除且不能被100整除的为闰年,或能被100整除且能被400整除
  if (((year % 4) === 0) && ((year % 100) !== 0) || ((year % 400) === 0)) {
    daysInMonth[2] = 29
  }

  // 上个月的年月字符串
  const yearMonth = getLastMonth()

  // 获取日期
  if (month === 0) { // 跨年则上个月为12月
    month = 12
  }
  day = Math.min(day, daysInMonth[month])

  return yearMonth + '-' + format(day)
}

// 格式化日期为字符串
export function formatToString(value, level) {
  const date = new Date(value)
  const year = date.getFullYear()
  const month = format(date.getMonth() + 1)
  const day = format(date.getDate())
  const hour = format(date.getHours())
  const minute = format(date.getMinutes())
  const second = format(date.getSeconds())

  level = level.toUpperCase()

  if (level === 'YEAR') {
    return year
  } else if (level === 'MONTH') {
    return year + '-' + month
  } else if (level === 'DATE') {
    return year + '-' + month + '-' + day
  } else if (level === 'HOUR') {
    return year + '-' + month + '-' + day + ' ' + hour
  } else if (level === 'MINUTE') {
    return year + '-' + month + '-' + day + ' ' + hour + ':' + minute
  } else if (level === 'SECOND') {
    return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second
  } else {
    return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second
  }
}

// 解析字符串为日期对象
export function parseToDate(str, level) {
  let year = ''
  let month = ''
  let day = ''
  let hour = ''
  let minute = ''
  let second = ''

  if (level === null || level === '') {
    return null
  }

  level = level.toUpperCase()

  if (level === 'YEAR' && str.length === 4) {
    year = str

    // return Date.UTC(year, 0, 1, 0, 0, 0);
    return new Date(year, 0, 1, 0, 0, 0).valueOf()
  } else if (level === 'MONTH' && str.length >= 7) {
    year = str.substring(0, 4)
    month = str.substring(5, 7)

    // return Date.UTC(year, month - 1, 1, 0, 0, 0);
    return new Date(year, month - 1, 1, 0, 0, 0).valueOf()
  } else if (level === 'DATE' && str.length >= 10) {
    year = str.substring(0, 4)
    month = str.substring(5, 7)
    day = str.substring(8, 10)

    // return Date.UTC(year, month - 1, day, 0, 0, 0);
    return new Date(year, month - 1, day, 0, 0, 0).valueOf()
  } else if (level === 'HOUR' && str.length >= 13) {
    year = str.substring(0, 4)
    month = str.substring(5, 7)
    day = str.substring(8, 10)
    hour = str.substring(11, 13)

    // return Date.UTC(year, month - 1, day, hour, 0, 0) - oneHourTime * 8;
    return new Date(year, month - 1, day, hour, 0, 0).valueOf()
  } else if (level === 'MINUTE' && str.length >= 16) {
    year = str.substring(0, 4)
    month = str.substring(5, 7)
    day = str.substring(8, 10)
    hour = str.substring(11, 13)
    minute = str.substring(14, 16)

    // return Date.UTC(year, month - 1, day, hour, minute, 0) - oneHourTime * 8;
    return new Date(year, month - 1, day, hour, minute, 0).valueOf()
  } else if (level === 'SECOND' && str.length >= 19) {
    year = str.substring(0, 4)
    month = str.substring(5, 7)
    day = str.substring(8, 10)
    hour = str.substring(11, 13)
    minute = str.substring(14, 16)
    second = str.substring(17, 19)

    // return Date.UTC(year, month - 1, day, hour, minute, second) - oneHourTime * 8;
    return new Date(year, month - 1, day, hour, minute, second).valueOf()
  } else {
    return null
  }
}
