import dayjs from 'dayjs'
import numeral from 'numeral'
import { Toast } from 'vant'
import { state as helpState } from './helpState'

export const browser = () => {
  const u = navigator.userAgent
  //   app = navigator.appVersion;
  return {
    trident: u.indexOf('Trident') > -1, //IE内核
    presto: u.indexOf('Presto') > -1, //opera内核
    webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
    gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
    mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
    ios: !!u.match(/Mac OS X/), //ios终端
    // ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
    android: u.indexOf('DAYAAPPA') > -1 || u.indexOf('Adr') > -1, //android终端
    iPhone: u.indexOf('DAYAAPPI') > -1, //是否为iPhone或者QQHD浏览器
    isApp:
      u.indexOf('DAYAAPPI') > -1 ||
      u.indexOf('DAYAAPPA') > -1 ||
      u.indexOf('Adr') > -1,
    iPad: u.indexOf('iPad') > -1, //是否iPad
    webApp: u.indexOf('Safari') == -1, //是否web应该程序，没有头部与底部
    weixin: u.indexOf('MicroMessenger') > -1, //是否微信 （2015-01-22新增）
    huawei: !!u.match(/huawei/i) || !!u.match(/honor/i),
    xiaomi: !!u.match(/mi\s/i) || !!u.match(/redmi/i) || !!u.match(/mix/i)
  }
}

export const getRandomKey = () => {
  const key = '' + new Date().getTime() + Math.floor(Math.random() * 1000000)
  return key
}

/**
 * 删除token
 */
export const removeAuth = () => {
  sessionStorage.removeItem('Authorization')
}

/**
 * 设置token
 * @param token
 * @returns {void}
 */
export const setAuth = (token: any) => {
  sessionStorage.setItem('Authorization', token)
}

/**
 * 获取token
 */
export const getAuth = () => {
  sessionStorage.getItem('Authorization')
}

/**
 * 开始加载
 */
export const openLoading = () => {
  if (helpState.loadingCount === 0) {
    helpState.loadingCount++
    Toast.loading({
      message: '加载中...',
      forbidClick: true,
      loadingType: 'spinner',
      duration: 0
    })
  }
}

/**
 * 关闭加载
 */
export const closeLoading = () => {
  // console.log(helpState.loadingCount, +new Date());
  if (helpState.loadingCount <= 0) return
  setTimeout(() => {
    helpState.loadingCount--
    if (helpState.loadingCount === 0) {
      Toast.clear()
    }
  }, 200)
}

export const getWeekCh = (week: number, type = 0) => {
  const template = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
  const template2 = [
    '星期天',
    '星期一',
    '星期二',
    '星期三',
    '星期四',
    '星期五',
    '星期六'
  ]
  return type ? template2[week] : template[week]
}

export const formatterDate = (type: string, val: any) => {
  if (type === 'year') {
    return `${val}年`
  }
  if (type === 'month') {
    return `${val}月`
  }
  if (type === 'day') {
    return `${val}日`
  }
  if (type === 'hour') {
    return `${val}时`
  }
  if (type === 'minute') {
    return `${val}分`
  }
  return val
}

export const numberFormat = (num: number, type?: string) => {
  if (type === 'percent') {
    return numeral(num).format('0.0%')
  }
  return numeral(num).format('0,0')
}

export const moneyFormat = (value: number, format = '0,0.00') => {
  return numeral(value).format(format)
}
export const dateFormat = (
  value: string | Date,
  format = 'YYYY-MM-DD HH:mm:ss'
) => {
  return dayjs(value).format(format)
}

// 学生地址
export function validStudentUrl() {
  const url = window.location.href
  let returnUrl = ''
  if (/test/.test(url)) {
    // test环境
    returnUrl = 'http://mstutest.dayaedu.com'
  } else if (/dev/.test(url)) {
    // dev 环境
    returnUrl = 'http://mstudev.dayaedu.com'
  } else if (/online/.test(url)) {
    //线上
    returnUrl = 'https://mstuonline.dayaedu.com'
  } else {
    // 默认dev环境
    returnUrl = 'http://mstutest.dayaedu.com'
    // returnUrl = 'http://192.168.3.120:9001'
  }
  return returnUrl
}
