import { FormSchema } from '/@/components/Table';
import { findDictItemByCode } from '/@/api/system/dict';

export enum FormField {
  FUNCTION_NAME = 'functionName',
  FUNCTION_TYPE = 'functionType',
  FUNCTION_JSON = 'functionJson',
  IDENTIFIER = 'identifier',
  TYPE = 'type',
  REFARK = 'remark',
  MIN = 'min',
  MAX = 'max',
  UNIT = 'unit',
  UNIT_NAME = 'unitName',
  STEP = 'step',
  VALUE_RANGE = 'valueRange',
  BOOL_CLOSE = 'boolClose',
  BOOL_OPEN = 'boolOpen',
  LENGTH = 'length',
  SPECS_LIST = 'specs',
  CALL_TYPE = 'callType',
  INPUT_PARAM = 'inputData',
  OUTPUT_PARAM = 'outputData',
  EVENT_TYPE = 'eventType',
  SERVICE_COMMAND = 'serviceCommand',
  ACCESS_MODE = 'accessMode',

  STRUCT = 'struct',
}

export enum FunctionType {
  PROPERTIES = 'properties',
  EVENTS = 'events',
  SERVICE = 'services',
}

export enum AssessMode {
  READ = 'r',
  WRITE = 'w',
}

/**
 * 新增参数 动态显示表单
 */
export enum DataTypeEnum {
  IS_NUMBER_INT = 'INT',
  IS_NUMBER_DOUBLE = 'DOUBLE',
  IS_STRING = 'TEXT',
  IS_STRUCT = 'STRUCT',
  IS_BOOL = 'BOOL',
}

const isNumber = (type: string) => {
  return type === DataTypeEnum.IS_NUMBER_INT || type === DataTypeEnum.IS_NUMBER_DOUBLE;
};

const isString = (type: string) => {
  return type === DataTypeEnum.IS_STRING;
};
const isStruct = (type: string) => {
  return type === DataTypeEnum.IS_STRUCT;
};

const isBool = (type: string) => {
  return type === DataTypeEnum.IS_BOOL;
};

export const serviceSchemas = (tcpDeviceFlag: boolean): FormSchema[] => {
  return [
    {
      field: FormField.FUNCTION_NAME,
      label: '功能名称',
      required: true,
      component: 'Input',
      colProps: {
        span: 18,
      },
      componentProps: {
        maxLength: 255,
        placeholder: '请输入功能名称',
      },
    },
    {
      field: FormField.IDENTIFIER,
      label: '标识符',
      required: true,
      component: 'Input',
      colProps: {
        span: 18,
      },
      componentProps: {
        maxLength: 255,
        placeholder: '请输入标识符',
      },
    },
    {
      field: FormField.CALL_TYPE,
      component: 'ApiRadioGroup',
      label: '调用方式',
      required: true,
      colProps: {
        span: 24,
      },
      defaultValue: 'ASYNC',
      componentProps: {
        placeholder: '请选择调用方式',
        api: findDictItemByCode,
        params: {
          dictCode: 'call_mode',
        },
        labelField: 'itemText',
        valueField: 'itemValue',
      },
    },
    {
      field: FormField.SERVICE_COMMAND,
      label: '输入参数',
      component: 'Input',
      ifShow: tcpDeviceFlag,
      componentProps: {
        placeholder: '请输入ASCII或HEX服务命令',
      },
    },
    {
      field: FormField.INPUT_PARAM,
      label: '输入参数',
      component: 'StructForm',
      valueField: 'value',
      changeEvent: 'update:value',
      ifShow: !tcpDeviceFlag,
      colProps: { span: 24 },
    },
    {
      field: FormField.OUTPUT_PARAM,
      label: '输出参数',
      component: 'StructForm',
      valueField: 'value',
      changeEvent: 'update:value',
      colProps: { span: 24 },
    },
    {
      field: FormField.REFARK,
      label: '备注',
      component: 'InputTextArea',
      componentProps: {
        rows: 6,
        maxLength: 100,
        placeholder: '请输入描述',
      },
    },
  ];
};

