<template>
  <div>
    <div class="toolbarWrapper">
      <!-- <ViewWindowSetting v-model="viewType" @update:model-value="handleUpdateViewType" /> -->
      <el-tooltip content="全屏">
        <el-icon class="el-input__icon" :size="18" style="margin-left: 10px; margin-right: 10px">
          <FullscreenExitOutlined role="full" @click="enterFullscreen" />
        </el-icon>
      </el-tooltip>
      <RenderSwitch @change-camera-render="changeRender" />
      <FenceToolbar
        :style="{ visibility: drawable ? 'visible' : 'hidden' }"
        @remove="handleRemove"
        @save="handleSave"
        @toggle-editable="toggleEditable"
        :is-edit="isEdit"
      />
    </div>
    <div
      class="cameraViewSettingWrapper"
      :style="{ width: domWidth + 'px', height: domHeight + 'px' }"
    >
      <div class="fenceEditorWrapper">
        <FenceEditor
          ref="fenceEditorRef"
          :dom-width="domWidth"
          :canvas-size="{ width: canvasWidth, height: canvasHeight }"
          :line-points="fenceStore.serverFencePoints || []"
        />
      </div>

      <div class="cameraVideo">
        <CameraLiveVideo />
      </div>
    </div>
    <div
      class="presetAddWrapper"
      :class="{ hidePresetControlCls: isEdit }"
      v-if="!!cameraDetailStore.detail?.isPtz"
    >
      <CameraDirectionControl />
      <ElButton
        type="primary"
        @click="handleAddPreset"
        size="small"
        style="margin-top: 20px; width: 100px"
        >添加预置位</ElButton
      >
      <AddPresetModal v-if="addPresetModalVisible" @close="handleClose" @ok="handleAddPresetOk" />
    </div>
  </div>
  <div class="presetWrapper">
    <PresetSelect />
  </div>

  <div class="cameraParamsSettingWrapper">
    <!-- <div class="cameraParamsSetting">
      <CameraParams />
    </div> -->
    <div class="algorithmsSetting"> <AlgorithmsSetting /> </div>
  </div>
</template>
<script lang="ts" setup>
  import { computed, ref, watchEffect } from 'vue';
  import FenceToolbar from '../FenceToolbar/FenceToolbar.vue';
  import FenceEditor from '../FenceEditorV2/FenceEditor.vue';
  import CameraLiveVideo from '../CameraLiveVideo/CameraLiveVideo.vue';
  import PresetSelect from '../PresetSelect/PresetSelect.vue';
  // import { ViewType } from '../ViewWindowSetting/types';
  import useFenceStore from '../../store/useFenceStore';
  import AddPresetModal from '../AddPresetModal/AddPresetModal.vue';
  import usePresetListStore from '../../store/usePresetListStore';
  import useCameraDetailStore from '../../store/useCameraDetailStore';
  import useCameraAlgoStore from '../../store/useCameraAlgoStore';
  import AlgorithmsSetting from '../AlgorithmsSetting/AlgorithmsSetting.vue';
  import RenderSwitch from '../RenderSwitch/RenderSwitch.vue';
  import { FullscreenExitOutlined } from '@vicons/antd';

  import { ElMessage } from 'element-plus';
  import CameraDirectionControl from '../CameraDirectionControl/CameraDirectionControl.vue';
  import { canvasHeight, canvasWidth, domHeight, domWidth } from './constants';
  import useFullscreen from 'vue-hooks-plus/lib/useFullscreen';

  const emits = defineEmits<{
    (e: 'changeTreeRender', render: number | string): unknown;
  }>();

  const [, { enterFullscreen }] = useFullscreen(() => document.querySelector('.cameraVideo'));

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

  const fenceStore = useFenceStore();

  const presetStore = usePresetListStore();
  const cameraDetailStore = useCameraDetailStore();
  const cameraAlgoStore = useCameraAlgoStore();

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

  const addPresetModalVisible = ref(false);

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

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

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

  const isEdit = ref(false);

  /** 退出编辑模式 */
  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 cameraId = cameraDetailStore.cameraId;
    if (!cameraId) {
      ElMessage.error('未选中相机');
      return;
    }
    const algoId = cameraAlgoStore.selectedAlgoId;
    if (!algoId) {
      ElMessage.error('未选中算法');
      return;
    }
    const presetToken = presetStore.currentPresetToken;
    if (!presetToken) {
      ElMessage.error('未选中预置位');
      return;
    }

    fenceStore
      .saveFence({
        cameraId: cameraId,
        algoId: algoId,
        presetToken,
        electronicFencePolygon: JSON.stringify(json),
      })
      ?.then(() => {
        ElMessage.success('更新成功');
      });
  };

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

  const changeRender = (render: string | number) => {
    emits('changeTreeRender', render);
  };

  const drawable = computed(() => {
    if (!presetStore.currentPresetToken) return false;
    if (!cameraAlgoStore.selectedAlgoId) return false;
    if (!cameraAlgoStore.selectedAlgoDetail?.electronicFenceBool) return false;
    return true;
  });

  watchEffect(() => {
    const electronicFenceBool = cameraAlgoStore.selectedAlgoDetail?.electronicFenceBool;

    if (presetStore.currentPresetToken && cameraAlgoStore.selectedAlgoId && electronicFenceBool) {
      const points = fenceStore.serverFencePoints || [];
      if (!points) {
        fenceEditorRef.value?.clear();
        return;
      }

      /** 先清空原有的 */
      fenceEditorRef.value?.clear();
      // fenceEditorRef.value?.createLines(rawLinePoints);
      fenceEditorRef.value?.setEditMode();
      isEdit.value = true;
      return;
    } else {
      fenceEditorRef.value?.clear();
      fenceEditorRef.value?.exitEditMode();
      isEdit.value = false;
    }
  });

  const handleAddPreset = () => {
    addPresetModalVisible.value = true;
  };
</script>
<style scoped>
  .cameraViewSettingWrapper {
    position: relative;
    /* border: 1px solid #ccc; */
  }
  .cameraViewOverflow {
    overflow: hidden;
    position: relative;
  }

  .cameraVideo {
    position: absolute;
    top: 0;
    left: 0;
    z-index: 8;
    background: #ccc;
    width: 100%;
    height: 100%;
  }

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

  .presetAddWrapper {
    position: absolute;
    bottom: 50px;
    right: 50px;
    flex-direction: column;
    display: flex;
    align-items: center;
    z-index: 10;
  }

  .cameraParamsSettingWrapper {
    display: flex;
    margin-top: 10px;
  }
  .algorithmsSetting {
    flex: 1;
    min-height: 300px;
    margin-left: 10px;
    /* border-left: 1px solid #ccc; */
    padding-left: 15px;
  }
  .cameraParamsSetting {
    flex-basis: 330px;
    flex-shrink: 0;
  }
  .hidePresetControlCls {
    display: none;
  }
  .fenceEditorWrapper {
    position: relative;
    z-index: 9;
  }

  .presetWrapper {
    padding: 20px;
    padding-left: 25px;
    border-bottom: 1px solid #ccc;
    padding-bottom: 10px;
    margin-bottom: 15px;
  }
</style>
