import { http } from '@/utils/http/axios';
import {
  CameraIPItem,
  CameraShowItem,
  CameraRangeItem,
  CameraSRS,
  CameraNVRItem,
} from '@/views/cameras/overview/type';

export type PaginationArrayData<T> = {
  records: T[];
  pageNumber: number;
  pageSize: number;
  totalPage: number;
  totalRow: number;
};

export type CameraQueryParams = {
  /** 相机协议类型 */
  cameraType?: string;
  /** 设备ID */
  code?: string;
  /** 相机IP */
  ip?: string;
  /** 场景 */
  sceneCode?: string;
  /** 是否接入平台 */
  integrationState?: number;
  /** 联网状态 */
  networkingState?: number;
  pageNumber?: number;
  pageSize?: number;
  sourceType?: string;
};

export enum IsPtz {
  disabled = 0,
  enbaled = 1,
}

/** 根据后端返回的相机详情 */
export interface CameraDetailServer {
  /** 相机IP */
  cameraIp: string;
  /**	相机协议类型 */
  cameraType: string;
  /** 相机端口 */
  cameraPort: string;
  /** 相机ID */
  code: string;
  /** 工位场景Id */
  workspaceId: string;
  /** 工位负责人 */
  principal?: string;
  /** 描述 */
  remark?: string;
  /** 相机名称 */
  name: string;
  /** id */
  id?: number;
  /** 用户名 */
  username?: string;
  /** 密码 */
  password?: string;
  /** 相机MAC地址 */
  cameraMac: string;
  /** 车间场景名称 */
  workshopName: string;
  /** 工位场景名称 */
  workspaceName: string;
  /** 联网状态: 0-启用, 1-禁用 */
  networkingState: number;
  /** 平台状态： 0-启用, 1-禁用 */
  integrationState: number;
  /** 状态: 0-启用, 1-禁用 */
  status: number;
  /** 相机流 */
  pushstreamIp: string;
  /** 渲染相机流 */
  pushstreamRenderUrl: string;
  /** 工位code */
  workspaceCode: string;

  /** 是否支持设置相机转动，比如设置预置位，调整视角等 */
  isPtz: IsPtz;
  /** 录制周期 */
  nvrPeriod: number;
  /** 录制开始时间 */
  nvrStartAt: string;
  /** 录制结束时间 */
  nvrEndAt: string;
  /** 分辨率 */
  resolution: number;
  //渲染
  render: number | string;
}

export const getCameraList = (params: CameraQueryParams) => {
  return http.request<PaginationArrayData<CameraDetailServer>>({
    url: '/cameraConfig/findCamera',
    method: 'get',
    params,
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
  });
};

export const addCameraItem = (data: CameraIPItem) => {
  return http.request({
    url: '/cameraConfig/saveCamera',
    method: 'post',
    data: data,
  });
};

export const getCamerasByIPRange = (params: CameraRangeItem) => {
  return http.request<CameraShowItem[]>({
    url: '/cameraConfig/getCameraByIpSegment',
    method: 'get',
    params,
  });
};

export const updateCameraItem = (data: Partial<CameraIPItem>) => {
  return http.request(
    {
      url: '/cameraConfig/updateCamera',
      method: 'put',
      data,
    },
    {
      isTransformResponse: false,
    },
  );
};

export const startCameraStream = (cameraId: number) => {
  return http.request({
    url: `/cameraConfig/startCameraStream?cameraId=${cameraId}`,
    method: 'put',
  });
};

export const stopCameraStream = (cameraId: number) => {
  return http.request({
    url: `/cameraConfig/stopCameraStream?cameraId=${cameraId}`,
    method: 'put',
  });
};

export const deleteCameraItem = (params: { cameraId: number }) => {
  return http.request({
    url: `/cameraConfig/deleteCamera?cameraId=${params.cameraId}`,
    method: 'delete',
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
  });
};

/** 获取相机状态 */
export const getCameraState = (data: { cameraCodeList: string[] }) => {
  return http.request<{ cameraCode: string; status: 0 | 1 }[]>({
    url: '/cameraConfig/getCameraStatusList',
    method: 'post',
    data,
  });
};

/** 添加流媒体相机 */
export const addSRSCameraItem = (data: CameraSRS) => {
  return http.request({
    url: '/cameraConfig/saveRstp',
    method: 'post',
    data,
  });
};

/** 添加NVR相机 */
export const addNVRCameraItem = (data: CameraNVRItem) => {
  return http.request({
    url: '/cameraConfig/saveByNVR',
    method: 'post',
    data,
  });
};
