import { defHttp } from '@jdlinker/func';
import { DepartmentItem, MetadataType } from '../../views/device/Product/typings';
import { useMessage } from '@jdlinker/func';

const { createConfirm } = useMessage();

/**
 * 根据条件查询产品（不带翻页）
 * @param data 查询条件
 * @returns
 */
export const queryNoPagingPost = (data: any) =>
  defHttp.post({ url: `/device/product/_query/no-paging?paging=false`, data }, { isTransformResponse: false });

/**
 * 导入第三方物模型
 * @param direction from|to
 * @param type 物模型类型
 * @param data 物模型数据
 * @returns
 */
export const convertMetadata = (direction: 'from' | 'to', type: string, data: any) =>
  defHttp.post({ url: `/device/product/metadata/convert-${direction}/${type}`, data }, { isTransformResponse: false });

/**
 * 修改产品
 * @param id 产品ID
 * @param data 产品数据
 * @returns
 */
export const modify = (id: string, data: any) =>
  defHttp.put({ url: `/device/product/${id}`, data }, { isTransformResponse: false });
/**
 *
 * @returns
 */
export const getCodecs = () => defHttp.get({ url: '/device/product/metadata/codecs' }, { isTransformResponse: false });

/**
 * 根据产品ID获取产品详情
 * @param id 产品ID
 * @returns
 */
export const detail = (id: string) => defHttp.get({ url: `/device/product/${id}` }, { isTransformResponse: false });

/**
 * 产品分类
 * @param data 查询条件
 */
export const category = (data: any) =>
  defHttp.get({ url: '/product/category/_tree?paging=false', data }, { isTransformResponse: false });

/**
 * 获取网关类型
 */
export const getProviders = (data?: any) =>
  defHttp.get({ url: '/gateway/device/providers', data }, { isTransformResponse: false });

/**
 * 查询所属部门
 * @param params 查询条件
 */
export const queryOrgThree = (params?: Record<string, any>) =>
  defHttp.post<DepartmentItem>({ url: '/organization/_all/tree', params });

/**
 * 获取接入方式
 * @param data 查询条件
 */
const defaultGatewayData = {
  paging: false,
  sorts: [
    {
      name: 'createTime',
      order: 'desc'
    }
  ]
};
export const queryGatewayList = (data: any = defaultGatewayData) =>
  defHttp.post({ url: '/gateway/device/_query/no-paging', data }, { isTransformResponse: false });

/**
 * 查询产品列表(分页)
 * @param params 查询条件
 */
export const productList = (params) => defHttp.get({ url: '/device/product/list', params });
/**
 * 查询产品列表(分页)
 * @param data 查询条件
 */
export const queryProductList = (data: any) => {
  const params = { ...data };
  return defHttp.post({ url: '/device/product/list', data, params }, { isTransformResponse: false });
};

/**
 * 启用产品
 * @param productId 产品ID
 * @param data
 * @returns
 */
export const _deploy = (productId: string) =>
  defHttp.post({ url: `/device/product/${productId}/deploy` }, { isTransformResponse: false });

/**
 * 禁用产品
 * @param productId 产品ID
 * @param data
 * @returns
 */
export const _undeploy = (productId: string) =>
  defHttp.post({ url: `/device/product/${productId}/undeploy` }, { isTransformResponse: false });

/**
 * 新增产品
 * @param data
 * @returns
 */
export const addProduct = (data: any) => defHttp.post({ url: '/device/product', data }, { isTransformResponse: false });

/**
 * 修改产品
 * @param id 产品ID
 * @param data
 * @returns
 */
export const editProduct = (data: any) => defHttp.put({ url: '/device/product', data }, { isTransformResponse: false });

/**
 * 删除单个
 */
export const deleteOne = (params, handleSuccess) => {
  return defHttp.delete({ url: '/device/product/delete', params }, { joinParamsToUrl: true }).then(() => {
    handleSuccess();
  });
};

/**
 * 批量删除
 * @param params
 */
export const batchDelete = (params, handleSuccess) => {
  createConfirm({
    iconType: 'warning',
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({ url: '/device/product/deleteBatch', data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess();
      });
    }
  });
};

