import { stringify } from 'qs';
import { request } from 'umi';

export async function getList(projectId) {
  const res = await request(`/api/v1/dumu/pull-msg/${projectId}`);
  res.data.forEach((item) => {
    item.url = base64ToImageUrl(item.event_bg);
  });
  return res.data;
}

export async function getAlarmList(data) {
  const res = await request(`/api/task/v1/grade-alarm/list`, {
    method: 'POST',
    data,
  });
  return res.data;
}

function base64ToImageUrl(base64String) {
  const byteCharacters = atob(base64String);
  const byteArrays = [];

  for (let i = 0; i < byteCharacters.length; i++) {
    byteArrays.push(byteCharacters.charCodeAt(i));
  }

  const byteArray = new Uint8Array(byteArrays);
  const blob = new Blob([byteArray], { type: 'image/png' });
  const imageUrl = URL.createObjectURL(blob);

  return imageUrl;
}

export async function getHistoryList(projectId, params) {
  const res = await request(
    `/api/v1/dumu/list/${projectId}?${stringify(params)}`,
  );
  res.data.list.forEach((item) => {
    item.url = base64ToImageUrl(item.event_bg);
  });
  return { data: res.data.list };
}

export async function getCameraList(projectId, params) {
  const res = await request(
    `/api/v1/dumu/list/${projectId}?${stringify(params)}`,
  );
  return res;
}

export async function getDetail(detailId, options) {
  const res = await request(`/api/v1/dumu/detail/${detailId}`, options);
  return res;
}
//获取感知分析数据
export async function getPatrolPerception(params) {
  const res = await request(`/api/v1/patrol/perception?${stringify(params)}`);
  return res;
}
