import { PRE, PRE_DARK, PRE_LIGHT, Levels, WHITE, BLACK } from './type'
import { getHexColor } from '@/utils'

const html = document.documentElement

/**
 * 混合颜色
 */
const mix = (color1: string, color2: string, weight: number) => {
  weight = Math.max(Math.min(Number(weight), 1), 0)
  const r1 = parseInt(color1.substring(1, 3), 16)
  const g1 = parseInt(color1.substring(3, 5), 16)
  const b1 = parseInt(color1.substring(5, 7), 16)
  const r2 = parseInt(color2.substring(1, 3), 16)
  const g2 = parseInt(color2.substring(3, 5), 16)
  const b2 = parseInt(color2.substring(5, 7), 16)
  const r = Math.round(r1 * (1 - weight) + r2 * weight)
  const g = Math.round(g1 * (1 - weight) + g2 * weight)
  const b = Math.round(b1 * (1 - weight) + b2 * weight)
  const _r = ('0' + (r || 0).toString(16)).slice(-2)
  const _g = ('0' + (g || 0).toString(16)).slice(-2)
  const _b = ('0' + (b || 0).toString(16)).slice(-2)
  return '#' + _r + _g + _b
}

/**
 * 更换颜色的方法
 * @param color 颜色
 */
const changeTheme = (color?: string | null) => {
  if (!color) {
    console.warn('未获取到颜色的值')
    return
  }
  if(!color.includes('#')) {
    color = getHexColor(color)
  }
  // 设置主要颜色变量 --el-color-primary
  html.style.setProperty(PRE, color)
  // 循环设置色阶颜色
  // --el-color-primary-light-${level}
  Levels.forEach(level => {
    html.style.setProperty(`${PRE_LIGHT}-${level}`, mix((color as string), WHITE, level * 0.1))
  })
  // 设置主要暗色
  // --el-color-primary-dark-2
  const dark = mix(color, BLACK, 0.2)
  html.style.setProperty(`${PRE_DARK}-2`, dark)
  return color;
}

export function useElementPlusTheme(color: string) {
  changeTheme(color)
  // html.style.setProperty('--el-menu-active-color', color)
}


let themeData = ''
export function darkFontColor(dark: boolean) {
  if (dark) {
   
  } else {
    
  }

}

// 设置不同租户的主题色
export const setThemes = (themeInfo: any, isDark: boolean) => {
  // 主题色
  
}