<template>
  <div>
    <div class="toolbarWrapper">
      <ViewWindowSetting v-model="viewType" @update:model-value="handleUpdateViewType" />
      <FenceToolbar
        @remove="handleRemove"
        @save="handleSave"
        @toggle-editable="toggleEditable"
        :is-edit="isEdit"
      />
      <PresetSelect />
    </div>

    <div class="cameraViewSettingWrapper">
      <FenceEditor ref="fenceEditorRef" />

      <div class="cameraVideo"><CameraLiveVideo /></div>
      <div class="presetAddWrapper">
        <CameraDirectionControl />
        <ElButton type="primary" @click="handleAddPreset" size="small">添加预置位</ElButton>
        <AddPresetModal
          v-if="addPresetModalVisible"
          @close="handleClose"
          @ok="handleAddPresetOk"
        /> </div
    ></div>
  </div>
</template>
<script lang="ts" setup>
  import { ref, watch } from 'vue';
  import FenceToolbar from '../FenceToolbar/FenceToolbar.vue';
  import FenceEditor from '../FenceEditor/FenceEditor.vue';
  import CameraLiveVideo from '../CameraLiveVideo/CameraLiveVideo.vue';
  import ViewWindowSetting from '../ViewWindowSetting/ViewWindowSetting.vue';
  import PresetSelect from '../PresetSelect/PresetSelect.vue';
  import { ViewType } from '../ViewWindowSetting/types';
  import useFenceStore from '../../store/useFenceStore';
  import AddPresetModal from '../AddPresetModal/AddPresetModal.vue';
  import { onMounted } from 'vue';
  import usePresetListStore from '../../store/usePresetListStore';
  import useCameraDetailStore from '../../store/useCameraDetailStore';

  const fenceEditorRef = ref<typeof FenceEditor | null>(null);

  const fenceStore = useFenceStore();

  const { data, getPresetList } = usePresetListStore();
  const cameraDetailStore = useCameraDetailStore();

  const viewType = ref<ViewType>(ViewType.window1);

  const addPresetModalVisible = ref(false);

  const handleClose = () => {
    addPresetModalVisible.value = false;
  };

  const handleAddPresetOk = () => {
    getPresetList(cameraDetailStore.cameraId);
    handleClose();
  };

  const handleRemove = () => {
    console.log('handleRemove');
    fenceEditorRef.value?.remove();
  };

  const isEdit = ref(true);

  /** 退出编辑模式 */
  const toggleEditable = (val: boolean) => {
    isEdit.value = val;
    if (val) {
      fenceEditorRef.value?.setEditMode();
    } else {
      fenceEditorRef.value?.exitEditMode();
    }
  };

  const handleSave = () => {
    const json = fenceEditorRef.value?.toObject();
    console.log('save json', json);
  };

  const handleUpdateViewType = (t: ViewType) => {
    console.log('viewType', t);
  };

  const handleUpdatePreset = (val: string) => {
    console.log('val', val);
  };
  watch(
    () => fenceStore.serverFencePoints,
    (newVal) => {
      console.log('newVal', newVal);
      if (!newVal) {
        fenceEditorRef.value?.clear();
        return;
      }
      // const rawLinePoints = newVal[0];

      const rawLinePoints = newVal.map((x) => {
        const points: number[] = [];
        x.forEach((line) => {
          points.push(line[0], line[1]);
        });
        return points;
      });
      if (!rawLinePoints) return;
      /** 先清空原有的 */
      fenceEditorRef.value?.clear();
      fenceEditorRef.value?.createLines(rawLinePoints);
    },
    { immediate: true },
  );

  const handleAddPreset = () => {
    addPresetModalVisible.value = true;
  };

  onMounted(() => {
    const cameraId = cameraDetailStore.cameraId;
    if (cameraId) {
      getPresetList(cameraId);
    }
  });
</script>
<style scoped>
  .cameraViewSettingWrapper {
    position: relative;
    width: 720px;
    height: 405px;
    border: 1px solid #ccc;
  }

  .cameraVideo {
    position: absolute;
    background: #ccc;
    width: 100%;
    height: 100%;
  }

  .toolbarWrapper {
    display: flex;
    align-items: center;
    margin-left: 25px;
    position: relative;
  }

  .presetAddWrapper {
    position: absolute;
    bottom: 50px;
    right: 50px;
    z-index: 10;
  }
</style>
