import {
  AddDataBoardParams,
  AddDataComponentParams,
  ComponentInfoDetail,
  DataBoardList,
  DataComponentRecord,
  DeviceAttributeParams,
  DeviceAttributeRecord,
  GetDataBoardParams,
  GetMeetTheConditionsDeviceParams,
  MasterDeviceList,
  SendCommandParams,
  UpdateDataBoardLayoutParams,
  UpdateDataBoardParams,
  UpdateDataComponentParams,
} from './model';
import { defHttp } from '/@/utils/http/axios';

enum DataBoardUrl {
  GET_DATA_BOARD = '/data_board',
  ADD_DATA_BOARD = '/data_board/add',
  DELETE_DATA_BOARD = '/data_board',
  UPDATE_DATA_BOARD = '/data_board/update',
  UPDATE_DATA_BOARD_LAYOUT = '/data_board',
}

enum DataComponentUrl {
  GET_DATA_COMPONENT = '/data_component',
  ADD_DATA_COMPONENT = '/data_component',
  DELETE_DATA_COMPONENT = '/data_component',
  UPDATE_DATA_COMPONENT = '/data_component',
}

enum DataBoardShareUrl {
  GET_DATA_COMPONENT = '/noauth/share/data_board',
}

enum SendCommand {
  ONEWAY = '/plugins/rpc/oneway',
}

enum DeviceUrl {
  GET_DEVICE_MASTER = '/device/list/master',
  GET_SLAVE_DEVICE = '/device/list/slave',
  GET_DEVICE_ATTRIBUTE = '/device/attributes',
  GET_DEVICE = '/device/list',
  GET_DEVICE_RELATION = '/device/device/relation',
}

/**
 * @description 获取数据看板
 * @param params
 * @returns
 */
export const getDataBoardList = (params: GetDataBoardParams) => {
  return defHttp.get<DataBoardList>({
    url: DataBoardUrl.GET_DATA_BOARD,
    params,
  });
};

/**
 * @description 新增数据看板
 * @param params
 * @returns
 */
export const addDataBoard = (params: AddDataBoardParams) => {
  return defHttp.post({
    url: DataBoardUrl.ADD_DATA_BOARD,
    params,
  });
};

/**
 * @description 编辑数据看吧
 * @param params
 * @returns
 */
export const updateDataBoard = (params: UpdateDataBoardParams) => {
  return defHttp.post({
    url: DataBoardUrl.UPDATE_DATA_BOARD,
    params,
  });
};

/**
 * @description 删除数据看板
 * @param params
 * @returns
 */
export const deleteDataBoard = (params: string[]) => {
  return defHttp.delete({
    url: DataBoardUrl.DELETE_DATA_BOARD,
    params: { ids: params },
  });
};

/**
 * @description 更新数据组件位置
 * @param params
 * @returns
 */
export const updateDataBoardLayout = (params: UpdateDataBoardLayoutParams) => {
  return defHttp.post({
    url: `${DataBoardUrl.UPDATE_DATA_BOARD_LAYOUT}/${params.boardId}/layout/`,
    params: params.layout,
  });
};

/**
 * @description 获取数据组件
 * @param params
 * @returns
 */
export const getDataComponent = (params: string) => {
  return defHttp.get<ComponentInfoDetail>({
    url: `${DataComponentUrl.GET_DATA_COMPONENT}/${params}`,
    // params: { boardId: params },
  });
};

export const addDataComponent = (params: AddDataComponentParams) => {
  return defHttp.post<{ data: DataComponentRecord }>({
    url: `${DataComponentUrl.ADD_DATA_COMPONENT}/${params.boardId}/add`,
    params: params.record,
  });
};

/**
 * @description 删除数据组件
 * @param params
 * @returns
 */
export const deleteDataComponent = (params: { dataBoardId: string; ids: string[] }) => {
  return defHttp.delete({
    url: `${DataComponentUrl.DELETE_DATA_COMPONENT}/${params.dataBoardId}`,
    params: {
      ids: params.ids,
    },
  });
};

/**
 * @description 更新数据组件
 * @param params
 * @returns
 */
export const updateDataComponent = (params: UpdateDataComponentParams) => {
  return defHttp.post({
    url: `${DataComponentUrl.UPDATE_DATA_COMPONENT}/${params.boardId}/update`,
    params: params.record,
  });
};

/**
 * @description 分享组件信息
 * @param params
 * @returns
 */
export const getShareBoardComponentInfo = (params: { boardId: string; tenantId: string }) => {
  const { boardId, tenantId } = params;
  return defHttp.get<ComponentInfoDetail>({
    url: `${DataBoardShareUrl.GET_DATA_COMPONENT}/${boardId}/${tenantId}`,
  });
};

/**
 * @description 获取所有主设备
 * @param params
 * @returns
 */
export const getAllDeviceByOrg = (organizationId: string, deviceProfileId?: string) => {
  return defHttp.get<MasterDeviceList[]>({
    url: DeviceUrl.GET_DEVICE,
    params: { deviceProfileId, organizationId },
  });
};

/**
 * @description 获取满足条件的设备
 * @param params
 * @returns
 */
export const getMeetTheConditionsDevice = (params: GetMeetTheConditionsDeviceParams) => {
  return defHttp.get({
    url: DeviceUrl.GET_DEVICE,
    params,
  });
};

/**
 * @description 获取网关子设备
 * @param params
 * @returns
 */
export const getGatewaySlaveDevice = (params: { organizationId: string; masterId: string }) => {
  const { masterId, organizationId } = params;
  return defHttp.get<MasterDeviceList[]>({
    url: `${DeviceUrl.GET_SLAVE_DEVICE}/${organizationId}`,
    params: { masterId },
  });
};

/**
 * @description 获取设备属性
 * @param params
 * @returns
 */
export const getDeviceAttributes = (params: DeviceAttributeParams) => {
  const { deviceProfileId, dataType } = params;
  return defHttp.get<DeviceAttributeRecord[]>({
    url: `${DeviceUrl.GET_DEVICE_ATTRIBUTE}/${deviceProfileId}`,
    params: {
      dataType,
    },
  });
};

export const sendCommandOneway = (params: SendCommandParams) => {
  return defHttp.post(
    {
      url: `${SendCommand.ONEWAY}/${params.deviceId}`,
      params: params.value,
    },
    { joinPrefix: false }
  );
};

export const getDeviceRelation = (params: { deviceId: string; isSlave: boolean }) => {
  return defHttp.get<string>({
    url: DeviceUrl.GET_DEVICE_RELATION,
    params,
  });
};
