import {
  RESULT_CODE_ERROR,
  RESULT_CODE_SUCCESS,
} from '@/constants/result-constants';
import { request } from '@/plugins/http-fetch';
import { parseObjLineToHump } from '@/utils/parse-utils';

/**
 * 规则导出
 * @param data 表单数据对象
 * @returns bolb
 */
export function exportRule(data: Record<string, any>) {
  return request({
    url: `/api/rest/ueManagement/v1/elementType/pcf/objectType/ueInfo/file/export`,
    method: 'get',
    params: data,
    responseType: 'blob',
    timeout: 180_000,
  });
}

/**
 * 导入规则数据
 * @param data 表单数据对象
 * @returns object
 */
export function importRuleData(data: Record<string, any>) {
  return request({
    url: `/api/rest/ueManagement/v1/elementType/pcf/objectType/ueInfo/file/import?neId=${data.neId}&filePath=${data.filePath}&fileType=${data.fileType}`,
    method: 'put',
    data,
    timeout: 60_000,
  });
}

/**
 * 查询规则列表
 * @param query 查询参数
 * @returns object
 */
export async function listRules(query: Record<string, any>) {
  const result = await request({
    url: '/api/rest/ueManagement/v1/elementType/pcf/objectType/ueInfo',
    method: 'get',
    params: query,
  });
  let data: DataList = {
    total: 0,
    rows: [],
    code: result.code,
    msg: result.msg,
  };

  // 解析数据
  if (result.code === RESULT_CODE_SUCCESS) {
    if (result.data?.status) {
      return {
        code: RESULT_CODE_ERROR,
        msg: result.data?.cause,
        rows: result.data,
      };
    }
    if (Array.isArray(result.data.data)) {
      const rows = parseObjLineToHump(result.data.data);
      data.total = rows.length;
      data.rows = rows;
    }
  }

  // 模拟数据
  // data.rows = [
  //       {
  //         "hdrEnrich": "dnn",
  //         "imsi": "160990100000003",
  //         "msisdn": "86755900011",
  //         "pccRules": "internet|ims_sig",
  //         "qosAudio": "qos_audio",
  //         "qosVideo": "qos_video",
  //         "rfsp": 0,
  //         "sar": "def_sar",
  //         "sessRules": "internet|ims_sig",
  //         "uePolicy": "uep_001"
  //     }
  // ]

  return data;
}

/**
 * 查询规则详细
 * @param neId 网元ID
 * @returns object
 */
export async function getRule(neId: string, imsi: string) {
  const result = await request({
    url: `/api/rest/ueManagement/v1/elementType/pcf/objectType/ueInfo?neId=${neId}&imsi=${imsi}`,
    method: 'get',
  });
  // 解析数据
  if (result.code === RESULT_CODE_SUCCESS && Array.isArray(result.data.data)) {
    result.data = result.data.data[0];
  }
  return result;
}

/**
 * 修改规则
 * @param data 规则对象
 * @returns object
 */
export async function updateRule(data: Record<string, any>) {
  const result = await request({
    url: `/api/rest/ueManagement/v1/elementType/pcf/objectType/ueInfo?neId=${data.neId}`,
    method: 'put',
    data: data,
  });
  // 解析数据
  if (result.code === RESULT_CODE_SUCCESS && result.data?.status) {
    return {
      code: RESULT_CODE_ERROR,
      msg: result.data?.cause,
      data: result.data,
    };
  }
  return result;
}

/**
 * 批量修改规则
 * @param data 规则对象
 * @returns object
 */
export async function batchUpdateRule(data: Record<string, any>) {
  const result = await request({
    url: `/api/rest/ueManagement/v1/elementType/pcf/objectType/ueInfo/batch/${data.num}?neId=${data.neId}`,
    method: 'put',
    data: data,
  });
  // 解析数据
  if (result.code === RESULT_CODE_SUCCESS && result.data?.status) {
    return {
      code: RESULT_CODE_ERROR,
      msg: result.data?.cause,
      data: result.data,
    };
  }
  return result;
}

/**
 * 新增规则
 * @param data 规则对象
 * @returns object
 */
export async function addRule(data: Record<string, any>) {
  const result = await request({
    url: `/api/rest/ueManagement/v1/elementType/pcf/objectType/ueInfo?neId=${data.neId}`,
    method: 'post',
    data: data,
  });
  // 解析数据
  if (result.code === RESULT_CODE_SUCCESS && result.data?.status) {
    return {
      code: RESULT_CODE_ERROR,
      msg: result.data?.cause,
      data: result.data,
    };
  }
  return result;
}

/**
 * 批量新增规则
 * @param data 规则对象
 * @returns object
 */
export async function batchAddRule(data: Record<string, any>) {
  const result = await request({
    url: `/api/rest/ueManagement/v1/elementType/pcf/objectType/ueInfo/batch/${data.num}?neId=${data.neId}`,
    method: 'post',
    data: data,
  });
  // 解析数据
  if (result.code === RESULT_CODE_SUCCESS && result.data?.status) {
    return {
      code: RESULT_CODE_ERROR,
      msg: result.data?.cause,
      data: result.data,
    };
  }
  return result;
}

/**
 * 删除规则
 * @param data 规则对象
 * @returns object
 */
export function delRule(neId: string, data: Record<string, any>) {
  return request({
    url: `/api/rest/ueManagement/v1/elementType/pcf/objectType/ueInfo?neId=${neId}&imsi=${data.imsi}`,
    method: 'delete',
  });
}

/**
 * 批量删除规则
 * @param data 规则对象
 * @returns object
 */
export async function batchDelRule(data: Record<string, any>) {
  return request({
    url: `/api/rest/ueManagement/v1/elementType/pcf/objectType/ueInfo/batch/${data.num}?neId=${data.neId}&imsi=${data.imsi}`,
    method: 'delete',
  });
}