/**
 * 删除产品
 * @param id 产品ID
 */
export const deleteProduct = (id: string) => defHttp.delete({ url: `/device/product/${id}` });

/**
 * 检测产品Id唯一性
 * @param id 产品ID
 */
export const queryProductId = (id: string) =>
  defHttp.get({ url: `/device/product/${id}/exists` }, { isTransformResponse: false });
/**
 * 保存产品
 * @param data 产品信息
 * @returns
 */
export const saveProductMetadata = (data: Record<string, unknown>) =>
  defHttp.put({ url: '/device/product', data }, { isTransformResponse: false });

/**
 * 获取设备数量
 * @param data 查询条件
 * @returns
 */
export const getDeviceNumber = (params: any) =>
  defHttp.get({ url: '/device/instance/_count', params }, { isTransformResponse: false });

/**
 * 获取协议详情
 *  @param id 协议ID
 */
export const getProtocolDetail = (id: string) => defHttp.post({ url: `/protocol/${id}/detail` });

/**
 * 查询设备列表
 */
export const queryList = (data: any) =>
  defHttp.post({ url: `/gateway/device/detail/_query`, data }, { isTransformResponse: false });

/**
 * 查询协议数据
 */
export const getConfigView = (id: string, transport: string) =>
  defHttp.get({ url: `/protocol/${id}/transport/${transport}` }, { isTransformResponse: false });

/**
 * 获取配置数据
 */
export const getConfigMetadata = (id: string) => defHttp.get({ url: `/device/product/${id}/config-metadata` });
/**
 * 引导页是否需要提示
 */
//export const productGuide = () => defHttp.get({ url: `/user/settings/product/guide` });
/**
 * 保存引导页修改值
 */
//export const productGuideSave = (data: any) => defHttp.put({ url: '/user/settings/product/guide', data });

/**
 * 存储策略
 */
export const getStoragList = () => defHttp.get({ url: '/device/product/storage/policies' }, { isTransformResponse: false });

/**
 * 保存设备(设备接入)
 */
export const saveDevice = (data: any) => defHttp.post({ url: '/device/product', data });

/**
 * 更新选择设备(设备接入)
 */
export const updateDevice = (data: any) => {
  delete data.deviceType;
  delete data.metadata;
  delete data.storePolicyConfiguration;
  return defHttp.put({ url: '/device/product', data }, { isTransformResponse: false });
};

export const importDevice = (data: any) => {
  data.deviceTypeExand = data.deviceType.value;
  delete data.deviceType;
  return defHttp.put({ url: '/device/product', data }, { isTransformResponse: false });
};

/**
 * 获取操作符
 */
export const getOperator = () => defHttp.get({ url: '/property-calculate-rule/description' });

/**
 * 获取聚合函数列表
 */
export const getStreamingAggType = () =>
  defHttp.get<Record<string, string>[]>({
    url: '/dictionary/streaming-agg-type/items'
  });

/**
 * 根据指定的接入方式获取产品需要的配置信息
 * @pId 产品id
 * @accessId 设备接入id
 */
export const getAccessConfig = (pId: any, accessId: string) =>
  defHttp.get({ url: `/device/product/${pId}/${accessId}/config-metadata` }, { isTransformResponse: false });

export const getMetadataConfig = (params: {
  deviceId: string;
  metadata: {
    type: MetadataType | 'property';
    id: string;
    dataType: string;
  };
}) =>
  defHttp.get<Record<any, any>[]>({
    url: `/device/product/${params.deviceId}/config-metadata/${params.metadata.type}/${params.metadata.id}/${params.metadata.dataType}`
  });

export const getMetadataDeviceConfig = (params: {
  deviceId: string;
  metadata: {
    type: MetadataType | 'property';
    id: string;
    dataType: string;
  };
}) =>
  defHttp.get<Record<any, any>[]>({
    url: `/device/instance/${params.deviceId}/config-metadata/${params.metadata.type}/${params.metadata.id}/${params.metadata.dataType}`
  });
