<template>
  <RightFillPano>
    <ui-group title="初始画面" borderBottom>
      <ui-group-option>
        <div class="init-pic" :class="{ disabled: isEdit }">
          <img :src="getFileUrl(setting!.cover)" class="init-puc-cover" />
          <div class="init-pic-set" @click="enterSetPic">设置</div>
        </div>
      </ui-group-option>
    </ui-group>

    <ui-group title="设置天空">
      <ui-group-option>
        <selectBack :value="setting?.back || setting?.mapId ? [setting?.back, setting?.mapId] : ['dt', 1]" @update:value="changeBack" />
      </ui-group-option>
    </ui-group>

    <ui-group title="设置定位参照模型">
      <ui-group-option>
        <p class="model-name">模型名称</p>
        <div v-if="!isEditModelName" class="model-name-input" @click="enterSetModelName">
          <span v-if="setting?.locationModelName"><img src="@/assets/orientation.svg" alt="未设置" />{{ setting?.locationModelName }}</span>
          <span v-else><img src="@/assets/orientation.svg" alt="未设置" />未设置</span>
        </div>
        <div v-else class="model-name-input">
          <img src="@/assets/orientation.svg" alt="未设置" />
          <ui-input
            style="width: 100%;"
            type="text"
            :modelValue="setting?.locationModelName"
            :maxlength="100"
            height="40px"
            @update:modelValue="(locationModelName: string) => getLocationModelName(locationModelName)"
        />
        </div>
      </ui-group-option>
    </ui-group>
  </RightFillPano>
</template>

<script lang="ts" setup>
import { RightFillPano } from "@/layout";
import { enterEdit, enterOld, setting, isEdit, updataSetting } from "@/store";
import { ref, watch } from "vue";
import { togetherCallback, getFileUrl, loadPack } from "@/utils";
import { showRightPanoStack, showRightCtrlPanoStack } from "@/env";
import { analysisPose, sdk } from "@/sdk";
import selectBack from "./select-back.vue";

const enterSetPic = () => {
  enterEdit(
    togetherCallback([
      showRightPanoStack.push(ref(false)),
      showRightCtrlPanoStack.push(ref(false)),
    ])
  );
  enterOld(async () => {
    const dataURL = await sdk.screenshot(300, 150);
    const res = await fetch(dataURL);
    const blob = await res.blob();

    setting.value = {
      ...setting.value!,
      cover: { url: dataURL, blob },
      pose: analysisPose(sdk.getPose()),
    };
    await updataSetting();
  });
};
let locationModelName = setting.value!.locationModelName
watch(() => setting.value!.locationModelName, (newVal) => {
  locationModelName = newVal;
});
let initBack = setting.value!.back;
let initMapId = setting.value!.mapId;
let isFirst = true;
let isInPutFirst = true;
let oldlocationModelName = JSON.parse(JSON.stringify(setting.value!.locationModelName))
const isEditModelName = ref<boolean>(false);
const changeBack = ([back, mapId]: [string | null, number | null]) => {
  setting.value!.back = back;
  setting.value!.mapId = mapId;

  if (isFirst) {
    let isSave = false;
    isFirst = false;
    enterEdit(() => {
      if (!isSave) {
        setting.value!.back = initBack;
        setting.value!.mapId = initMapId;
      }
      isFirst = true;
    });
    enterOld(async () => {
      initBack = setting.value!.back;
      initMapId = setting.value!.mapId;
      isSave = true;
      console.log(initBack, initMapId, locationModelName);
      await loadPack(updataSetting());
    });
  }
};

const enterSetModelName = () => {
  isEditModelName.value = true;
  let isSave = false;
  enterEdit(() => {
    if (!isSave) {
      // 不保存重置
      console.log(oldlocationModelName, 9999)
      setting.value!.locationModelName = oldlocationModelName;
    }
    isInPutFirst = true;
    isEditModelName.value = false;
  });
};

const getLocationModelName = (locationModelName: string) => {
  setting.value!.locationModelName = locationModelName;
  if(isInPutFirst){
    isInPutFirst = false
    enterOld(async () => {
      oldlocationModelName = locationModelName
      await updataSetting();
    });
    }
};


</script>

<style scoped lang="scss">
.init-pic {
  height: 150px;
  border-radius: 4px;
  overflow: hidden;
  position: relative;
}

.init-puc-cover {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.init-pic-set {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background-color: rgba(0, 0, 0, 0.5);
  font-size: 12px;
  color: #fff;
  line-height: 32px;
  z-index: 1;
  text-align: center;
  cursor: pointer;
}
.model-name{
  margin-bottom: 10px;
}
.model-name-input{
  height: 40px;
  display: flex;
  align-items: center;
  span{
    display: flex;
    align-items: center;
  }
  img{
    margin-right: 10px;
  }
}
</style>
