import { formatToDateTime } from '/@/utils/dateUtil';
import { Number_Operation, String_Operation, Boolean_Operation } from '/@/enums/operationEnum';

// 生成触发器或执行条件JSON数据
export const genTriggerOrConditionData = (triggerData) => {
  const {
    triggerType,
    entityId,
    type1,
    type2,
    device,
    detail,
    predicate,
    operationType,
    triggered,
    schedule,
    time,
    timeUnit,
    replaceValue,
    deviceProfileId,
    deviceType,
  } = triggerData;
  const mapPredicate = predicate?.map((item) => {
    return {
      key: {
        type: type1,
        key: type2,
      },
      valueType: operationType,
      // value: null,
      predicate: {
        type: operationType === 'DATE_TIME' ? 'NUMERIC' : operationType,
        operation: item.operation,
        ignoreCase: operationType === 'STRING' ? item.ignoreCase : undefined,
        value: {
          defaultValue:
            operationType === 'DATE_TIME' ? Number(formatToDateTime(item.value, 'x')) : item.value,
          // userValue: null,
          // dynamicValue: null,
        },
      },
    };
  });
  return {
    deviceProfileId,
    triggerType,
    entityType: device,
    entityId: entityId?.length ? entityId : null,
    deviceType,
    triggerCondition: {
      alarmDetails: detail,
      condition: {
        condition: mapPredicate,
        spec: {
          type: triggered,
          unit: triggered === 'DURATION' ? timeUnit : undefined,
          predicate: {
            defaultValue:
              triggered === 'DURATION'
                ? time
                : triggered === 'REPEATING'
                ? replaceValue
                : undefined,
            // userValue: null,
            // dynamicValue: null,
          },
        },
      },
      schedule,
    },
  };
};

export const genActionData = (actionData) => {
  const {
    alarm_config,
    alarm_level,
    outTarget,
    clearRule: clearRules,
    doContext,
    device,
    deviceId,
    deviceProfileId,
    commandType,
    callType,
    service,
    thingsModelId,
    deviceType,
  } = actionData;
  const clearRule = clearRules.map((item) => {
    const mapPredicate = item.predicate.map((pred) => {
      return {
        key: {
          type: item.type1,
          key: item.type2,
        },
        valueType: item.operationType,
        // value: null,
        predicate: {
          type: item.operationType === 'DATE_TIME' ? 'NUMERIC' : item.operationType,
          operation: pred.operation,
          ignoreCase: item.operationType === 'STRING' ? pred.ignoreCase : undefined,
          // value: {
          //   defaultValue: pred.value,
          //   // userValue: null,
          //   // dynamicValue: null,
          // },
          value: {
            defaultValue:
              item.operationType === 'DATE_TIME'
                ? Number(formatToDateTime(pred.value, 'x'))
                : pred.value,
            // userValue: null,
            // dynamicValue: null,
          },
        },
      };
    });
    return {
      triggerType: item.triggerType,
      entityType: item.device,
      entityId: item.device === 'PART' ? item.entityId : null,
      deviceType: item.deviceType,
      deviceProfileId: item.deviceProfileId,
      triggerCondition: {
        alarmDetails: item.detail,
        condition: {
          condition: mapPredicate,
          spec: {
            type: item.triggered,
            unit: item.timeUnit,
            predicate: {
              defaultValue: item.time,
            },
          },
        },
        schedule: item.schedule,
      },
    };
  });
  return [
    {
      alarmProfileId: alarm_config,
      outTarget,
      deviceType,
      entityType: device ?? 'ALL',
      deviceId: device === 'PART' ? deviceId : null,
      deviceProfileId,
      commandType: Number(commandType),
      callType,
      service,
      thingsModelId,
      doContext: clearRule?.length
        ? {
            alarmLevel: alarm_level,
            clearRule,
          }
        : {
            ...doContext,
            alarmLevel: outTarget === 'MSG_NOTIFY' ? alarm_level : undefined,
          },
    },
  ];
};

export const operationNumber_OR_TIME = [
  { label: '等于', value: Number_Operation.EQUAL },
  { label: '不等于', value: Number_Operation.NOT_EQUAL },
  { label: '小于', value: Number_Operation.LESS },
  { label: '小于等于', value: Number_Operation.LESS_OR_EQUAL },
  { label: '大于', value: Number_Operation.GREATER },
  { label: '大于等于', value: Number_Operation.GREATER_OR_EQUAL },
];

export const operationString = [
  { label: '等于', value: String_Operation.EQUAL },
  { label: '不等于', value: String_Operation.NOT_EQUAL },
  { label: '开始于', value: String_Operation.BEGAN_IN },
  { label: '结束于', value: String_Operation.END_IN },
  { label: '包含', value: String_Operation.INCLUDE },
  { label: '不包含', value: String_Operation.NOT_INCLUDE },
];

export const operationBoolean = [
  { label: '等于', value: Boolean_Operation.EQUAL },
  { label: '不等于', value: Boolean_Operation.NOT_EQUAL },
];

