import request from '@/utils/request';
import { AxiosPromise } from 'axios';
import {
  IntegrationCommandDispatchForm,
  IntegrationConnectionTestForm,
  IntegrationConnectionTestResult,
  IntegrationEndpointVO,
  IntegrationEndpointForm,
  IntegrationEndpointId,
  IntegrationEndpointIds,
  IntegrationEndpointQuery,
  IntegrationPayloadPreviewForm,
  IntegrationPayloadPreviewResult,
  IntegrationWorkbenchDetail
} from '@/api/ems/base/integrationEndpoint/types';
import type { ControlCommandLogVO } from '@/api/ems/types';

/**
 * 查询对接端点配置列表
 * @param query
 * @returns {*}
 */

export const listIntegrationEndpoint = (query?: IntegrationEndpointQuery): AxiosPromise<IntegrationEndpointVO[]> => {
  return request({
    url: '/ems/base/integrationEndpoint/list',
    method: 'get',
    params: query
  });
};

/**
 * 查询对接端点全量列表
 * 表单绑定端点下拉时走专门接口，避免为拿全部选项触发分页语义。
 */
export const listIntegrationEndpointAll = (): AxiosPromise<IntegrationEndpointVO[]> => {
  return request({
    url: '/ems/base/integrationEndpoint/listAll',
    method: 'get'
  });
};

/**
 * 查询对接端点配置详细
 * @param id
 */
export const getIntegrationEndpoint = (id: IntegrationEndpointId): AxiosPromise<IntegrationEndpointVO> => {
  return request({
    url: '/ems/base/integrationEndpoint/' + id,
    method: 'get'
  });
};

/**
 * 批量查询对接端点配置详细
 * @param ids
 */
export const getIntegrationEndpointByIds = (ids: IntegrationEndpointIds): AxiosPromise<IntegrationEndpointVO[]> => {
  return request({
    url: '/ems/base/integrationEndpoint/listByIds',
    method: 'post',
    data: ids
  });
};

/**
 * 统计对接端点配置数量
 * @param query
 */
export const countIntegrationEndpoint = (query?: IntegrationEndpointQuery): AxiosPromise<number> => {
  return request({
    url: '/ems/base/integrationEndpoint/count',
    method: 'get',
    params: query
  });
};

/**
 * 判断是否存在符合条件的对接端点配置
 * @param query
 */
export const existsIntegrationEndpoint = (query?: IntegrationEndpointQuery): AxiosPromise<boolean> => {
  return request({
    url: '/ems/base/integrationEndpoint/exists',
    method: 'get',
    params: query
  });
};

/**
 * 新增对接端点配置
 * @param data
 */
export const addIntegrationEndpoint = (data: IntegrationEndpointForm) => {
  return request({
    url: '/ems/base/integrationEndpoint',
    method: 'post',
    data: data
  });
};

/**
 * 修改对接端点配置
 * @param data
 */
export const updateIntegrationEndpoint = (data: IntegrationEndpointForm) => {
  return request({
    url: '/ems/base/integrationEndpoint',
    method: 'put',
    data: data
  });
};

/**
 * 删除对接端点配置
 * @param id
 */
export const delIntegrationEndpoint = (id: IntegrationEndpointId | IntegrationEndpointIds) => {
  return request({
    url: '/ems/base/integrationEndpoint/' + id,
    method: 'delete'
  });
};

/**
 * 查询协议工作台聚合详情
 * @param id
 */
export const getIntegrationWorkbenchDetail = (id: IntegrationEndpointId): AxiosPromise<IntegrationWorkbenchDetail> => {
  return request({
    url: '/ems/base/integrationEndpoint/workbench/' + id,
    method: 'get'
  });
};

/**
 * 测试端点连接配置
 * @param data
 */
export const testIntegrationConnection = (data: IntegrationConnectionTestForm): AxiosPromise<IntegrationConnectionTestResult> => {
  return request({
    url: '/ems/base/integrationEndpoint/testConnection',
    method: 'post',
    data
  });
};

/**
 * 预览协议报文
 * @param data
 */
export const previewIntegrationPayload = (data: IntegrationPayloadPreviewForm): AxiosPromise<IntegrationPayloadPreviewResult> => {
  return request({
    url: '/ems/base/integrationEndpoint/previewPayload',
    method: 'post',
    data
  });
};

/**
 * 执行命令下发
 * @param data
 */
export const dispatchIntegrationCommand = (data: IntegrationCommandDispatchForm): AxiosPromise<ControlCommandLogVO> => {
  return request({
    url: '/ems/base/integrationEndpoint/dispatchCommand',
    method: 'post',
    data
  });
};
