import { Toast } from 'vant'
import i18n from '@/lang'

export function copyText(text, msg) {
  var oInput = document.getElementById('copy-input')
  oInput.value = text
  oInput.select() // 选择对象;
  document.execCommand('Copy') // 执行浏览器复制命令
  Toast({
    message: msg || i18n.t('common.l39'),
  })
}

export function isPc(){
  if(window.navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)) {
    return true; // 移动端
  }else{
    return false; // PC端
  }
}

export function deepCopy(obj) {
  if (typeof obj !== 'object' || obj === null) {
      return obj;
  }
  var newObj = Array.isArray(obj) ? [] : {};
  for (var key in obj) {
      newObj[key] = deepCopy(obj[key]);
  }
  return newObj;
}

export function parseTime(time, cFormat) {
  if (arguments.length === 0) {
    return null
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  var date
  if (typeof time === 'object') {
    date = time
  } else {
    if (('' + time).length === 10) {
      time = parseInt(time) * 1000
    }
    date = new Date(parseInt(time))
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    var value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
    if (result.length > 0 && value < 10) {
      value = '0' + value
    }
    return value || 0
  })
  return time_str
}

export function numToThousand(num) {
  if (!num || isNaN(Number(num))) {
    return '0.00'
  }
  var str = (handleDigits(Number(num)) + '').split('.');
  var reg = /(?=(?!\b)(\d{3})+$)/g
  var data = str[0].replace(reg, ',')
  if (str[1]) {
    data += ('.' + str[1])
  }
  return data
}

export function handleDigits(value, digits = 2) {
  if (!value || isNaN(Number(value))) {
    return '0.00'
  }
  var arr = value.toString().split('.')
  var str = arr[1]
  if (str && str.length > digits) {
    str = str.slice(0, digits)
  }
  var length = str ? (digits - str.length) : digits
  var z = ''
  for (var i = 0; i < length; i++) {
    z += '0'
  }
  return `${arr[0]}.${str || ''}${z}`
}

export function handleDigits2(value) {
  if (value === undefined || isNaN(value)) {
    return '0.00'
  }
  return handleDigits(value)
}

export function getTradeState(timeList, time) {
    var now = new Date(time);
    // 获取北京时区的时间字符串
    var beijingTime = now.toLocaleString('en-US', { timeZone: 'Asia/Shanghai' });
    // 将北京时间字符串转换为日期对象
    var beijingDate = new Date();
    var year = beijingDate.getFullYear()
    var m = beijingDate.getMonth() + 1
    if (m < 10) {
      m = '0' + m
    }
    var d = beijingDate.getDate()
    if (d < 10) {
      d = '0' + d
    }
    var week = beijingDate.getDay()
    // 获取北京时间的时间戳
    var beijingTimestamp = beijingDate.getTime();
    var sign = false
    timeList.forEach(item => {
      if (item.week == week) {
        if (new Date(`${year}/${m}/${d} ${item.start_time}:00`).getTime() < beijingTimestamp && beijingTimestamp < new Date(`${year}/${m}/${d} ${item.end_time}:00`).getTime()) {
          sign = true
        }
      }
    })
    if (sign) {
      return '盘中'
    } else {
      return '已休盘'
    }
}

export function getAreaCode() {
  var code = localStorage.getItem('areaCode')
  if (code) {
    return JSON.parse(code)
  }
  var obj = {
    'en': {
      code: '84',
      country: 'vn'
    },
    'zh-CN': {
      code: '86',
      country: 'cn'
    },
    'zh-TW': {
      code: '886',
      country: 'tw'
    },
    'ru': {
      code: '7',
      country: 'ru'
    },
    'es': {
      code: '34',
      country: 'es'
    },
    'fr': {
      code: '33',
      country: 'fr'
    },
    'vi': {
      code: '84',
      country: 'vn'
    },
    'de': {
      code: '49',
      country: 'de'
    },
    'tr': {
      code: '90',
      country: 'tr'
    },
    'pt': {
      code: '351',
      country: 'pt'
    },
    'ko': {
      code: '82',
      country: 'kr'
    },
    'id': {
      code: '62',
      country: 'id'
    },
    'ja': {
      code: '81',
      country: 'jp'
    },
    'th': {
      code: '66',
      country: 'th'
    },
  }
  return obj[navigator.language] || obj['en']
}