export const scheduleOptions = [
  { label: '始终启用', value: 'ANY_TIME' },
  { label: '定时启用', value: 'SPECIFIC_TIME' },
  { label: '自定义启用', value: 'CUSTOM' },
];
export const timeUnitOptions = [
  {
    label: '秒',
    value: 'SECONDS',
  },
  {
    label: '分',
    value: 'MINUTES',
  },
  {
    label: '时',
    value: 'HOURS',
  },
  {
    label: '天',
    value: 'DAYS',
  },
];
export const options = [
  {
    label: '数字',
    value: 'NUMERIC',
  },
  {
    label: '布尔值',
    value: 'BOOLEAN',
  },
  {
    label: '字符串',
    value: 'STRING',
  },
  {
    label: '时间',
    value: 'DATE_TIME',
  },
];

// 查找操作符
export function findOperation(valueType, operation) {
  switch (valueType) {
    case 'NUMERIC' || 'DATE_TIME':
      return operationNumber_OR_TIME.find((item) => item.value === operation);
    case 'DATE_TIME':
      return operationNumber_OR_TIME.find((item) => item.value === operation);
    case 'STRING':
      return operationString.find((item) => item.value === operation);
    case 'BOOLEAN':
      return operationBoolean.find((item) => item.value === operation);
  }
}

export function isType(operationType) {
  switch (operationType) {
    case 'NUMERIC':
      return [
        {
          field: 'operation',
          label: '执行操作',
          component: 'Select',
          required: true,
          componentProps: {
            options: operationNumber_OR_TIME,
          },
          colProps: {
            span: 8,
          },
        },
        {
          field: 'value',
          label: '操作值',
          required: true,
          component: 'InputNumber',
          componentProps: {
            min: 0,
            max: 99999999999,
          },
          colProps: {
            span: 8,
          },
        },
      ];
    case 'STRING':
      return [
        {
          field: 'ignoreCase',
          label: '忽略大小写',
          component: 'Checkbox',
          colProps: {
            span: 4,
          },
        },
        {
          field: 'operation',
          label: '执行操作',
          component: 'Select',
          required: true,
          componentProps: {
            options: operationString,
          },
          colProps: {
            span: 8,
          },
        },
        {
          field: 'value',
          label: '操作值',
          required: true,
          component: 'Input',
          colProps: {
            span: 9,
          },
        },
      ];
    case 'BOOLEAN':
      return [
        {
          field: 'operation',
          label: '执行操作',
          component: 'Select',
          required: true,
          componentProps: {
            options: operationBoolean,
          },
          colProps: {
            span: 8,
          },
        },
        {
          field: 'value',
          label: '操作值',
          component: 'Select',
          required: true,
          componentProps: {
            options: [
              {
                label: '真',
                value: 'true',
              },
              {
                label: '假',
                value: 'false',
              },
            ],
          },
          colProps: {
            span: 8,
          },
        },
      ];
    case 'DATE_TIME':
      return [
        {
          field: 'operation',
          label: '执行操作',
          required: true,
          component: 'Select',
          componentProps: {
            options: operationNumber_OR_TIME,
          },
          colProps: {
            span: 8,
          },
        },
        {
          field: 'value',
          label: '操作值',
          required: true,
          component: 'DatePicker',
          componentProps: {
            showTime: true,
          },
          colProps: {
            span: 10,
          },
        },
      ];
  }
}
export function conditionPreView(data, operationType) {
  if (operationType === 'NUMERIC' || operationType === 'DATE_TIME') {
    const { EQUAL, NOT_EQUAL, LESS, LESS_OR_EQUAL, GREATER, GREATER_OR_EQUAL } = Number_Operation;
    return data.map((item) => {
      return {
        operation:
          item?.operation === EQUAL
            ? '等于'
            : item?.operation === NOT_EQUAL
            ? '不等于'
            : item?.operation === LESS
            ? '小于'
            : item?.operation === LESS_OR_EQUAL
            ? '小于等于'
            : item?.operation === GREATER
            ? '大于'
            : item?.operation === GREATER_OR_EQUAL
            ? '大于等于'
            : '',
        value: item.value,
        attribute: item.attribute,
      };
    });
  } else if (operationType === 'STRING') {
    const { EQUAL, NOT_EQUAL, BEGAN_IN, END_IN, INCLUDE, NOT_INCLUDE } = String_Operation;
    return data.map((item) => {
      return {
        operation:
          item?.operation === EQUAL
            ? '等于'
            : item?.operation === NOT_EQUAL
            ? '不等于'
            : item?.operation === BEGAN_IN
            ? '开始于'
            : item?.operation === END_IN
            ? '结束于'
            : item?.operation === INCLUDE
            ? '包含'
            : item?.operation === NOT_INCLUDE
            ? '不包含'
            : '',
        value: item.value,
        attribute: item.attribute,
      };
    });
  } else if (operationType === 'BOOLEAN') {
    const { EQUAL, NOT_EQUAL } = Boolean_Operation;
    return data.map((item) => {
      return {
        operation:
          item?.operation === EQUAL ? '等于' : item?.operation === NOT_EQUAL ? '不等于' : '',
        value: item.value,
        attribute: item.attribute,
      };
    });
  }
}
