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

export default {
  label: i18n.global.t('chart'),
  icon,
  component: Chart,
  key: 'lv_chart',
  group: i18n.global.t('display'),
  sort: 1,
  defaultStyle,
  parts: [
    {
      name: 'main',
      stateList
    }
  ],
  defaultSchema: {
    name: 'chart',
    props: {
      x: 0,
      y: 0,
      width: 205,
      height: 155,
      flags: [
        'LV_OBJ_FLAG_CLICKABLE',
        'LV_OBJ_FLAG_CLICK_FOCUSABLE',
        'LV_OBJ_FLAG_SCROLLABLE',
        '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: [],
      scrollbar: 'off',
      column: 4,
      row: 4,
      chart_type: 'line',
      hide_line_points: false,
      axis_y: {
        min: 0,
        max: 100
      },
      // 副Y轴
      open_axis_y2: false,
      axis_y2: {
        min: 0,
        max: 100
      },
      chart_data: [
        {
          name: 'data_0',
          chart_axis: 'y',
          color: '#000000ff',
          points: [10, 20, 30, 40, 50]
        }
      ]
    },
    styles: [
      {
        part: {
          name: 'main',
          state: 'default'
        },
        theme: DEFAULT_THEME_KEY,
        background: {
          color: '#ffffffff',
          image: {
            imgId: '',
            recolor: '#ffffff00',
            alpha: 255
          }
        },
        border: {
          color: '#e0e0e0ff',
          width: 0,
          radius: 6,
          side: ['all']
        },
        line: {
          color: '#e0e0e0ff',
          width: 2,
          // 虚线宽度
          dashWidth: 0,
          // 虚线间隔
          dashGap: 0
        },
        shadow: {
          color: '#000000ff',
          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: [
          {
            label: '',
            field: 'props.x',
            valueType: 'number',
            componentProps: {
              span: 12,
              min: -10000,
              max: 10000
            },
            slots: { prefix: 'X' },
            canUseEventSet: true
          },
          {
            label: '',
            field: 'props.y',
            valueType: 'number',
            componentProps: {
              span: 12,
              min: -10000,
              max: 10000
            },
            slots: { prefix: 'Y' },
            canUseEventSet: true
          },
          {
            label: '',
            field: 'props.width',
            valueType: 'number',
            componentProps: {
              span: 12,
              min: 1,
              max: 10000
            },
            slots: { prefix: 'W' },
            canUseEventSet: true
          },
          {
            label: '',
            field: 'props.height',
            valueType: 'number',
            componentProps: {
              span: 12,
              min: 1,
              max: 10000
            },
            slots: { prefix: 'H' },
            canUseEventSet: true
          }
        ]
      },
      {
        label: '滚动条',
        field: 'props.scrollbar',
        valueType: 'select',
        componentProps: {
          options: [
            { label: 'On', value: 'on' },
            { label: 'Off', value: 'off' },
            { label: 'Auto', value: 'auto' },
            { label: 'Active', value: 'active' }
          ]
        },
        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',
        children: [
          {
            field: 'props.column',
            valueType: 'number',
            componentProps: {
              span: 12,
              min: 0,
              max: 200
            },
            slots: {
              prefix: 'C'
            }
          },
          {
            field: 'props.row',
            valueType: 'number',
            componentProps: {
              span: 12,
              min: 0,
              max: 200
            },
            slots: {
              prefix: 'R'
            }
          }
        ]
      },
      {
        label: '类型',
        field: 'props.chart_type',
        labelWidth: '80px',
        valueType: 'select',
        componentProps: {
          options: [
            { label: 'Line', value: 'line' },
            { label: 'Bar', value: 'bar' },
            { label: 'None', value: 'none' }
          ]
        }
      },
      {
        valueType: 'dependency',
        name: ['props.chart_type'],
        dependency: (dependency) => {
          return dependency?.['props.chart_type'] === 'line'
            ? [
                {
                  label: '隐藏线条点',
                  field: 'props.hide_line_points',
                  labelWidth: '80px',
                  valueType: 'switch'
                }
              ]
            : []
        }
      },
      {
        label: '主Y轴',
        valueType: 'group',
        children: [
          {
            field: 'props.axis_y.min',
            valueType: 'number',
            componentProps: {
              span: 12,
              min: -10000,
              max: 10000
            },
            slots: {
              prefix: 'Min'
            }
          },
          {
            field: 'props.axis_y.max',
            valueType: 'number',
            componentProps: {
              span: 12,
              min: -10000,
              max: 10000
            },
            slots: {
              prefix: 'Max'
            }
          }
        ]
      },
      {
        label: '副Y轴',
        field: 'props.open_axis_y2',
        valueType: 'switch'
      },
      {
        valueType: 'dependency',
        names: ['props.open_axis_y2'],
        dependency: (dependency) => {
          return dependency['props.open_axis_y2']
            ? [
                {
                  label: '主Y轴',
                  valueType: 'group',
                  children: [
                    {
                      field: 'props.axis_y2.min',
                      valueType: 'number',
                      componentProps: {
                        span: 12,
                        min: -10000,
                        max: 10000
                      },
                      slots: {
                        prefix: 'Min'
                      }
                    },
                    {
                      field: 'props.axis_y2.max',
                      valueType: 'number',
                      componentProps: {
                        span: 12,
                        min: -10000,
                        max: 10000
                      },
                      slots: {
                        prefix: 'Max'
                      }
                    }
                  ]
                }
              ]
            : []
        }
      },
      {
        label: '属性',
        field: 'props.chart_data',
        valueType: '',
        render: (val) => {
          return <Config values={val} />
        }
      }
    ],
    // 组件样式
    styles: [
      {
        label: '模块状态',
        field: 'part',
        valueType: 'part'
      },
      {
        valueType: 'dependency',
        name: ['part'],
        dependency: () => {
          return [
            {
              label: '背景',
              field: 'background',
              valueType: 'background'
            },
            {
              label: '边框',
              field: 'border',
              valueType: 'border'
            },
            {
              label: '直线',
              field: 'line',
              valueType: 'line',
              componentProps: {
                hasDash: true,
                hideRadius: true
              }
            },
            {
              label: '阴影',
              field: 'shadow',
              valueType: 'shadow'
            },
            {
              label: '变换',
              field: 'transform',
              valueType: 'transform'
            }
          ]
        }
      }
    ]
  }
} as IComponentModelConfig
