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

enum EnumMQTT {
  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 === EnumMQTT.IS_PROTOBUF;
};

export const MqttSchemas: FormSchema[] = [
  {
    field: 'deviceTelemetryTopic',
    component: 'Input',
    label: '遥测数据主题筛选器',
    required: true,
    defaultValue: 'v1/devices/me/telemetry',
    componentProps: {
      placeholder: '请输入遥测数据 topic 筛选器',
    },
    colProps: { span: 23 },
  },
  {
    field: 'deviceAttributesTopic',
    component: 'Input',
    required: true,
    label: '属性主题过滤器',
    defaultValue: 'v1/devices/me/attributes',
    componentProps: {
      placeholder: '请输入Attributes topic 筛选器',
    },
    colProps: { span: 23 },
  },
  {
    field: 'desc',
    component: 'InputTextArea',
    label: '',
    slot: 'descSlot',
  },
  {
    field: 'transportPayloadType',
    component: 'Select',
    label: 'MQTT 设备 Payload',
    defaultValue: 'JSON',
    componentProps: {
      options: [
        { label: 'JSON', value: 'JSON' },
        { label: 'PROTOBUF', value: 'PROTOBUF' },
      ],
    },
    colProps: { span: 11 },
  },
  {
    field: 'useJsonPayloadFormatForDefaultDownlinkTopics',
    label: '',
    colProps: { span: 23 },
    defaultValue: false,
    component: 'Checkbox',
    ifShow: ({ values }) => isProtobuf(values.transportPayloadType),
    renderComponentContent: '启用与其他有效负载格式的兼容性',
  },
  {
    field: 'desc1',
    component: 'InputTextArea',
    label: '',
    slot: 'desc1',
    ifShow: ({ values }) => isProtobuf(values.transportPayloadType),
  },
  {
    field: 'enableCompatibilityWithJsonPayloadFormat',
    label: '',
    colProps: { span: 23 },
    defaultValue: false,
    component: 'Checkbox',
    renderComponentContent: '默认下行主题使用Json格式',
    ifShow: ({ values }) =>
      isProtobuf(values.transportPayloadType) &&
      !!values.useJsonPayloadFormatForDefaultDownlinkTopics,
  },
  {
    field: 'desc2',
    component: 'InputTextArea',
    label: '',
    slot: 'desc2',
    ifShow: ({ values }) =>
      isProtobuf(values.transportPayloadType) &&
      !!values.useJsonPayloadFormatForDefaultDownlinkTopics,
  },
  {
    field: 'deviceTelemetryProtoSchema',
    label: '遥测数据 proto schema',
    colProps: { span: 23 },
    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),
  },
  {
    field: 'deviceAttributesProtoSchema',
    label: 'Attributes proto schema',
    colProps: { span: 23 },
    component: 'InputTextArea',
    componentProps: {
      rows: 11,
    },
    defaultValue: `
    syntax ="proto3";
    package attributes;

    message SensorConfiguration {
      optional string firmwareVersion = 1;
      optional string serialNumber = 2;
    }
    `,
    ifShow: ({ values }) => isProtobuf(values.transportPayloadType),
  },
  {
    field: 'deviceRpcRequestProtoSchema',
    label: 'RPC 请求 proto schema',
    colProps: { span: 23 },
    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),
  },
  {
    field: 'deviceRpcResponseProtoSchema',
    label: 'RPC 响应 proto schema',
    colProps: { span: 23 },
    component: 'InputTextArea',
    componentProps: {
      rows: 11,
    },
    defaultValue: `
    syntax ="proto3";
    package rpc;
    
    message RpcResponseMsg {
      optional string payload = 1;
    }
    `,
    ifShow: ({ values }) => isProtobuf(values.transportPayloadType),
  },
];

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;
}
`;

export const fixedTextA = `支持单[+]和多级[#]通配符。 [+] is suitable for any topic filter
level。例如：v1/devices/+/telemetry or +/devices/+/attributes。 [#]可以替换 topic
filter 本身，并且必须是 topic 的最后一个符号。例如：# or v1/devices/me/#。`;

export const fixedTextB = `启用时，默认情况下，平台将使用Protobuf有效载荷格式。如果解析失败，平台将尝试使用JSON负载格式。
有助于固件更新期间的向后兼容性。例如，固件的初始版本使用Json，而新版本使用Protobuf。
在设备组的固件更新过程中，需要同时支持Protobuf和JSON。兼容性模式会导致性能略有下降，
因此建议在更新所有设备后禁用此模式。`;

export const fixedTextC = `启用后，平台将使用Json负载格式通过以下主题推送属性
和RPC:v1/devices/me/attributes/response/$request id、v1/devices/me/attributes、
v1/devices/me/RPC/request/$request id、v1/devices/me/RPC/response/$request id。
此设置不会影响使用新（v2）主题发送的属性和rpc订阅： v2/a/res/$request
id、v2/a、v2/r/req/$request id、v2/r/res/$request id。 其中，$request
id是整数请求标识符。`;
