import { defHttp } from '/@/utils/http/axios';
import {
  TDeviceConfigParams,
  IDeviceConfigAddOrEditModel,
  ProfileRecord,
  RuleChainRecord,
} from '/@/api/device/model/deviceConfigModel';
import { PaginationResult } from '/#/axios';

enum EDeviceConfigApi {
  /**
   * 设备配置URL
   */
  DEVICE_CONFIG_GET_PAGE = '/device_profile',
  DEVICE_CONFIG_POST_ADD_OR_EDIT = '/device_profile',
  DEVICE_CONFIG_GET_DETAIL = '/device_profile/',
  DEVICE_CONFIG_DELETE = '/device_profile',
  DEVICE_CONFIG_GET_RULECHAIN = '/rule_chain/me/list',
  ALARM_CONTACT_GET_PAGE = '/alarm_contact',
  DEVICE_CONFIG_EXPORT = '/device_profile/export',
  DEVICE_CONFIG_IMPORT = '/device_profile/import',
  SET_DEVICE_ISDEFAULT = '/deviceProfile',
  FRP_API = '/frp/',
  GET_TB_DEVICE_ID = '/device/get_subset',
  COMMODRECORD = '/rpc',
}

/**
 * 设备配置详情
 */
export const deviceConfigGetDetail = (id: string) => {
  return defHttp.get({
    url: `${EDeviceConfigApi.DEVICE_CONFIG_GET_DETAIL}${id}`,
  });
};

/**
 * 获取规则链
 */
export const deviceConfigGetRuleChain = () => {
  return defHttp.get<RuleChainRecord[]>({
    url: EDeviceConfigApi.DEVICE_CONFIG_GET_RULECHAIN,
  });
};

/**
 * 获取告警联系人
 */
export const alarmContactGetPage = () => {
  return defHttp.get({
    url: `${EDeviceConfigApi.ALARM_CONTACT_GET_PAGE}?page=1&pageSize=10`,
  });
};

/**
 * 分页查询设备配置页面
 */
export const deviceConfigGetQuery = (params?: TDeviceConfigParams) => {
  return defHttp.get<PaginationResult<ProfileRecord>>({
    url: EDeviceConfigApi.DEVICE_CONFIG_GET_PAGE,
    params,
  });
};

/**
 * 新增或者编辑设备配置

 */
export const deviceConfigAddOrEdit = (params: IDeviceConfigAddOrEditModel) => {
  return defHttp.post<IDeviceConfigAddOrEditModel>({
    url: EDeviceConfigApi.DEVICE_CONFIG_POST_ADD_OR_EDIT,
    params,
  });
};

/**
 * 删除设备配置
 */
export const deviceConfigDelete = (ids: string[]) => {
  return defHttp.delete({
    url: EDeviceConfigApi.DEVICE_CONFIG_DELETE,
    data: {
      ids,
    },
  });
};

/**
 * 导出设备配置
 */
export const deviceConfigExport = (params: IDeviceConfigAddOrEditModel) => {
  return defHttp.post<IDeviceConfigAddOrEditModel>({
    url: EDeviceConfigApi.DEVICE_CONFIG_EXPORT,
    params,
  });
};

/**
 * 导入设备配置
 */
export const deviceConfigImport = (params: IDeviceConfigAddOrEditModel) => {
  return defHttp.post<IDeviceConfigAddOrEditModel>({
    url: EDeviceConfigApi.DEVICE_CONFIG_IMPORT,
    params,
  });
};

/**
 *
 * 设置该设备配置为默认
 */
export const setDeviceProfileIsDefaultApi = (id: string, v, params?: {}) => {
  return defHttp.post(
    {
      url: EDeviceConfigApi.SET_DEVICE_ISDEFAULT + '/' + id + '/' + v,
      params,
    },
    {
      joinPrefix: false,
    }
  );
};

/**
 * Frp内网穿透信息API
 */
export const frpGetInfoApi = (proxyName: string) => {
  return defHttp.get({
    url: `${EDeviceConfigApi.FRP_API}${proxyName}`,
  });
};

export const frpPutInfoApi = (proxyName: string, enableRemote: number) => {
  return defHttp.put({
    url: `${EDeviceConfigApi.FRP_API}${proxyName}/${enableRemote}`,
  });
};

export const getTbDeviceId = (params: string) => {
  return defHttp.get({
    url: `${EDeviceConfigApi.GET_TB_DEVICE_ID}/${params}`,
  });
};

/**
 * 命令下发记录
 */
export const deviceCommandRecordGetQuery = (params?: TDeviceConfigParams) => {
  return defHttp.get({
    url: EDeviceConfigApi.COMMODRECORD,
    params,
  });
};
