import { useRootSetting } from '/@/hooks/setting/useRootSetting';
// import { getThemeColors, generateColors } from '../../../build/config/themeConfig';

// import { replaceStyleVariables } from 'vite-plugin-theme/es/client';
// import { mixLighten, mixDarken, tinycolor } from 'vite-plugin-theme/es/colorUtils';
import { ThemeEnum } from '/@/enums/appEnum';
/** rgb转hex */
function rgbToHex(a, b, c) {
  const hexs = [a.toString(16), b.toString(16), c.toString(16)];
  for (let i = 0; i < 3; i++) {
    if (hexs[i].length == 1) hexs[i] = `0${hexs[i]}`;
  }
  return `#${hexs.join('')}`;
}
/** hex转rgb */
function hexToRgb(str) {
  const hxs = str.replace('#', '').match(/../g);
  for (let i = 0; i < 3; i++) hxs[i] = parseInt(hxs[i], 16);
  return hxs;
}

/** 加深颜色值 */
export function mixDarken(color, level) {
  const rgbc = hexToRgb(color);
  for (let i = 0; i < 3; i++) rgbc[i] = Math.floor(rgbc[i] * (1 - level));
  return rgbToHex(rgbc[0], rgbc[1], rgbc[2]);
}

/** 变浅颜色值 */
export function mixLighten(color, level) {
  const rgbc = hexToRgb(color);
  for (let i = 0; i < 3; i++) rgbc[i] = Math.floor((255 - rgbc[i]) * level + rgbc[i]);
  return rgbToHex(rgbc[0], rgbc[1], rgbc[2]);
}

function setPropertyPrimary(i: number, color: string) {
  const { getDarkMode } = useRootSetting();
  const colorVal = getDarkMode.value == ThemeEnum.DARK ? mixDarken(color, i / 10) : mixLighten(color, i / 10);
  console.log('🚀 ~ file: index.ts:39 ~ setPropertyPrimary ~ getDarkMode.value:', getDarkMode.value);
  document.documentElement.style.setProperty(`--ant-primary-${i}`, colorVal);
  if (i == 3 && getDarkMode.value == ThemeEnum.LIGHT) {
    document.documentElement.style.setProperty(`--ant-primary-color-hover`, colorVal);
    document.documentElement.style.setProperty(`--ant-primary-color-active`, colorVal);
  }
  if (i == 5 && getDarkMode.value == ThemeEnum.DARK) {
    document.documentElement.style.setProperty(`--ant-primary-color-hover`, colorVal);
    document.documentElement.style.setProperty(`--ant-primary-color-active`, colorVal);
  }
}

export async function changeTheme(color: string) {
  document.documentElement.style.setProperty('--ant-primary-color', color);
  for (let i = 1; i <= 9; i++) {
    setPropertyPrimary(i, color);
  }
  return '';
  // const colors = generateColors({
  //   mixDarken,
  //   mixLighten,
  //   tinycolor,
  //   color,
  // });

  // return await replaceStyleVariables({
  //   colorVariables: [...getThemeColors(color), ...colors],
  // });
}
