import { FormSchema } from '/@/components/Form';

enum securityEnum {
  IS_X_CERTIFICATE = 'X509',
  IS_PAW_PUBLIC_KEY = 'RPK',
  IS_AUTO_GENERATE = 2,
  IS_AUTO_GENERATE_UNIQUE = 2,
  IS_DRX = 'PSM',
  IS_EDRX = 'E_DRX',
}

const isPawPublicKey = (type: string) => {
  return type === securityEnum.IS_PAW_PUBLIC_KEY;
};

const isCertificate = (type: string) => {
  return type === securityEnum.IS_X_CERTIFICATE;
};

const isAutoGenerate = (type: string) => {
  return type === securityEnum.IS_AUTO_GENERATE;
};

const isAutoGenerateUnique = (type: string) => {
  return type === securityEnum.IS_AUTO_GENERATE_UNIQUE;
};

const isDrx = (type: string) => {
  return type === securityEnum.IS_DRX;
};

const isEdrx = (type: string) => {
  return type === securityEnum.IS_EDRX;
};

export const modelSchemas: FormSchema[] = [
  {
    field: '0',
    component: 'Input',
    label: '对象列表',
    componentProps: {
      placeholder: '请输入Object list',
    },
    colProps: { span: 11 },
  },
];

export const settingsSchemas: FormSchema[] = [
  {
    field: 'fwUpdateStrategy',
    component: 'Select',
    label: '固件更新策略',
    defaultValue: 1,
    componentProps: {
      options: [
        {
          label: 'Push firmware update as binary file using Object 5 and Resource 0 (Package)',
          value: 1,
        },
        {
          label:
            'Auto-generate unique CoAP URL to download the package and push firmware update as Object 5 and',
          value: 2,
        },
        {
          label: 'Push firmware update as binary file using Object 19 and Resource 0 (Data)',
          value: 3,
        },
      ],
    },
    colProps: { span: 18 },
  },
  {
    field: 'fwUpdateResource',
    component: 'Input',
    label: '固件更新CoAP资源',
    required: true,
    defaultValue: 'coap://localhost:5685',
    componentProps: {
      placeholder: '请输入',
    },
    ifShow: ({ values }) => isAutoGenerate(Reflect.get(values, 'fwUpdateStrategy')),
    colProps: { span: 11 },
  },
  {
    field: 'swUpdateStrategy',
    component: 'Select',
    label: '软件更新策略',
    defaultValue: 1,
    componentProps: {
      options: [
        {
          label: 'Push binary file using Object 9 and Resource 2 (Package)',
          value: 1,
        },
        {
          label:
            'Auto-generate unique CoAP URL to download the package and push software update using Object 9 and',
          value: 2,
        },
      ],
    },
    colProps: { span: 18 },
  },
  {
    field: 'swUpdateResource',
    component: 'Input',
    label: '软件更新CoAP资源',
    required: true,
    defaultValue: 'coap://localhost:5685',
    componentProps: {
      placeholder: '请输入',
    },
    ifShow: ({ values }) => isAutoGenerateUnique(Reflect.get(values, 'swUpdateStrategy')),
    colProps: { span: 11 },
  },
  {
    field: 'powerMode',
    component: 'Select',
    label: '节能模式',
    defaultValue: 'DRX',
    componentProps: {
      options: [
        {
          label: 'Discontinuous Reception (DRX)',
          value: 'DRX',
        },
        {
          label: 'Power Saving Mode (PSM)',
          value: 'PSM',
        },
        {
          label: 'Extended Discontinuous Reception (eDRX)',
          value: 'E_DRX',
        },
      ],
    },
    colProps: { span: 18 },
  },
  {
    field: 'edrxCycle',
    component: 'InputNumber',
    label: 'PSM活动计时器',
    required: true,
    defaultValue: 10,
    colProps: { span: 11 },
    ifShow: ({ values }) => isDrx(values.powerMode),
  },
  {
    field: 'unit2',
    component: 'Select',
    label: '时间单位',
    defaultValue: 'second',
    componentProps: {
      options: [
        { label: 'Milliseconds', value: 'Milliseconds' },
        { label: '秒', value: 'second' },
        { label: '分钟', value: 'minute' },
        { label: '小时', value: 'hour' },
      ],
    },
    colProps: { span: 11 },
    ifShow: ({ values }) => isDrx(values.powerMode),
  },

  {
    field: 'edrxCycle',
    component: 'InputNumber',
    label: 'eDRX循环',
    required: true,
    defaultValue: 81,
    componentProps: {
      placeholder: '请输入PSM Activity Timer',
    },
    colProps: { span: 11 },
    ifShow: ({ values }) => isEdrx(values.powerMode),
  },
  {
    field: 'unit2',
    component: 'Select',
    label: '时间单位',
    defaultValue: 'second',
    componentProps: {
      options: [
        { label: 'Milliseconds', value: 'Milliseconds' },
        { label: '秒', value: 'second' },
        { label: '分钟', value: 'minute' },
        { label: '小时', value: 'hour' },
      ],
    },
    colProps: { span: 11 },
    ifShow: ({ values }) => isEdrx(values.powerMode),
  },
  {
    field: 'pagingTransmissionWindow',
    component: 'InputNumber',
    label: '寻呼传输窗口',
    required: true,
    defaultValue: 10,
    componentProps: {
      placeholder: '请输入Paging Transmission Window',
    },
    colProps: { span: 11 },
    ifShow: ({ values }) => isEdrx(values.powerMode),
  },
  {
    field: 'unit',
    component: 'Select',
    label: '时间单位',
    defaultValue: 'second',
    componentProps: {
      options: [
        { label: 'Milliseconds', value: 'Milliseconds' },
        { label: '秒', value: 'second' },
        { label: '分钟', value: 'minute' },
        { label: '小时', value: 'hour' },
      ],
    },
    colProps: { span: 11 },
    ifShow: ({ values }) => isEdrx(values.powerMode),
  },
  {
    field: 'compositeOperationsSupport',
    label: '启用',
    colProps: { span: 22 },
    component: 'Checkbox',
    defaultValue: false,
    renderComponentContent: '支持复合读/写/观察操作',
  },
];

