/**
 * 设备相关工具函数
 */

/**
 * 获取屏幕分辨率
 */
export function getScreenResolution() {
  return {
    width: window.screen.width,
    height: window.screen.height,
    availWidth: window.screen.availWidth,
    availHeight: window.screen.availHeight
  }
}

/**
 * 获取视口尺寸
 */
export function getViewportSize() {
  return {
    width: window.innerWidth,
    height: window.innerHeight
  }
}

/**
 * 获取设备信息
 */
export function getDeviceInfo() {
  const ua = navigator.userAgent

  const info = {
    platform: navigator.platform,
    language: navigator.language,
    userAgent: ua,
    isMobile: /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(ua),
    isTouch: 'ontouchstart' in window || navigator.maxTouchPoints > 0,
    screenWidth: window.screen.width,
    screenHeight: window.screen.height
  }

  return info
}

/**
 * 获取网络状态
 */
export function getNetworkStatus() {
  const connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection
  if (connection) {
    return {
      effectiveType: connection.effectiveType,
      downlink: connection.downlink,
      rtt: connection.rtt,
      saveData: connection.saveData
    }
  }
  return {
    online: navigator.onLine
  }
}

/**
 * 防抖函数
 */
export function debounce(fn, delay = 300) {
  let timer = null
  return function (...args) {
    if (timer) clearTimeout(timer)
    timer = setTimeout(() => {
      fn.apply(this, args)
    }, delay)
  }
}

/**
 * 节流函数
 */
export function throttle(fn, delay = 300) {
  let last = 0
  return function (...args) {
    const now = Date.now()
    if (now - last >= delay) {
      last = now
      fn.apply(this, args)
    }
  }
}

/**
 * 深拷贝
 */
export function deepClone(obj) {
  if (obj === null || typeof obj !== 'object') return obj
  if (Array.isArray(obj)) {
    return obj.map(item => deepClone(item))
  }
  const clone = {}
  for (const key in obj) {
    if (obj.hasOwnProperty(key)) {
      clone[key] = deepClone(obj[key])
    }
  }
  return clone
}

/**
 * 生成唯一ID
 */
export function generateId(prefix = '') {
  const id = Date.now().toString(36) + Math.random().toString(36).substring(2)
  return prefix ? `${prefix}_${id}` : id
}

/**
 * 格式化文件大小
 */
export function formatFileSize(bytes) {
  if (bytes === 0) return '0 B'
  const k = 1024
  const sizes = ['B', 'KB', 'MB', 'GB', 'TB']
  const i = Math.floor(Math.log(bytes) / Math.log(k))
  return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]
}

/**
 * 手机号脱敏
 */
export function maskMobile(mobile) {
  if (!mobile || mobile.length !== 11) return mobile
  return mobile.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2')
}

/**
 * 身份证号脱敏
 */
export function maskIdCard(idCard) {
  if (!idCard || idCard.length < 10) return idCard
  return idCard.replace(/(\d{6})\d{8}(\d{4})/, '$1********$2')
}

/**
 * 随机颜色
 */
export function randomColor() {
  const colors = [
    '#2f8ee5', '#20b7c7', '#10b981', '#f59e0b',
    '#ef4444', '#8b5cf6', '#ec4899', '#06b6d4'
  ]
  return colors[Math.floor(Math.random() * colors.length)]
}

/**
 * 睡眠函数
 */
export function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms))
}

export default {
  getScreenResolution,
  getViewportSize,
  getDeviceInfo,
  getNetworkStatus,
  debounce,
  throttle,
  deepClone,
  generateId,
  formatFileSize,
  maskMobile,
  maskIdCard,
  randomColor,
  sleep
}
