import dayjs from 'dayjs'
import numeral from 'numeral'
import Cookies from 'js-cookie'
import { state as helpState } from './helpState'
import mitt from '@/helpers/mitt'
// import { platform } from 'os'
import EventEmitter from 'eventemitter3';

export const eventGlobal = new EventEmitter();


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('COLEXIUAPPA') > -1 || u.indexOf('Adr') > -1, //android终端
    iPhone: u.indexOf('COLEXIUAPPI') > -1, //是否为iPhone或者QQHD浏览器
    isApp:
      u.indexOf('COLEXIUAPPI') > -1 ||
      u.indexOf('COLEXIUAPPA') > -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
}
/**
 * 设置用户信息
 */
export const setUserInfo = (userInfo: any) => {
  Cookies.set('userInfo', userInfo, { expires: 7 })
}
/**
 * 删除token
 */
export const removeAuth = () => {
  Cookies.remove('token')
  Cookies.remove('userInfo')
}

/**
 * 设置token
 * @param token
 * @returns {void}
 */
export const setAuth = (token: any) => {
  // 7天过期
  Cookies.set('token', token, { expires: 7 })
}

/**
 * 获取用户信息
 */
export const getUserInfo = () => {
  let userInfo = Cookies.get('userInfo')
  userInfo = userInfo ? JSON.parse(userInfo) : {}
  return userInfo || null
}
/**
 * 获取token
 */
export const getAuth = () => {
  let token = Cookies.get('token')
  token = token ? JSON.parse(token) : {}
  return token.token || null
}

/**
 * 获取登录用户类型
 * @returns {string}
 */
export const getUserType = () => {
  let token = Cookies.get('token')
  token = token ? JSON.parse(token) : {}
  return token.loginUserType || null
}

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

export function vaildTeachingUrl() {
  const url = window.location.href
  let returnUrl = ''
  if (/dev/.test(url)) {
    //线上
    returnUrl = 'https://dev.colexiu.com'
  } else if (/ponline/.test(url)) {
    returnUrl = 'https://ponline.colexiu.com'
  } else if (/online/.test(url)) {
    // dev 环境
    returnUrl = 'https://online.colexiu.com'
  } else if (/test/.test(url)) {
    // dev 环境
    returnUrl = 'https://dev.colexiu.com'
  } else {
    // 默认dev环境
    returnUrl = 'https://dev.colexiu.com'
  }
  return returnUrl
}
// 获取当前连接
export const getBaseUrl = (platform?: string) => {
  const url = window.location.origin
  const pathname = platform || window.location.pathname
  return `${url}${pathname}`
}

export const getCodeBaseUrl = (platform?: string) => {
  let url = window.location.origin
  if (url.indexOf('https://colexiu.com') > -1) {
    url = 'https://online.colexiu.com'
  } else {
    url = url.replace('www.', 'online.')
  }

  const pathname = platform || window.location.pathname
  return `${url}${pathname}`
}
