/** 相机详情的store */

import { CameraDetailServer } from '@/api/camera/camera-overview';
import { useRouteQuery } from '@vueuse/router';
import dayjs from 'dayjs';
import { defineStore } from 'pinia';
import { ref } from 'vue';
import { VideoResolution } from '../components/CameraParams/types';

interface CameraParams {
  startAt: string;
  endAt: string;
  resolution: number;
  period: number;
}

/** 宽/长的比例 */
export const WIDTH_HEIGHT_RATIO = 0.5625;
/** 分辨率以1920为基础 */
export const BASE_RESOLUTION = 1920;

const defaultParams: CameraParams = {
  startAt: '',
  endAt: '',
  resolution: VideoResolution.HIGH_RESOLUTION,
  period: 30,
};

const formatDateTime = (time: string) => {
  const dayStr = dayjs().format('YYYY-MM-DD');
  return dayStr + ' ' + time;
};

const useCameraDetailStore = defineStore('cameraDetail', () => {
  const cameraId = useRouteQuery('cameraId', '', { transform: (str) => Number(str) });

  const detail = ref<CameraDetailServer | null>(null);

  /** 参数设置 */
  const params = ref<CameraParams>({ ...defaultParams });

  const setDetail = (newDetail: CameraDetailServer) => {
    detail.value = newDetail;
    params.value = {
      startAt: formatDateTime(newDetail.nvrStartAt || '00:00:00'),
      endAt: formatDateTime(newDetail.nvrEndAt || '23:59:59'),
      resolution: newDetail.resolution || VideoResolution.HIGH_RESOLUTION,
      period: newDetail.nvrPeriod || 30,
    };
  };

  const clear = () => {
    detail.value = null;
    params.value = { ...defaultParams };
  };
  return { detail, setDetail, cameraId, params, clear };
});

export default useCameraDetailStore;