export const eventSchemas: FormSchema[] = [
  {
    field: FormField.FUNCTION_NAME,
    label: '功能名称',
    required: true,
    component: 'Input',
    colProps: {
      span: 18,
    },
    componentProps: {
      maxLength: 255,
      placeholder: '请输入功能名称',
    },
  },
  {
    field: FormField.IDENTIFIER,
    label: '标识符',
    required: true,
    component: 'Input',
    colProps: {
      span: 18,
    },
    componentProps: {
      maxLength: 255,
      placeholder: '请输入标识符',
    },
  },
  {
    field: FormField.EVENT_TYPE,
    component: 'ApiRadioGroup',
    label: '事件类型',
    required: true,
    colProps: {
      span: 24,
    },
    defaultValue: 'INFO',
    componentProps: {
      placeholder: '请选择事件类型',
      api: findDictItemByCode,
      params: {
        dictCode: 'event_type',
      },
      labelField: 'itemText',
      valueField: 'itemValue',
    },
  },
  {
    field: FormField.OUTPUT_PARAM,
    label: '输出参数',
    component: 'StructForm',
    valueField: 'value',
    changeEvent: 'update:value',
    colProps: { span: 24 },
  },
  {
    field: FormField.REFARK,
    label: '备注',
    component: 'InputTextArea',
    componentProps: {
      rows: 6,
      maxLength: 100,
      placeholder: '请输入描述',
    },
  },
];

export const addParamsSchemas: FormSchema[] = [
  {
    field: FormField.FUNCTION_NAME,
    label: '参数名称',
    required: true,
    component: 'Input',
    colProps: {
      span: 23,
    },
    componentProps: {
      maxLength: 255,
      placeholder: '请输入参数名称',
    },
  },
  {
    field: FormField.IDENTIFIER,
    label: '标识符',
    required: true,
    component: 'Input',
    colProps: {
      span: 23,
    },
    componentProps: {
      maxLength: 255,
      placeholder: '请输入标识符',
    },
  },
  {
    field: FormField.TYPE,
    label: '数据类型',
    required: true,
    component: 'ApiSelect',
    colProps: {
      span: 23,
    },
    defaultValue: 'INT',
    componentProps: {
      placeholder: '请选择数据类型',
      api: findDictItemByCode,
      params: {
        dictCode: 'data_type',
      },
      labelField: 'itemText',
      valueField: 'itemValue',
    },
  },
  {
    field: FormField.STRUCT,
    label: 'JSON 对象',
    component: 'Input',
    colProps: {
      span: 23,
    },
    ifShow: ({ values }) => isStruct(values[FormField.TYPE]),
  },
  {
    field: FormField.BOOL_CLOSE,
    component: 'Input',
    required: true,
    label: '0 -',
    colProps: {
      span: 23,
    },
    componentProps: {
      placeholder: '如：关',
    },
    ifShow: ({ values }) => isBool(values[FormField.TYPE]),
  },
  {
    field: FormField.BOOL_OPEN,
    component: 'Input',
    required: true,
    label: '1 -',
    colProps: {
      span: 23,
    },
    componentProps: {
      placeholder: '如：开',
    },
    ifShow: ({ values }) => isBool(values[FormField.TYPE]),
  },
  {
    field: FormField.LENGTH,
    component: 'Input',
    required: true,
    label: '数据长度',
    defaultValue: '10240',
    colProps: {
      span: 8,
    },
    componentProps: {
      placeholder: '请输入数据长度',
    },
    renderComponentContent: () => {
      return {
        suffix: () => '字节',
      };
    },
    ifShow: ({ values }) => isString(values[FormField.TYPE]),
  },
  {
    field: FormField.VALUE_RANGE,
    label: '取值范围',
    component: 'CustomMinMaxInput',
    colProps: {
      span: 23,
    },
    ifShow: ({ values }) => isNumber(values[FormField.TYPE]),
  },
  {
    field: FormField.STEP,
    label: '步长',
    component: 'Input',
    colProps: {
      span: 23,
    },
    componentProps: {
      maxLength: 255,
      placeholder: '请输入步长',
    },
    ifShow: ({ values }) => isNumber(values[FormField.TYPE]),
  },
  {
    field: FormField.UNIT,
    label: '单位',
    component: 'ApiSelect',
    colProps: {
      span: 23,
    },
    componentProps: {
      placeholder: '请选择单位',
      api: findDictItemByCode,
      params: {
        dictCode: 'attribute_unit',
      },
      labelField: 'itemText',
      valueField: 'itemValue',
    },
    ifShow: ({ values }) => isNumber(values[FormField.TYPE]),
  },
];
