import { ProfileRecord } from '../device/model/deviceConfigModel';
import {
  CreateOtaPackageParams,
  DefaultDeviceProfileInfo,
  DeviceProfileRecord,
  GetDeviceProfileInfosParams,
  GetOtaPackagesParams,
  OtaRecordDatum,
  TBResponse,
  UploadOtaPackagesParams,
} from './model';
import { defHttp } from '/@/utils/http/axios';

enum Api {
  GET_OTA_PACKAGES = '/otaPackages',
  CREATE_OTA_PACKAGES = '/otaPackage',
  UPLOAD_OTA_PACKAGES = '/otaPackage',
  DELETE_OTA_PACKAGES = '/otaPackage',
  GET_DEVICE_PROFILE_INFO_DEFAULT = '/deviceProfileInfo/default',
  GET_OTA_PACKAGE_INFO = '/otaPackage/info',
  DOWNLOAD_PACKAGE = '/otaPackage',

  GET_DEVICE_PROFILE_INFO_BY_ID = '/deviceProfileInfo',
  GET_DEVICE_PROFILE_INFOS = '/deviceProfileInfos',
}

/**
 * @description 获取ota包列表
 * @param params
 * @returns
 */
export const getOtaPackagesList = (params: GetOtaPackagesParams) => {
  return defHttp.get<TBResponse<OtaRecordDatum>>(
    {
      url: Api.GET_OTA_PACKAGES,
      params,
    },
    {
      joinPrefix: false,
    }
  );
};

/**
 * @description 创建ota包
 * @param params
 * @returns
 */
export const createOtaPackage = (params: CreateOtaPackageParams) => {
  return defHttp.post<OtaRecordDatum>(
    {
      url: Api.CREATE_OTA_PACKAGES,
      params,
    },
    { joinPrefix: false }
  );
};

/**
 * @description 上传ota包
 * @param param
 * @returns
 */
export const uploadOtaPackages = (params: UploadOtaPackagesParams) => {
  return defHttp.post(
    {
      url: `${Api.UPLOAD_OTA_PACKAGES}/${params.otaPackageId}?checksumAlgorithm=${
        params.checksumAlgorithm
      }${params.checksum ? `&checksum=${params.checksum}` : ''}`,
      params: params.file,
    },
    { joinPrefix: false }
  );
};

/**
 * @description 获取设备默认信息
 * @returns
 */
export const getDefaultDeviceProfile = () => {
  return defHttp.get<DefaultDeviceProfileInfo>(
    {
      url: Api.GET_DEVICE_PROFILE_INFO_DEFAULT,
    },
    {
      joinPrefix: false,
    }
  );
};

export const deleteOtaPackage = (id: string) => {
  return defHttp.delete(
    {
      url: `${Api.DELETE_OTA_PACKAGES}/${id}`,
    },
    { joinPrefix: false }
  );
};

export const getOtaPackageInfo = (id: string) => {
  return defHttp.get<OtaRecordDatum>(
    {
      url: `${Api.GET_OTA_PACKAGE_INFO}/${id}`,
    },
    {
      joinPrefix: false,
    }
  );
};

export const downloadPackage = (id: string) => {
  return defHttp.get({ url: `${Api.DOWNLOAD_PACKAGE}/${id}/download` }, { joinPrefix: false });
};

export const getDeviceProfileInfos = (params: GetDeviceProfileInfosParams) => {
  const { page = 0, pageSize = 10, sortOrder = 'ASC', sortProperty = 'name', textSearch } = params;
  return defHttp.get<TBResponse<DeviceProfileRecord>>(
    {
      url: `${Api.GET_DEVICE_PROFILE_INFOS}`,
      params: {
        page,
        pageSize,
        sortOrder,
        sortProperty,
        textSearch,
      },
    },
    { joinPrefix: false }
  );
};

export const getDeviceProfileInfoById = (id: string) => {
  return defHttp.get<ProfileRecord>(
    {
      url: `${Api.GET_DEVICE_PROFILE_INFO_BY_ID}/${id}`,
    },
    { joinPrefix: false }
  );
};
