/**
 * @description: 由于总览页面和应急处置页面都需要使用到相机列表，为了避免重复请求，将相机列表存储在pinia中
 * @return {
 *  idOfOverview: 总览页面相机分组id
 *  selectedCamerasOfOverview: 总览页面已选择的相机列表
 *  selectedCameraIdsOfOverview: 总览页面已选择的相机id列表
 * }
 * @return {
 *  idOfEmergencyEvent: 应急处置页面当前应急处置事件id
 *  idOfCommandCenter: 应急处置页面当前应急处置事件相机分组id
 *  selectedCamerasOfCommandCenter: 应急处置页面已选择的相机列表
 *  selectedCameraIdsOfCommandCenter: 应急处置页面已选择的相机id列表
 * }
 */

import { ref } from 'vue';
import { defineStore } from 'pinia';
import { getCameraTreeList, CameraInfo, getMonitorCameraLists } from '@/api/disaster-overview';
import { getCommandCenterCameraLists } from '@/api/command-center';

export const useMonitorCameraEdit = defineStore('monitorCameraEdit', () => {
  // 所有相机列表
  const allCameraTree = ref();
  // 初始化所有相机列表
  const getAllCameraTree = async () => {
    const res = await getCameraTreeList();
    allCameraTree.value = res;
  };

  interface SelectedCameraIds {
    id: number;
    name: string;
    code: string;
  }
  // 用于UpdateMonitorArea.vue组件中，记录当前已选择的相机id列表
  const selectedCameraIds = ref<SelectedCameraIds[]>([]);

  /**
   * @description: 灾害防范-总览
   */
  const idOfOverview = ref<number>(); // 总览页面相机分组id
  const selectedCamerasOfOverview = ref<CameraInfo[]>([]); // 记录当前已选择的相机列表
  const selectedCameraIdsOfOverview = ref<SelectedCameraIds[]>([]);

  // 初始化总览页面相机分组id + 当前已选择的相机列表
  const getSelectedCameraIdsOfOverview = async () => {
    await getMonitorCameraLists().then((res) => {
      idOfOverview.value = res[0].id; // 总览相机分组有且只有一个，取res[0].id
      selectedCamerasOfOverview.value = res[0].children;
      selectedCameraIdsOfOverview.value = selectedCamerasOfOverview.value.map((item) => ({
        id: item.id,
        name: item.name,
        code: item.code,
      }));
      selectedCameraIds.value = selectedCameraIdsOfOverview.value;
    });
  };

  /**
   * @description: 应急管理-应急处置-指挥中心
   */

  const idOfEmergencyEvent = ref<number>(); // 当前应急处置事件id
  const idOfCommandCenter = ref<number>(); // 相机分组id
  // 记录当前已选择的相机列表
  const selectedCamerasOfCommandCenter = ref<CameraInfo[]>([]);
  const selectedCameraIdsOfCommandCenter = ref<SelectedCameraIds[]>([]);

  // 初始化总览页面相机分组id + 当前已选择的相机列表
  const getSelectedCameraIdsOfCommandCenter = async () => {
    if (!idOfEmergencyEvent.value) return;
    await getCommandCenterCameraLists(idOfEmergencyEvent.value).then((res) => {
      idOfCommandCenter.value = res.id;
      selectedCamerasOfCommandCenter.value = res.children;
      selectedCameraIdsOfCommandCenter.value = selectedCamerasOfCommandCenter.value.map((item) => ({
        id: item.id,
        name: item.name,
        code: item.code,
      }));
      selectedCameraIds.value = selectedCameraIdsOfCommandCenter.value;
    });
  };

  return {
    allCameraTree,
    getAllCameraTree,
    idOfOverview,
    selectedCamerasOfOverview,
    selectedCameraIdsOfOverview,
    getSelectedCameraIdsOfOverview,
    idOfEmergencyEvent,
    idOfCommandCenter,
    selectedCamerasOfCommandCenter,
    selectedCameraIdsOfCommandCenter,
    getSelectedCameraIdsOfCommandCenter,
    selectedCameraIds,
  };
});

export default useMonitorCameraEdit;
