// 显式导入所有电路元件图片，并定义其基本属性

import battery from './circuit-ele/battery.png'
import powerSupply from './circuit-ele/power_supply.png'
import capacitor from './circuit-ele/capacitor.png'
import inductor from './circuit-ele/inductor.png'
import lightBulb from './circuit-ele/light_bulb.png'
import lightBulbGrow from './circuit-ele/light_bulb_grow.png'
import resistor from './circuit-ele/resistor.png'
import resistanceBox from './circuit-ele/resistance_box.png'
import switchOff from './circuit-ele/switch_off.png'
import switchOn from './circuit-ele/switch_on.png'
import meter from './circuit-ele/meter.png'
import slidingRheostat from './circuit-ele/sliding_rheostat.png'
import slidingRheostatPin from './circuit-ele/sliding_rheostat_pin.png'
import textBox from './circuit-ele/text_box.svg'

export type ConnectionPoint = {
    // 以百分比（0..1）定义在图片盒子内的位置
    x: number
    y: number
    name?: string
}

export type CircuitElement = {
    key: string
    name: string
    url: string
    defaultSize?: number // 渲染尺寸（像素），可选
    connectionPoints: ConnectionPoint[]
    propertySchemas?: PropertySchema[]
    stateImages?: { on: string; off: string }
    // 预设：一键应用一组属性值
    preset?: Preset[]
    // 可选：叠加图片（例如滑动变阻器的滑块）
    pinUrl?: string
}

export type PropertySchema =
    | {
        key: string
        label: string
        type: 'number'
        unit?: string
        default: number
    }
    | {
        key: string
        label: string
        type: 'text'
        default: string
    }
    | {
        key: string
        label: string
        type: 'color'
        default: string
    }
    | {
        key: string
        label: string
        type: 'select'
        options: Array<{ label?: string; value: string }> | string[]
        default: string
    }

// 预设定义：展示名称 + 要覆盖的属性值
export type Preset = {
    name: string
    propertyValues: Record<string, number | string>
}

