<script lang="ts" setup>
  import { DeviceRecord } from '/@/api/device/model/deviceModel';
  import { useDescription, Description } from '/@/components/Description';
  import { Input } from 'ant-design-vue';
  import { h } from 'vue';
  import {
    COAPDeviceTypeEnum,
    PowerModeEnum,
    PowerModeNameEnum,
    TransportPayloadTypeEnum,
  } from './const';

  type TransportConfiguration = DeviceRecord['profileData']['transportConfiguration'];
  const props = defineProps<{
    record: TransportConfiguration;
  }>();

  const showProtoSchema = (data: TransportConfiguration) => {
    return (
      data.coapDeviceTypeConfiguration.transportPayloadTypeConfiguration.transportPayloadType ===
        TransportPayloadTypeEnum.PROTOBUF &&
      data.coapDeviceTypeConfiguration.coapDeviceType === COAPDeviceTypeEnum.DEFAULT
    );
  };

  const showPowerModeIsPSM = (data: TransportConfiguration) => {
    return data.clientSettings.powerMode === PowerModeNameEnum.PSM;
  };

  const showPowerModeIsEDRX = (data: TransportConfiguration) => {
    return data.clientSettings.powerMode === PowerModeEnum.E_DRX;
  };

  const [register] = useDescription({
    layout: 'vertical',
    column: 2,
    data: props.record,
    schema: [
      {
        field: 'type',
        label: '接入协议',
      },
      {
        field: 'coapDeviceType',
        label: 'COAP设备类型',
        render: (_, data: TransportConfiguration) =>
          data.coapDeviceTypeConfiguration.coapDeviceType,
      },
      {
        field: 'coapDeviceType',
        label: 'COAP设备消息Payload',
        render: (_, data: TransportConfiguration) =>
          data.coapDeviceTypeConfiguration.transportPayloadTypeConfiguration.transportPayloadType,
      },
      {
        field: 'powerMode',
        label: '节能模式',
        render: (_, data: TransportConfiguration) =>
          PowerModeNameEnum[data.clientSettings.powerMode],
      },
      {
        field: 'psmActivityTimer',
        label: 'PSM活动计时器',
        show: showPowerModeIsPSM,
        render: (_, data: TransportConfiguration) => data.clientSettings.psmActivityTimer,
      },
      {
        field: 'edrxCycle',
        label: 'eDRX循环',
        show: showPowerModeIsEDRX,
        render: (_, data: TransportConfiguration) => data.clientSettings.edrxCycle,
      },
      {
        field: 'edrxCycleTime',
        label: 'eDRX循环时间单位',
        show: showPowerModeIsEDRX,
        render: () => '秒',
      },
      {
        field: 'pagingTransmissionWindow',
        label: '寻呼传输窗口',
        show: showPowerModeIsEDRX,
        render: (_, data: TransportConfiguration) => data.clientSettings.pagingTransmissionWindow,
      },
      {
        field: 'pagingTransmissionWindowTime',
        label: '寻呼传输窗口时间单位',
        show: showPowerModeIsEDRX,
        render: () => '秒',
      },
      {
        field: 'unit',
        label: '时间单位',
        show: showPowerModeIsPSM,
        render: () => '秒',
      },
      {
        field: 'deviceTelemetryProtoSchema',
        label: '遥测数据 proto schema',
        span: 2,
        show: showProtoSchema,
        render(_, data: TransportConfiguration) {
          return h(Input.TextArea, {
            autoSize: true,
            value:
              data.coapDeviceTypeConfiguration.transportPayloadTypeConfiguration
                .deviceTelemetryProtoSchema,
          });
        },
      },
      {
        field: 'deviceAttributesProtoSchema',
        label: 'Attributes proto schema',
        span: 2,
        show: showProtoSchema,
        render(_, data: TransportConfiguration) {
          return h(Input.TextArea, {
            autoSize: true,
            value:
              data.coapDeviceTypeConfiguration.transportPayloadTypeConfiguration
                .deviceAttributesProtoSchema,
          });
        },
      },
      {
        field: 'deviceRpcRequestProtoSchema',
        label: 'RPC 请求 proto schema',
        span: 2,
        show: showProtoSchema,
        render(_, data: TransportConfiguration) {
          return h(Input.TextArea, {
            autoSize: true,
            value:
              data.coapDeviceTypeConfiguration.transportPayloadTypeConfiguration
                .deviceRpcRequestProtoSchema,
          });
        },
      },
      {
        field: 'deviceRpcResponseProtoSchema',
        label: 'RPC 响应 proto schema',
        span: 2,
        show: showProtoSchema,
        render(_, data: TransportConfiguration) {
          return h(Input.TextArea, {
            autoSize: true,
            value:
              data.coapDeviceTypeConfiguration.transportPayloadTypeConfiguration
                .deviceRpcResponseProtoSchema,
          });
        },
      },
    ],
  });
</script>

<template>
  <Description @register="register" />
</template>