export const deviceSchemas: FormSchema[] = [
  {
    field: '1',
    label: 'LWM2M',
    colProps: { span: 22 },
    component: 'InputTextArea',
    componentProps: {
      autoSize: {
        maxRows: 50,
      },
    },
    defaultValue: `
    {
      "observeAttr": {
        "observe": [],
        "attribute": [],
        "telemetry": [],
        "keyName": {},
        "attributeLwm2m": {}
      },
      "bootstrap": [
        {
          "shortServerId": 123,
          "bootstrapServerIs": false,
          "host": "0.0.0.0",
          "port": 5685,
          "clientHoldOffTime": 1,
          "serverPublicKey": "",
          "serverCertificate": "",
          "bootstrapServerAccountTimeout": 0,
          "lifetime": 300,
          "defaultMinPeriod": 1,
          "notifIfDisabled": true,
          "binding": "U",
          "securityMode": "NO_SEC"
        }
      ],
      "clientLwM2mSettings": {
        "clientOnlyObserveAfterConnect": 1,
        "fwUpdateStrategy": 1,
        "swUpdateStrategy": 1,
        "powerMode": "DRX",
        "compositeOperationsSupport": false
      },
      "bootstrapServerUpdateEnable": false,
      "type": "LWM2M"
    }
    `,
  },
];

