/** 相机预览的接口 */
// cameraPreview/getList

import { http } from '@/utils/http/axios';
import qs from 'qs';

/** 相机树的结点类型 */
export enum CameraTreeNodeType {
  /** 公司 */
  company = 'company',
  /** 车间 */
  workshop = 'workshop',
  /** 工位 */
  workspace = 'workspace',
  /** 相机 */
  camera = 'camera',
}

export interface CameraTree {
  id: number;
  name: string;
  code: string;
  children: CameraTree[];
  nodeType: CameraTreeNodeType;
  pushstreamIp: string;
  integrationState: number;
}

/** 获取摄像头所在的树状结构 */
export const getCameraTree = () => {
  return http.request<CameraTree[]>({
    url: '/cameraPreview/getList',
    method: 'get',
  });
};

/** 算法是否启用 */
export enum ALGO_ENABLED_STATUS {
  // 0代表启用
  enabled = 0,
  disabled = 1,
}

/** 电子围栏是否启用 */
export enum FENCE_ENBALED_STATUS {
  enabled = 0,
  disabled = 1,
}

interface AlgoItem {
  id: number;
  name: string;
  code?: string;
  showName?: string;
  remark?: string;
  url?: string;
  pushStatement?: string;
  pushLinkPrompt?: string;
  status?: ALGO_ENABLED_STATUS;
  createdAt?: string;
  updatedAt?: string;
}
/** 查询所有的算法 */
export const getAllAlgosApi = () => {
  return http.request<AlgoItem[]>({
    url: '/cameraPreview/getAlgoList',
    method: 'get',
  });
};

/** 相机关联的算法信息 */
export interface CameraAlgoItem {
  id?: number;
  algoId: number;
  cameraId?: number;
  code: string;
  name: string;
  detectionFrequency: number;
  detectionTime: string;
  electronicFence: number;
  status: ALGO_ENABLED_STATUS;
  algoInfo: AlgoItem;
}

/** 查询某个camera下的所有算法 */
export const getCameraAlgoListApi = (cameraId: number): Promise<CameraAlgoItem[]> => {
  return http.request({
    url: '/cameraPreview/getAlgo',
    method: 'get',
    params: { cameraId },
  });
};

interface SaveCameraAlgoParam {
  algoId: number;
  cameraId: number;
  detectionFrequency: number;
  detectionTime: string;
  electronicFence: string;
  status: 0 | 1;
}

interface CreateCameraAlgoParam {
  algoIds: number[];
  cameraId: number;
}

/** 保存相机的某个算法 */
export const createCameraAlgoApi = (param: CreateCameraAlgoParam) => {
  return http.request({
    url: '/cameraPreview/saveAlgo',
    data: param,
    method: 'post',
  });
};

/** 更新相机的某个算法 */
export const updateCameraAlgoApi = (param: SaveCameraAlgoParam & { id: number }) => {
  return http.request({
    url: '/cameraPreview/updateAlgo',
    data: param,
    method: 'put',
  });
};

/** 删除相机的某个算法 */
export const deleteCameraAlgoApi = (params: { cameraId: number; algoId: number }) => {
  const paramString = qs.stringify(params);
  return http.request({
    url: '/cameraPreview/deleteAlgo?' + paramString,
    method: 'delete',
  });
};

export interface GetFenceParams {
  algoId: number;
  cameraId: number;
  presetToken: string;
}

/** 查询电子围栏 */
export const getFenceApi = (
  params: GetFenceParams,
): Promise<{ id: number; electronicFencePolygon: string }> => {
  return http.request({
    url: '/cameraPreview/getFence',
    method: 'GET',
    params,
  });
};

export interface SaveFenceParams {
  algoId: number;
  cameraId: number;
  electronicFencePolygon: string;
  presetToken: string;
}
/** 添加电子围栏 */
export const saveFenceApi = (data: SaveFenceParams) => {
  return http.request({
    url: '/cameraPreview/saveFence',
    method: 'post',
    data,
  });
};

interface UpdateFenceParams {
  algoId: number;
  cameraId: number;
  electronicFencePolygon: string;
  id: number;
  presetToken: string;
}
/** 编辑电子围栏 */
export const editFenceApi = (data: UpdateFenceParams) => {
  return http.request({
    url: '/cameraPreview/updateFence',
    method: 'put',
    data,
  });
};

/** 删除电子围栏 */
export const deleteFenceApi = (cameraAlgoPresetRelId: number) => {
  return http.request({
    url: `/cameraPreview/deleteFence?cameraAlgoPresetRelId=${cameraAlgoPresetRelId}`,
    method: 'delete',
  });
};

interface CreatePresetParam {
  presetName: string;
  cameraId: number;
}

/** 创建预置位 */
export const createPresetApi = (data: CreatePresetParam) => {
  return http.request({
    url: `/camera/createPreset`,
    method: 'post',
    data,
  });
};

interface UpdatePresetNameParam {
  presetToken: string;
  presetName: string;
  cameraId: number;
}
/** 修改预置位名称 */
export const changePresetNameApi = (data: UpdatePresetNameParam) => {
  return http.request({
    url: `/camera/changePresetName`,
    method: 'post',
    data,
  });
};

/** 删除预置位 */
export const deletePresetApi = (data: { presetToken: string; cameraId: number }) => {
  return http.request({
    url: `/camera/deletePreset`,
    method: 'post',
    data,
  });
};

interface PresetDetailItem {
  name: string;
  token: string;
  ptzposition: {
    panTilt: {
      x: number;
      y: number;
      space: null;
    };
    zoom: {
      x: number;
      space: null;
    };
  };
}

/** 获取预置位列表 */
export const getPresetListApi = (cameraId: number) => {
  return http.request<PresetDetailItem[]>({
    url: `/camera/getPresets`,
    method: 'get',
    params: { cameraId },
  });
};

/** 跳转到对应的预置位 */
export const goToPresetApi = (data: { presetToken: string; cameraId: number }) => {
  return http.request({
    url: `/camera/gotoPreset`,
    method: 'post',
    data,
  });
};

interface CameraMoveParam {
  cameraId: number;
  x: number;
  zoom: number;
  y: number;
}

/** 移动相机 */
export const cameraMoveApi = (data: CameraMoveParam) => {
  return http.request({
    url: `/camera/move`,
    method: 'post',
    data,
  });
};

interface SaveCameraParams {
  cameraId: number;
  // 视频录制结束时间（格式"HH:mm"）
  endAt: string;
  // 视频录制周期，单位是天;
  period: number;
  // 视频录制开始时间（格式"HH:mm"）
  startAt: string;
  /** 分辨率 */
  resolution: number;
}

export const saveCameraParamsApi = (data: SaveCameraParams) => {
  return http.request({
    url: `/cameraPreview/saveNvr`,
    method: 'put',
    data,
  });
};

interface RenderPara {
  render: number | string;
  cameraId: number;
}

//相机是否选择算法渲染
export const renderCamera = (data: RenderPara) => {
  return http.request({
    url: `/cameraPreview/saveRender`,
    method: 'put',
    data,
  });
};
