<template></template>

<script setup lang="ts">
import { computed, ref } from "vue";
import { loadModel, fuseModel as FModel } from "@/model";
import { fetchScene, fetchScenesAll, getSceneUrl, Scene, SceneType } from "@/api";
import { createFuseModels, defTitle, fuseModels, SceneStatus } from "@/store";
import {
  custom,
  params,
  showModelsMapStack,
  showRightPanoStack,
  viewModeStack,
} from "@/env";
import { Dialog } from "bill/index";
import { useViewStack, useActive } from "@/hook";
import { sdk } from "@/sdk";

const active = useActive();
let pop: () => void;
const loadSignModel = async () => {
  showRightPanoStack.push(ref(false));
  viewModeStack.push(ref("full"));
  let scene: Scene | undefined;
  if ("modelId" in params) {
    const mscene = await fetchScene(Number(params.modelId));
    if (mscene.status !== SceneStatus.SUCCESS) {
      scene = mscene;
    }
  } else if ("m" in params) {
    const SSscenes = await fetchScenesAll({
      numList: [params.m!],
      type: SceneType.SWSSMX,
    });
    const YDscenes = await fetchScenesAll({
      numList: [params.m!],
      type: SceneType.SWYDMX,
    });
    const scenes = [...SSscenes, ...YDscenes];
    scene = scenes.find((scene) => scene.num === params.m);
    console.log(scene);
  }

  if (!scene) {
    return Dialog.alert(`模型不存在！`);
  }
  if (active.value) {
    defTitle.value = scene.title || scene.modelTitle;

    const fuseModel = createFuseModels({
      modelId: scene.modelId,
      show: true,
      raw: scene,
      url: getSceneUrl(scene),
      type: scene.type,
      opacity: 100,
      modelType: scene.modelDateType,
    });
    fuseModels.value.push(fuseModel);

    await loadModel(FModel);
    pop = showModelsMapStack.push(ref(new Map([[fuseModel, true]])));
    (window as any).__sdk = sdk;
  }
};

useViewStack(() => {
  const bcModels = fuseModels.value;
  fuseModels.value = [];

  loadSignModel();
  return () => {
    fuseModels.value = bcModels;
    pop && pop();
  };
});
</script>
