import { resolveSize, type ThemeConfig, type ThemeContext, type ThemePaddingOrMarginType } from "./ThemeDefine";

export type StringSize = number | string;

export function solveUrl(v: string) {
  if (v.startsWith("url("))
    return v;
  return `url(${v})`;
}
// #ifdef H5
const info = {
  screenWidth: globalThis.window.innerWidth,
  screenHeight: globalThis.window.innerHeight,
}
// #endif
// #ifndef H5
const info = uni.getWindowInfo() ;
// #endif

export const screenWidth = info.screenWidth;
export const screenHeight = info.screenHeight;

export function rpx2px(rpx: number) {
  return rpx / 750 * screenWidth;
} 

export function configMargin(style: Record<string, any>, theme: ThemeContext, input: ThemePaddingOrMarginType|undefined) {
  configMarginOrStyle('margin', style, theme, input);
}
export function configPadding(style: Record<string, any>, theme: ThemeContext, input: ThemePaddingOrMarginType|undefined) {
  configMarginOrStyle('padding', style, theme, input);
}
function configMarginOrStyle(name: string, style: Record<string, any>, theme: ThemeContext, input: ThemePaddingOrMarginType|undefined) {
  if (input === undefined)
    return;
  if (input instanceof Array) {
    style[name] = undefined;
    if (input.length === 2) {
      style[name] = `${theme.resolveThemeSize(input[0] ?? 0)} ${theme.resolveThemeSize(input[1] ?? 0)}`;
    } else if (input.length === 4) {
      style[name] = `${theme.resolveThemeSize(input[0] ?? 0)} ${theme.resolveThemeSize(input[1] ?? 0)} ${theme.resolveThemeSize(input[2] ?? 0)} ${theme.resolveThemeSize(input[3] ?? 0)}`;
    }
  } else if (typeof input === 'object') {
    style[name] = `${theme.resolveThemeSize(input.t ?? 0)} ${theme.resolveThemeSize(input.r ?? 0)} ${theme.resolveThemeSize(input.b ?? 0)} ${theme.resolveThemeSize(input.l ?? 0)}`;
  } else {
    style[name] = theme.resolveThemeSize(input);
  } 
}

export interface DynamicVarType {
  name: string,
  defaultValue: any,
  name2?: string,
  defaultValue2?: any,
  type: 'var'|'color'|'size'|'size2',
}

export function DynamicVar(name: string, defaultValue?: any) : DynamicVarType {
  return {
    name,
    defaultValue,
    type: 'var',
  }
}
export function DynamicColor(name: string, defaultValue?: string) : DynamicVarType {
  return {
    name,
    defaultValue,
    type: 'color',
  }
}
export function DynamicSize(name: string, defaultValue?: StringSize) : DynamicVarType {
  return {
    name,
    defaultValue,
    type: 'size',
  }
}
export function DynamicSize2(name1: string, name2: string, defaultValue1?: StringSize, defaultValue2?: StringSize) : DynamicVarType {
  return {
    name: name1,
    name2,
    defaultValue: defaultValue1,
    defaultValue2,
    type: 'size2',
  }
}

/**
 * 通过字符串参数选择指定类型的样式对象
 * @param type 字符串参数
 * @param styleObject 样式对象，子对象key是名称。
 */
export function selectStyleType<T, K extends string>(type: K|undefined, defaultTypeName: K, styleObject: { [P in K]: T|(() => T) }) {
  const result = type ? styleObject[type] : styleObject[defaultTypeName];
  return typeof result === 'function' ? result() : result;
}

/**
 * 通过字符串参数选择指定类型的对象
 * @param type 字符串参数
 * @param object 对象，子对象key是名称。
 */
export function selectObjectByType<T, K extends string>(type: K|undefined, defaultTypeName: K, object: { [index: string]: T }) {
  return type ? object[type] : object[defaultTypeName];
}