import { resolveSize, type ThemeConfig, type ThemePaddingMargin } 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.getSystemInfoSync() ;
// #endif

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

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

export function configThemePaddingMarginOneSide(input: string|number|number[]|ThemePaddingMargin|undefined, controlType: 'vertical'|'horizontal', controlValue: number) : ThemePaddingMargin {
  
  const result = { l: 0, r: 0, t: 0, b: 0 };
  if (!input) {
    if (controlType == 'horizontal')
      result.l = result.r = controlValue;
    else
      result.t = result.b = controlValue;
    return result;
  }

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

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];
}