import Bar from './Bar.vue'
import icon from '../assets/icon/icon_10bar.svg'
import type { IComponentModelConfig } from '../type'
import i18n from '@/locales'
import { flagOptions, stateOptions, stateList, DEFAULT_THEME_KEY } from '@/constants'
import defaultStyle from './style.json'

export default {
  label: i18n.global.t('bar'),
  icon,
  component: Bar,
  key: 'lv_bar',
  group: i18n.global.t('basic'),
  sort: 1,
  defaultStyle,
  parts: [
    {
      name: 'main',
      stateList
    },
    {
      name: 'indicator',
      stateList
    }
  ],
  defaultSchema: {
    name: 'bar',
    props: {
      x: 0,
      y: 0,
      width: 150,
      height: 20,
      flags: [
        'LV_OBJ_FLAG_CLICKABLE',
        'LV_OBJ_FLAG_CLICK_FOCUSABLE',
        'LV_OBJ_FLAG_SCROLL_ELASTIC',
        'LV_OBJ_FLAG_SCROLL_MOMENTUM',
        'LV_OBJ_FLAG_SCROLL_CHAIN_HOR',
        'LV_OBJ_FLAG_SCROLL_CHAIN_VER',
        'LV_OBJ_FLAG_SCROLL_CHAIN',
        'LV_OBJ_FLAG_SCROLL_WITH_ARROW',
        'LV_OBJ_FLAG_SNAPPABLE',
        'LV_OBJ_FLAG_PRESS_LOCK',
        'LV_OBJ_FLAG_GESTURE_BUBBLE'
      ],
      states: [],
      min: 0,
      max: 100,
      value: 50,
      animationTime: 1000,
      animation: false,
      mode: 'normal',
      startValue: 20,
      direction: 'left'
    },
    styles: [
      {
        part: {
          name: 'main',
          state: 'default'
        },
        theme: DEFAULT_THEME_KEY,
        background: {
          color: '#2092f53c',
          image: {
            imgId: '',
            recolor: '',
            alpha: 255
          }
        },
        border: {
          color: '#2092f5ff',
          width: 0,
          radius: 10,
          side: ['all']
        },
        outline: {
          color: '#000000ff',
          width: 0,
          pad: 0
        },
        padding: {
          left: 0,
          right: 0,
          top: 0,
          bottom: 0
        },
        shadow: {
          color: '#2092f5ff',
          offsetX: 0,
          offsetY: 0,
          spread: 0,
          width: 0
        },
        transform: {
          width: 0,
          height: 0,
          translateX: 0,
          translateY: 0,
          originX: 0,
          originY: 0,
          rotate: 0,
          scale: 256
        }
      }
    ]
  },
  config: {
    // 组件属性
    props: [
      {
        label: '名称',
        field: 'name',
        valueType: 'text',
        componentProps: {
          placeholder: '请输入名称',
          type: 'text'
        }
      },
      {
        label: '位置',
        valueType: 'group',
        children: [
          {
            field: 'props.x',
            valueType: 'number',
            componentProps: {
              span: 12
            },
            slots: { prefix: 'X' },
            canUseEventSet: true
          },
          {
            field: 'props.y',
            valueType: 'number',
            componentProps: {
              span: 12
            },
            slots: { prefix: 'Y' },
            canUseEventSet: true
          },
          {
            field: 'props.width',
            valueType: 'number',
            componentProps: {
              span: 12
            },
            slots: { prefix: 'W' },
            canUseEventSet: true
          },
          {
            field: 'props.height',
            valueType: 'number',
            componentProps: {
              span: 12
            },
            slots: { prefix: 'H' },
            canUseEventSet: true
          }
        ]
      },
      {
        label: '标识',
        field: 'props.flags',
        valueType: 'checkbox',
        componentProps: {
          options: flagOptions,
          defaultCollapsed: true
        },
        canUseEventSet: true
      },
      {
        label: '状态',
        field: 'props.states',
        valueType: 'checkbox',
        componentProps: {
          options: stateOptions,
          defaultCollapsed: true
        },
        canUseEventSet: true
      }
    ],
    coreProps: [
      {
        label: '范围',
        valueType: 'group',
        canUseEventSet: true,
        children: [
          {
            field: 'props.min',
            valueType: 'number',
            componentProps: {
              span: 12,
              min: -10000,
              max: 10000
              // onValueChange: (val, formData) => {
              //   if (val >= formData.props.max) {
              //     formData.props.min = formData.props.max - 1
              //   }
              //   if (formData.props.mode === 'symmetrical' && val >= 0) {
              //     formData.props.min = -1
              //   }
              // }
            },
            slots: { prefix: 'S' }
          },
          {
            field: 'props.max',
            valueType: 'number',
            componentProps: {
              span: 12,
              min: -10000,
              max: 10000
              // onValueChange: (val, formData) => {
              //   if (val <= formData.props.min) {
              //     formData.props.max = formData.props.min + 1
              //   }
              //   if (formData.props.mode === 'symmetrical' && val <= 0) {
              //     formData.props.min = 1
              //   }
              // }
            },
            slots: { prefix: 'E' } as any
          }
        ]
      },
      {
        valueType: 'dependency',
        name: ['props.min', 'props.max'],
        canUseEventSet: true,
        dependency: (dependency) => {
          const min = dependency['props.min']
          const max = dependency['props.max']
          return [
            {
              label: '当前值',
              field: 'props.value',
              valueType: 'number',
              componentProps: {
                min: Math.min(min, max),
                max: Math.max(min, max)
              }
            }
          ]
        }
      },
      {
        label: '动画',
        valueType: 'group',
        canUseEventSet: true,
        children: [
          {
            label: '',
            field: 'props.animation',
            valueType: 'switch',
            componentProps: {
              span: 6
            }
          },
          {
            valueType: 'dependency',
            name: ['props.animation'],
            dependency: (dependency) => {
              return [
                {
                  label: '',
                  field: 'props.animationTime',
                  valueType: 'number',
                  componentProps: {
                    span: 18,
                    min: 0,
                    max: 100000,
                    disabled: !dependency['props.animation']
                  },
                  slots: { prefix: 'Time' }
                }
              ]
            }
          }
        ]
      },
      {
        label: '模式',
        field: 'props.mode',
        valueType: 'select',
        componentProps: {
          options: [
            { label: 'Normal', value: 'normal' },
            { label: 'Symmetrical', value: 'symmetrical' },
            { label: 'Range', value: 'range' }
          ]
        },
        canUseEventSet: true
      },
      {
        valueType: 'dependency',
        name: ['props.mode', 'props.min', 'props.max'],
        dependency: (dependency) => {
          const min = dependency['props.min']
          const max = dependency['props.max']
          return dependency['props.mode'] === 'range'
            ? [
                {
                  label: '开始值',
                  field: 'props.startValue',
                  valueType: 'number',
                  componentProps: {
                    min: Math.min(min, max),
                    max: Math.max(min, max)
                  },
                  canUseEventSet: true
                }
              ]
            : []
        }
      }
    ],
    // 组件样式
    styles: [
      {
        label: '模块/状态',
        field: 'part',
        valueType: 'part'
      },
      {
        valueType: 'dependency',
        name: ['part'],
        dependency: ({ part }) => {
          return part?.name === 'main'
            ? [
                {
                  label: '背景',
                  field: 'background',
                  valueType: 'background'
                },
                {
                  label: '边框',
                  field: 'border',
                  valueType: 'border'
                },
                {
                  label: '轮廓',
                  field: 'outline',
                  valueType: 'outline'
                },
                {
                  label: '内边距',
                  field: 'padding',
                  valueType: 'padding'
                },
                {
                  label: '阴影',
                  field: 'shadow',
                  valueType: 'shadow'
                },
                {
                  label: '变换',
                  field: 'transform',
                  valueType: 'transform'
                }
              ]
            : [
                {
                  label: '背景',
                  field: 'background',
                  valueType: 'background'
                },
                {
                  label: '边框',
                  field: 'border',
                  valueType: 'border'
                },
                {
                  label: '轮廓',
                  field: 'outline',
                  valueType: 'outline'
                },
                {
                  label: '阴影',
                  field: 'shadow',
                  valueType: 'shadow'
                }
              ]
        }
      }
    ]
  }
} as IComponentModelConfig
