import dayjs from 'dayjs'
export const handleCall = (phone: string) => {
  uni.makePhoneCall({ phoneNumber: phone })
}
export const isImageOrVideo = (url) => {
  const ext = url.split('.').pop().toLowerCase()
  if (['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp'].includes(ext)) {
    return 'image'
  } else if (['mp4', 'avi', 'mov', 'mkv', 'webm', 'flv'].includes(ext)) {
    return 'video'
  } else {
    return 'unknown'
  }
}
export const toast = (msg: string, icon = 'none') => {
  uni.showToast({ title: msg, icon })
}
export const requestToast = async <T>(
  func: () => Promise<IResData<T>>,
  options: { error?: boolean; errorTitle?: string; success?: boolean; successTitle?: string } = {
    error: true,
  },
) => {
  const { code, data, msg } = await func()
  if (code === 0 && options.success) {
    uni.showToast({
      title: options.successTitle,
      icon: 'none',
    })
  }
  if (code !== 0 && options.error) {
    uni.showToast({
      title: options.errorTitle || msg,
      icon: 'none',
    })
  }
  return { code, data, msg }
}

export const getActivityStatus = (startAt: string | number, endAt: string | number) => {
  const now = new Date()
  if (dayjs(now).isBefore(dayjs(startAt))) {
    return 'waiting'
  } else if (dayjs(now).isAfter(dayjs(startAt)) && dayjs(now).isBefore(dayjs(endAt))) {
    return 'running'
  } else {
    return 'overdue'
  }
}
export const getActivityStatusText = (startAt: string | number, endAt: string | number) =>
  ({ running: '报名中', waiting: '未开始', overdue: '已结束' })[getActivityStatus(startAt, endAt)]
export const getActivityStatusButtonText = (startAt: string | number, endAt: string | number) =>
  ({ running: '立即报名', waiting: '查看详情', overdue: '查看详情' })[
    getActivityStatus(startAt, endAt)
  ]
export const getCountsArr = (data, imgWidth, imgHeight) => {
  let r = 1
  let g = 1
  let b = 1
  // 获取所有像素的累加值
  for (let row = 0; row < imgHeight; row++) {
    for (let col = 0; col < imgWidth; col++) {
      if (row === 0) {
        r += data[imgWidth * row + col]
        g += data[imgWidth * row + col + 1]
        b += data[imgWidth * row + col + 2]
      } else {
        r += data[(imgWidth * row + col) * 4]
        g += data[(imgWidth * row + col) * 4 + 1]
        b += data[(imgWidth * row + col) * 4 + 2]
      }
    }
  }
  // 求rgb平均值
  r /= imgWidth * imgHeight
  g /= imgWidth * imgHeight
  b /= imgWidth * imgHeight
  // 四舍五入
  r = Math.round(r)
  g = Math.round(g)
  b = Math.round(b)
  return [r, g, b].join(',')
}
/**
 * 对象转二维码参数
 */
export const toQrCodeString = (type: string, options: Record<string, any>) => {
  if (!options || typeof options !== 'object') {
    return ''
  }
  let str = `${type}:`
  for (const key in options) {
    str += `${key}:${options[key]};`
  }
  return `${str};`
}
/**
 * 二维码参数转对象
 */
export const qrCodeString2Object = (str: string): Record<string, any> => {
  if (!str || typeof str !== 'string') {
    return {}
  }
  const [type, ...options] = str.split(':')
  const obj = {} as Record<string, any>
  for (const item of options.join(':').split(';' as const)) {
    const [key, value] = item.split(':')
    if (key && value) {
      obj[key] = value
    }
  }
  return { type, options: obj }
}