export const elements: CircuitElement[] = [
    {
        key: 'text_box',
        name: '文本框',
        url: textBox,
        defaultSize: 0,
        // 文本框不参与电路连接，无端点
        connectionPoints: [],
        propertySchemas: [
            { key: 'text', label: '文本', type: 'text', default: '双击编辑' },
            { key: 'fontSize', label: '字体大小', type: 'number', unit: 'px', default: 24 },
            { key: 'color', label: '颜色', type: 'color', default: '#111827' },
            // 可选：是否粗体/对齐方式等，后续再加
        ],
    },
    {
        key: 'battery',
        name: '电池',
        url: battery,
        defaultSize: 110,
        connectionPoints: [
            { x: 0.95, y: 0.5, name: '正极' }, // 右侧
            { x: 0.05, y: 0.5, name: '负极' }, // 左侧
        ],
        propertySchemas: [
            { key: 'voltage', label: '电压', type: 'number', unit: 'V', default: 3 },
            { key: 'internalResistance', label: '内阻', type: 'number', unit: 'Ω', default: 0.5 },
        ],
    },
    {
        key: 'power_supply',
        name: '电源',
        url: powerSupply,
        defaultSize: 180,
        connectionPoints: [
            { x: 0.8, y: 1, name: '正极' },
            { x: 0.2, y: 1, name: '负极' },
        ],
        propertySchemas: [
            { key: 'voltage', label: '电压', type: 'number', unit: 'V', default: 5 },
            { key: 'internalResistance', label: '内阻', type: 'number', unit: 'Ω', default: 0.1 },
        ],
    },
    {
        key: 'switch',
        name: '开关',
        url: switchOff,
        defaultSize: 130,
        connectionPoints: [
            { x: 0.05, y: 0.7, name: 'A' },
            { x: 0.95, y: 0.7, name: 'B' },
        ],
        // 点击元件切换 on/off 显示
        stateImages: { on: switchOn, off: switchOff },
        propertySchemas: [
            { key: 'state', label: '状态', type: 'select', options: ['off', 'on'], default: 'off' },
        ],
    },
    {
        key: 'light_bulb',
        name: '灯泡',
        url: lightBulb,
        defaultSize: 80,
        connectionPoints: [
            { x: 0.5, y: 1, name: 'A' },
            { x: 0.72, y: 0.8, name: 'B' },
        ],
        propertySchemas: [
            { key: 'resistance', label: '电阻', type: 'number', unit: 'Ω', default: 20 },
        ],
        // 当电流超过阈值时在前端根据 state 切换图片
        stateImages: { on: lightBulbGrow, off: lightBulb },
    },
    {
        key: 'sliding_rheostat',
        name: '滑动变阻器',
        url: slidingRheostat,
        pinUrl: slidingRheostatPin,
        defaultSize: 180,
        connectionPoints: [
            { x: 0.15, y: 0.28, name: '下部左侧' },
            { x: 0.85, y: 0.28, name: '下部右侧' },
            { x: 0.15, y: 0.76, name: '上部左侧' },
            { x: 0.85, y: 0.76, name: '上部右侧' },
        ],
        propertySchemas: [
            { key: 'maxResistance', label: '最大电阻', type: 'number', unit: 'Ω', default: 100 },
            { key: 'position', label: '滑块位置', type: 'number', default: 0.5 },
        ],
    },
    {
        key: 'resistor',
        name: '电阻',
        url: resistor,
        defaultSize: 110,
        connectionPoints: [
            { x: 0.05, y: 0.5, name: 'A' },
            { x: 0.95, y: 0.5, name: 'B' },
        ],
        propertySchemas: [
            { key: 'resistance', label: '电阻', type: 'number', unit: 'Ω', default: 5 },
        ],
    },
    {
        key: 'resistance_box',
        name: '电阻箱',
        url: resistanceBox,
        defaultSize: 180,
        connectionPoints: [
            { x: 0.1, y: 1, name: 'A' },
            { x: 0.9, y: 1, name: 'B' },
        ],
        propertySchemas: [
            { key: 'resistance', label: '电阻', type: 'number', unit: 'Ω', default: 100 },
        ],
    },
    {
        key: 'meter',
        name: '表',
        url: meter,
        defaultSize: 150,
        connectionPoints: [
            { x: 0.25, y: 1, name: '正极' },
            { x: 0.45, y: 1, name: '负极' },
        ],
        preset: [{
            name: '微安电流表', propertyValues: { resistance: 0.05, renderFunc: '(i) => `${(i * 1000000).toFixed(0)} uA`' }
        }, {
            name: '毫安电流表', propertyValues: { resistance: 0.5, renderFunc: '(i) => `${(i * 1000).toFixed(1)} mA`' }
        }, {
            name: '电流表', propertyValues: { resistance: 0.1, renderFunc: '(i) => `${(i).toFixed(2)} A`' }
        }, {
            name: '电压表', propertyValues: { resistance: 10000000, renderFunc: '(i, r) => `${(i * r).toFixed(2)} V`' }
        }],
        propertySchemas: [
            { key: 'resistance', label: '电阻', type: 'number', unit: 'Ω', default: 0.1 },
            { key: 'renderFunc', label: '显示函数', type: 'text', default: '(i) => `${(i).toFixed(2)} A`' },
        ],
    },
    {
        key: 'capacitor',
        name: '电容',
        url: capacitor,
        defaultSize: 120,
        connectionPoints: [
            { x: 0.4, y: 0.9, name: '正极' },
            { x: 0.6, y: 0.96, name: '负极' },
        ],
        propertySchemas: [
            { key: 'capacitance', label: '电容', type: 'number', unit: 'F', default: 0.000001 },
        ],
    },
    {
        key: 'inductor',
        name: '电感',
        url: inductor,
        defaultSize: 100,
        connectionPoints: [
            { x: 0.05, y: 0.5, name: 'A' },
            { x: 0.95, y: 0.5, name: 'B' },
        ],
        propertySchemas: [
            { key: 'inductance', label: '电感', type: 'number', unit: 'H', default: 0.001 },
        ],
    },
]
