/*
 * 创建时间和现在时间作对比
 * @param time 时间戳
 *
 * */
export function beforeTime(time) {
  let way = Date.parse(new Date()) / 1000 - time
  let r = ''
  if (way < 60) {
    r = '刚刚'
  } else if (way >= 60 && way < 3600) {
    r = Math.floor(way / 60) + '分钟前'
  } else if (way >= 3600 && way < 86400) {
    r = Math.floor(way / 3600) + '小时前'
  } else if (way >= 86400 && way < 2592000) {
    r = Math.floor(way / 86400) + '天前'
  } else if (way >= 2592000 && way < 15552000) {
    r = Math.floor(way / 2592000) + '个月前'
  } else if (time === 0 || !time) {
    r = '暂无'
  } else {
    r = dateChange('Y-m-d', time)
  }
  return r
}

/*
 * 返回时间年月日时分秒
 * @param {date}  时间戳
 * @param {fmt}   转换格式  'yyyy-MM-dd hh:mm:ss'
 *
 * */
export function formatDate(date, fmt) {
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  var o = {
    'M+': date.getMonth() + 1,
    'd+': date.getDate(),
    'h+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds()
  }
  for (let k in o) {
    if (new RegExp(`(${k})`).test(fmt)) {
      let str = o[k] + ''
      fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : padLeftZero(str))
    }
  }
  return fmt
}

function padLeftZero(str) {
  return ('00' + str).substr(str.length)
}

export function formatTimestamp(timestamp) {
  const now = new Date()
  const targetDate = new Date(timestamp)

  // 时间差
  const timeDifference = now - targetDate

  if (isNaN(timeDifference)) {
    return ''
  }

  // 年月日时分
  const minutes = Math.floor(timeDifference / 1000 / 60)
  const hours = targetDate.getHours()
  const day = targetDate.getDate()
  const month = targetDate.getMonth() + 1 // 月份从0开始，所以加1
  const year = targetDate.getFullYear()

  // 获取当前日期的年月日
  const currentYear = now.getFullYear()
  const currentMonth = now.getMonth() + 1
  const currentDay = now.getDate()

  // 补零函数
  const padZero = (num) => num.toString().padStart(2, '0')

  // 小于1分钟
  if (minutes < 1) {
    return '刚刚'
  }

  // 同一天的不同时间段
  if (year === currentYear && targetDate.getDate() === currentDay) {
    const formattedHour = padZero(hours)
    const formattedMinute = padZero(targetDate.getMinutes())

    if (hours < 6) {
      return `凌晨 ${formattedHour}:${formattedMinute}`
    } else if (hours < 12) {
      return `上午 ${formattedHour}:${formattedMinute}`
    } else if (hours < 18) {
      return `下午 ${formattedHour - 12}:${formattedMinute}`
    } else {
      return `晚上 ${formattedHour - 12}:${formattedMinute}`
    }
  }

  // 昨天
  const yesterday = new Date(now)
  yesterday.setDate(now.getDate() - 1)
  if (year === currentYear && targetDate.getDate() === yesterday.getDate()) {
    return '昨天'
  }

  // 同年
  if (year === currentYear) {
    return `${month}月${day}日`
  }

  // 大于一年
  return `${year}年${padZero(month)}月${padZero(day)}日`
}

// 消息的内容转换
export function messageContentParse(messageContent) {
  try {
    let content = {}
    if (messageContent) {
      content = JSON.parse(messageContent)
      return content.messageContent
    } else {
      return ''
    }
  } catch (error) {
    console.log(error)
  }
}
