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

enum EnumType {
  IS_DEFAULT = 'DEFAULT',
  IS_MQTT = 'MQTT',
  IS_PROTOBUF = 'PROTOBUF',
  IS_COAP = 'COAP',
  IS_LWM2M = 'LWM2M',
  IS_SNMP = 'SNMP',
  IS_PSM = 'PSM',
  IS_DRX = 'E_DRX',
  IS_EFENTO_NBIOT = 'EFENTO',
}

const isProtobuf = (type: string) => {
  return type === EnumType.IS_PROTOBUF;
};

const isPsm = (type: string) => {
  return type === EnumType.IS_PSM;
};

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

const isEfentoNb = (type: string) => {
  return type === EnumType.IS_EFENTO_NBIOT;
};

export const CoapSchemas: FormSchema[] = [
  {
    field: 'coapDeviceType',
    component: 'Select',
    label: 'CoAP设备类型',
    defaultValue: 'DEFAULT',
    componentProps: {
      options: [
        { label: '默认', value: 'DEFAULT' },
        { label: 'Efento NB-IoT', value: 'EFENTO' },
      ],
    },
    colProps: { span: 22 },
  },
  {
    field: 'transportPayloadType',
    component: 'Select',
    label: 'CoAP设备消息Payload',
    defaultValue: 'JSON',
    componentProps: {
      options: [
        { label: 'JSON', value: 'JSON' },
        { label: 'PROTOBUF', value: 'PROTOBUF' },
      ],
    },
    colProps: { span: 22 },
    ifShow: ({ values }) => !isEfentoNb(values.coapDeviceType),
  },
  {
    field: 'powerMode',
    component: 'Select',
    label: '节能模式',
    defaultValue: 'DRX',
    componentProps: {
      options: [
        { label: 'Power Saving Mode (PSM)', value: 'PSM' },
        { label: 'Discontinuous Reception (DRX)', value: 'DRX' },
        {
          label: 'Extended Discontinuous Reception (eDRX)',
          value: 'E_DRX',
        },
      ],
    },
    colProps: { span: 22 },
  },
  {
    field: 'psmActivityTimer',
    component: 'InputNumber',
    label: 'PSM活动计时器',
    required: true,
    defaultValue: 10,
    componentProps: {
      placeholder: '请输入PSM活动计时器',
    },
    colProps: { span: 11 },
    ifShow: ({ values }) => isPsm(values.powerMode),
  },
  {
    field: 'unit1',
    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 }) => isPsm(values.powerMode),
  },
  {
    field: 'edrxCycle',
    component: 'InputNumber',
    label: 'eDRX循环',
    required: true,
    defaultValue: 81,
    componentProps: {
      placeholder: '请输入eDRX循环',
    },
    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: 'pagingTransmissionWindow',
    component: 'InputNumber',
    label: '寻呼传输窗口',
    required: true,
    defaultValue: 10,
    componentProps: {
      placeholder: '请输入寻呼传输窗口',
    },
    colProps: { span: 11 },
    ifShow: ({ values }) => isDrx(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 }) => isDrx(values.powerMode),
  },
  {
    field: 'deviceTelemetryProtoSchema',
    label: '遥测数据 proto schema',
    colProps: { span: 22 },
    component: 'InputTextArea',
    componentProps: {
      rows: 17,
    },
    defaultValue: `
    syntax ="proto3";
    package telemetry;

    message SensorDataReading {
      optional double temperature = 1;
      optional double humidity = 2;
      InnerObject innerObject = 3;
      message InnerObject {
        optional string key1 = 1;
        optional bool key2 = 2;
        optional double key3 = 3;
        optional int32 key4 = 4;
        optional string key5 = 5;
      }
    }
    `,
    ifShow: ({ values }) =>
      isProtobuf(values.transportPayloadType) && !isEfentoNb(values.coapDeviceType),
  },
  {
    field: 'deviceAttributesProtoSchema',
    label: 'Attributes proto schema',
    colProps: { span: 22 },
    component: 'InputTextArea',
    componentProps: {
      rows: 9,
    },
    defaultValue: `
    syntax ="proto3";
    package attributes;

    message SensorConfiguration {
      optional string firmwareVersion = 1;
      optional string serialNumber = 2;
    }
    `,
    ifShow: ({ values }) =>
      isProtobuf(values.transportPayloadType) && !isEfentoNb(values.coapDeviceType),
  },
  {
    field: 'deviceRpcRequestProtoSchema',
    label: 'RPC 请求 proto schema',
    colProps: { span: 22 },
    component: 'InputTextArea',
    componentProps: {
      rows: 11,
    },
    defaultValue: `
    syntax ="proto3";
    package rpc;
    
    message RpcRequestMsg {
      optional string method = 1;
      optional int32 requestId = 2;
      optional string params = 3;
    }
    `,
    ifShow: ({ values }) =>
      isProtobuf(values.transportPayloadType) && !isEfentoNb(values.coapDeviceType),
  },
  {
    field: 'deviceRpcResponseProtoSchema',
    label: 'RPC 响应 proto schema',
    colProps: { span: 22 },
    component: 'InputTextArea',
    componentProps: {
      rows: 9,
    },
    defaultValue: `
    syntax ="proto3";
    package rpc;
    
    message RpcResponseMsg {
      optional string payload = 1;
    }
    `,
    ifShow: ({ values }) =>
      isProtobuf(values.transportPayloadType) && !isEfentoNb(values.coapDeviceType),
  },
];

export const deviceTelemetryProtoSchemaData = `
syntax ="proto3";
package telemetry;

message SensorDataReading {
  optional double temperature = 1;
  optional double humidity = 2;
  InnerObject innerObject = 3;
  message InnerObject {
    optional string key1 = 1;
    optional bool key2 = 2;
    optional double key3 = 3;
    optional int32 key4 = 4;
    optional string key5 = 5;
  }
}
`;

export const deviceAttributesProtoSchemaData = `
syntax ="proto3";
package attributes;

message SensorConfiguration {
  optional string firmwareVersion = 1;
  optional string serialNumber = 2;
}
`;

export const deviceRpcRequestProtoSchemaData = `
syntax ="proto3";
package rpc;

message RpcRequestMsg {
  optional string method = 1;
  optional int32 requestId = 2;
  optional string params = 3;
}
`;

export const deviceRpcResponseProtoSchemaData = `
syntax ="proto3";
package rpc;

message RpcResponseMsg {
  optional string payload = 1;
}
`;