export const serverSchemas: FormSchema[] = [
  {
    field: 'securityMode',
    component: 'Select',
    label: '安全配置',
    defaultValue: 'NO_SEC',
    helpMessage: [
      'X.509 Certificate模式和Raw Public Key模式和Pre-Shared Key模式下，port端口必须是5686',
    ],
    componentProps: {
      options: [
        {
          label: 'No Security',
          value: 'NO_SEC',
        },
        {
          label: 'Pre-Shared Key',
          value: 'PSK',
        },
        {
          label: 'Raw Public Key',
          value: 'RPK',
        },
        {
          label: 'X.509 Certificate',
          value: 'X509',
        },
      ],
    },
    colProps: { span: 8 },
  },
  {
    field: 'shortServerId',
    component: 'InputNumber',
    label: '短服务器ID',
    helpMessage: ['短服务器ID是唯一的'],
    required: true,
    defaultValue: 123,
    colProps: { span: 8 },
  },
  {
    field: 'host',
    component: 'Input',
    label: '主机',
    helpMessage: ['主机是唯一的'],
    required: true,
    defaultValue: '0.0.0.0',
    colProps: { span: 8 },
  },
  {
    field: 'port',
    component: 'Input',
    label: '端口',
    helpMessage: ['端口必须是5685'],
    required: true,
    defaultValue: 5685,
    colProps: { span: 8 },
  },
  {
    field: 'clientHoldOffTime',
    component: 'InputNumber',
    label: '延迟时间',
    required: true,
    defaultValue: 1,
    colProps: { span: 8 },
  },
  {
    field: 'bootstrapServerAccountTimeout',
    component: 'InputNumber',
    label: '超时后的帐户',
    required: true,
    defaultValue: 0,
    colProps: { span: 8 },
  },
  {
    field: 'lifetime',
    component: 'InputNumber',
    label: '注册生存期',
    required: true,
    defaultValue: 300,
    colProps: { span: 8 },
  },
  {
    field: 'defaultMinPeriod',
    component: 'InputNumber',
    label: '两次通知之间的最小周期',
    required: true,
    defaultValue: 1,
    colProps: { span: 8 },
  },
  {
    field: 'binding',
    component: 'Select',
    label: 'Binding',
    defaultValue: 'UQ',
    componentProps: {
      options: [
        {
          label: 'U: Client is reachable via the UDP binding at any time.',
          value: 'U',
        },
        {
          label: 'M: Client is reachable via the MQTT binding at any time.',
          value: 'M',
        },
        {
          label: 'H: Client is reachable via the HTTP binding at any time.',
          value: 'H',
        },
        {
          label: 'T: Client is reachable via the TCP binding at any time.',
          value: 'T',
        },
        {
          label: 'S: Client is reachable via the SMS binding at any time.',
          value: 'S',
        },
        {
          label:
            'N: Client MUST send the response to such a request over the Non-IP binding (is supported since LWM',
          value: 'N',
        },
        {
          label: 'UQ: UDP connection in queue mode (is not supported since LWM2M 1.1)',
          value: 'UQ',
        },
        {
          label:
            'UQS: both UDP and SMS connections active; UDP in queue mode, SMS in standard mode (is not supported since LWM2M 1.1)',
          value: 'UQS',
        },
        {
          label: 'TQ: TCP connection in queue mode (is not supported since LWM2M 1.1)',
          value: 'TQ:',
        },
        {
          label:
            'TQS: both TCP and SMS connections active; TCP in queue mode, SMS in standard mode (is not supported since LWM2M 1.1)	',
          value: 'TQS',
        },
        {
          label: 'SQ: SMS connection in queue mode (is not supported since LWM2M 1.1)',
          value: 'SQ',
        },
      ],
    },
    colProps: { span: 11 },
  },
  {
    field: 'notifIfDisabled',
    label: '是否启用',
    colProps: { span: 22 },
    component: 'Checkbox',
    defaultValue: true,
    renderComponentContent: '禁用或脱机时的通知存储',
  },
  {
    field: 'serverPublicKey',
    component: 'InputTextArea',
    label: '服务器公钥',
    required: true,
    colProps: { span: 22 },
    componentProps: {
      maxLength: 255,
      placeholder: '请输入服务器公钥',
    },
    ifShow: ({ values }) =>
      isCertificate(Reflect.get(values, 'securityMode')) ||
      isPawPublicKey(Reflect.get(values, 'securityMode')),
  },
];
