<template>
  <div>
    <v-stage ref="stageAll" :config="stageConfig" @click="handleStageClick">
      <v-layer ref="layer">
        <v-image :config="bgConfig" v-show="bgImgUrl" />
        <v-group
          v-for="camera in cameras"
          :key="camera.id"
          :id="camera.id"
          :config="camera.groupConfig"
          @click="handleCameraClick(camera)"
          @mouseover="(e) => handleMouseOver(e)"
          @mouseleave="handleMouseLeave()"
          @dragstart="handleDragStart()"
        >
          <v-image :config="camera.config" />
          <v-image v-if="camera.isDefault" ref="defaultIcon" :config="defaultIconConfig" />
        </v-group>
        <v-transformer :config="transformerConfig" ref="transformer" />
      </v-layer>
    </v-stage>
    <div
      v-show="defaultShow"
      class="opt-container"
      :style="{ position: 'absolute', left: posX + 'px', top: posY + 'px' }"
    >
      <div class="opt-item" :class="{ disabled: disabledSet }" @click="setDefaultCamera"
        >设为默认相机</div
      >
      <div class="opt-item" @click="previewCamera">预览相机</div>
    </div>

    <CameraPreview
      v-if="isShow"
      :last-pos-x="posX!"
      :last-pos-y="posY!"
      :video-url="videoUrl"
      @close="closePreview"
    />

    <DefaultTip
      v-show="tipShow"
      :position="pos"
      :style="{ position: 'absolute', left: posTipX + 'px', top: posTipY + 'px' }"
    />
  </div>
</template>

<script setup lang="ts">
  import { ref, onMounted, onBeforeUnmount, watch } from 'vue';
  import { ElMessage, ElMessageBox } from 'element-plus';
  import { useGlobSetting } from '@/hooks/setting';
  import DefaultTip from '../components/DefaultTip.vue';
  import urlJoin from 'url-join';
  import cameraImgSrc from '@/assets/camera/cameraImg.png';
  import favoritesImgSrc from '@/assets/camera/favorites.png';
  import { TipPositionEnum, camerasGroupType } from '../type';
  import { cloneDeep } from 'lodash-es';
  import useMiniMap from '../use-mini-map';
  import { storeToRefs } from 'pinia';
  import { updateMinMapViewLayoutApi } from '@/api/scene/scene';
  import CameraPreview from './CameraPreview.vue';

  const globSetting = useGlobSetting();

  const miniMap = useMiniMap();
  const { shopCameraList } = storeToRefs(miniMap);

  interface DataType {
    name: string;
    code: string;
    cameraIp: string;
    remark: string;
    status: number;
    pushstreamIp: string;
    isSet: number;
    workSpaceName: string;
    integrationState?: number;
  }

  const emit = defineEmits(['changeDefaultCamera', 'sendCameraId', 'change']);
  const props = defineProps<{ filterData: DataType[] }>();
  const camImg = new Image();

  const stageConfig = ref({
    width: 800,
    height: 600,
  });

  const bgImg = new Image();
  const favoritesImg = new Image();
  favoritesImg.src = favoritesImgSrc;

  //右键点击是否出现
  const defaultShow = ref<boolean>(false);
  const posX = ref<number>();
  const posY = ref<number>();
  const posTipX = ref<number>();
  const posTipY = ref<number>();
  const pos = ref(TipPositionEnum.TOP);
  //上一次点击的相机
  const lastClickedGroupId = ref<string | null>(null);
  const lastClickedVideoUrl = ref<string | null>(null);

  const bgImgUrl = ref<string | null>('');
  const cameras = ref<camerasGroupType[]>([]);
  //默认相机id
  const defaultCameraId = ref('');
  const tipShow = ref(false);
  const disabledSet = ref(false);
  const videoUrl = ref<string>('');

  const cameraIconSize = { width: 52, height: 52 };

  //标签
  const layer = ref();
  const transformer = ref();
  const defaultIcon = ref();

  function addIntegrationState(data: camerasGroupType[], updateData: DataType[]) {
    for (let i = 0; i < data.length; i++) {
      const camera = data[i];
      const matchedCamera = updateData.find((item) => item.code === camera.id);
      if (matchedCamera) {
        camera.groupConfig.visible = matchedCamera.integrationState === 0 ? true : false;
      }
    }
  }

  const bgConfig = ref({
    x: 0,
    y: 0,
    id: 'bgImg',
    width: bgImg.width,
    height: bgImg.height,
    image: bgImg,
    name: 'bgImg',
    backgroundSize: 'cover',
  });

  //待修改
  const transformerConfig = ref({
    keepRatio: true,
    rotateAnchorOffset: 30,
    enabledAnchors: ['top-left', 'top-right', 'bottom-left', 'bottom-right'],
  });

  //背景大小变换
  const resizeContainer = (width, height) => {
    stageConfig.value.width = width;
    stageConfig.value.height = height;
    layer.value.getNode().draw();
  };

  const defaultIconConfig = ref({
    x: 18,
    y: -16,
    width: 16,
    height: 16,
    id: 'defaultIcon',
    image: favoritesImg,
  });

  //取消
  const handleMouseOver = (e) => {
    document.oncontextmenu = () => {
      return false;
    };

    const group = e.target.parent;
    if (group.id() === defaultCameraId.value) {
      tipShow.value = true;
      const stage = transformer.value.getNode().getStage();
      const defaultNode = stage.findOne('#defaultIcon');
      const tipPosition = defaultNode.absolutePosition();
      posTipX.value = Number(tipPosition?.x.toFixed(2)) || 0;
      posTipY.value = Number(tipPosition?.y.toFixed(2)) || 0;
      const angle = group.rotation() >= 0 ? group.rotation() : group.rotation() + 360;
      if (angle >= 30) {
        if (angle <= 150) {
          pos.value = TipPositionEnum.RIGHT;
          posTipX.value += 26;
          posTipY.value -= 17;
        } else if (angle <= 210) {
          pos.value = TipPositionEnum.BOTTOM;
          posTipY.value += 26;
          posTipX.value -= 50;
        } else {
          pos.value = TipPositionEnum.LEFT;
          posTipX.value -= 121;
          posTipY.value -= 25;
        }
      } else {
        posTipY.value -= 61;
        posTipX.value -= 43;
      }
    }
  };
  // 还原浏览器默认鼠标事件
  const handleMouseLeave = () => {
    document.oncontextmenu = () => {
      return true;
    };
    tipShow.value = false;
  };

  const handleDragStart = () => {
    tipShow.value = false;
    emit('change', true);
  };

  const handleStageClick = (e: any) => {
    defaultShow.value = false;
    disabledSet.value = false;
    document.oncontextmenu = () => {
      return false;
    };
    const parent = e.target.parent;
    //判断是否点击相机组
    if (parent.hasName('group')) {
      lastClickedGroupId.value = parent.id();
      emit('change', true);
      // 判断是否为右键点击
      if (e.evt.button === 2) {
        lastClickedGroupId.value = parent.id();
        const clickedVideoUrl = props.filterData.find(
          (item) => item.code === lastClickedGroupId.value,
        );
        if (clickedVideoUrl) {
          lastClickedVideoUrl.value = clickedVideoUrl.pushstreamIp;
        }
        isShow.value = false;
        posX.value = e.evt.offsetX + 20;
        posY.value = e.evt.offsetY;
        disabledSet.value = defaultCameraId.value === parent.id();
        defaultShow.value = true;
      }
    } else {
      lastClickedGroupId.value = null;
      //取消transformer选择
      const transformerNode = transformer.value.getNode();
      transformerNode.nodes([]);
    }
  };

  const handleCameraClick = (camera) => {
    tipShow.value = false;
    const transformerNode = transformer.value.getNode();
    const stage = transformerNode.getStage();
    const cameraNode = stage.findOne('#' + camera.id);
    // 将变换器附加到点击的相机
    transformerNode.nodes([cameraNode]);
    transformerNode.moveToTop();
    emit('change', true);
  };

  //添加相机
  const addCamera = (id: string) => {
    const existingCamera = cameras.value.find((camera) => camera.id === id);
    if (existingCamera) return;
    const config = {
      width: cameraIconSize.width,
      height: cameraIconSize.height,
      image: camImg,
      name: 'image',
      id: id,
    };
    const groupConfig = {
      x: 50,
      y: 50,
      draggable: true,
      name: 'group',
    };
    const cameraDetail = {
      id,
      groupConfig,
      config,
      isDefault: false,
    };
    cameras.value.push(cameraDetail);
    //当只有一个相机时，自动设置默认相机
    if (cameras.value.length === 1) {
      cameras.value[0].isDefault = true;
      defaultCameraId.value = id;
    }
    emit('change', true);
  };

  //设置默认相机
  const setDefaultCamera = () => {
    //选中的相机id号
    defaultCameraId.value = lastClickedGroupId.value!;
    cameras.value.forEach((item) => {
      if (item.id === defaultCameraId.value) {
        item.isDefault = true;
      } else {
        item.isDefault = false;
      }
    });
    const transformerNode = transformer.value.getNode();
    const stage = transformerNode.getStage();
    transformerNode.nodes([]);
    const cameraNode = stage.findOne('#' + defaultCameraId.value);
    // 将变换器附加到点击的相机
    transformerNode.nodes([cameraNode]);
    defaultShow.value = false;
    emit('change', true);
  };

  const isShow = ref<boolean>(false);

  const previewCamera = () => {
    videoUrl.value = lastClickedVideoUrl.value!;
    isShow.value = true;
    defaultShow.value = false;
  };

  const closePreview = () => {
    isShow.value = false;
    defaultShow.value = false;
  };

  watch(
    lastClickedGroupId,
    () => {
      emit('changeDefaultCamera', lastClickedGroupId.value);
    },
    { immediate: true },
  );

  watch(
    () => cameras.value,
    () => {
      emit('sendCameraId', cameras.value);
    },
    { immediate: true, deep: true },
  );

  watch(
    () => props.filterData,
    () => {
      addIntegrationState(cameras.value, props.filterData);
    },
    { immediate: true, deep: true },
  );

  const clearBg = () => {
    bgImg.src = null as any as string;
    layer.value.getNode().draw();
  };

  //添加背景
  const addBg = (imgBg) => {
    return new Promise((resolve) => {
      bgImgUrl.value = imgBg;
      bgImg.src = urlJoin(globSetting.imgUrl!, imgBg) as string;
      bgImg.onload = () => {
        bgConfig.value.width = bgImg.width;
        bgConfig.value.height = bgImg.height;
        resizeContainer(bgImg.width, bgImg.height);
        resolve(null);
      };
    });
  };

  //根据id找到对应的相机
  const findCamera = (cameraId: string) => {
    const camera = cameras.value.find((item) => item.id === cameraId);
    return camera;
  };

  //保存布局
  const saveLayout = () => {
    const stage = transformer.value.getNode().getStage();
    const groups = stage.find('.group');
    const tempList = cloneDeep(cameras.value);
    const camerasLists = tempList.map((item, index) => {
      item.groupConfig.x = groups[index].attrs.x;
      item.groupConfig.y = groups[index].attrs.y;
      item.groupConfig.rotation = groups[index].attrs.rotation || 0;
      item.groupConfig.scaleX = groups[index].attrs.scaleX || 1;
      item.groupConfig.scaleY = groups[index].attrs.scaleY || 1;
      // item.config.url = cameraImgSrc;
      return item;
    });
    const layout = {
      stageConfig: stageConfig.value,
      bgConfig: bgConfig.value,
      bgImgUrl: bgImgUrl.value,
      defaultCameraId: defaultCameraId.value,
      cameraList: camerasLists,
    };
    emit('change', false);
    return JSON.stringify(layout);
  };

  //删除相机
  const handleKeyDown = (e) => {
    if (e.keyCode === 46 || e.code === 'Delete' || e.keyCode === 8 || e.code === 'Backspace') {
      if (lastClickedGroupId.value === defaultCameraId.value) {
        // ElMessage.error({
        //   message: '无法删除默认相机',
        // });
        // return;
        ElMessageBox.confirm('此相机为默认相机，您确认要删除此相机?', 'Warning', {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(() => {
            const index = cameras.value.findIndex((item) => item.id === lastClickedGroupId.value);
            index >= 0 && cameras.value.splice(index, 1);
            lastClickedGroupId.value = '';

            //取消transformer
            const transformerNode = transformer.value.getNode();
            transformerNode.nodes([]);
          })
          .catch(() => {});
      } else {
        const index = cameras.value.findIndex((item) => item.id === lastClickedGroupId.value);
        index >= 0 && cameras.value.splice(index, 1);
        lastClickedGroupId.value = '';

        //取消transformer
        const transformerNode = transformer.value.getNode();
        transformerNode.nodes([]);
      }
      emit('change', true);
    }
  };

  //重置
  const resetMap = () => {
    bgImgUrl.value = null;
    clearBg();
    cameras.value = [];
    lastClickedGroupId.value = null;
    lastClickedGroupId.value = null;
    videoUrl.value = '';
    isShow.value = false;
    defaultCameraId.value = '';
  };

  /** 导入布局json */
  const createMap = (layout, selectId) => {
    addBg(layout.bgImgUrl).then((_res) => {
      const unExitList = [] as any[];
      stageConfig.value = layout.stageConfig;
      defaultCameraId.value = layout.defaultCameraId;
      layout.cameraList = layout.cameraList
        ?.map((item) => {
          item.config.image = camImg;
          const width = item.config.width;
          const height = item.config.height;
          const ratio = width / height;
          /** 老版本的相机icon比例在1.4左右，新icon已经更新，比例是1:1，为了避免新icon渲染出来变形，需要调整icon大小
           * 如果图标的比例在该范围，那么会将它设置为新图标的icon大小 */
          if (1.3 < ratio && ratio < 1.5) {
            item.config.width = cameraIconSize.width;
            item.config.height = cameraIconSize.height;
          }

          return item;
        })
        ?.filter((cam) => {
          if (shopCameraList.value.findIndex((x) => x.code === cam.id) >= 0) {
            return true;
          } else {
            unExitList.push(cam.code);
            return false;
          }
        });
      if (unExitList.length > 0) {
        ElMessage.warning('部分相机不存在，已为您删除!');

        const layoutNew = cloneDeep(layout);
        updateMinMapViewLayoutApi({
          layout: JSON.stringify({ ...layoutNew, isUploadBg: true }),
          targetId: String(selectId),
        });
      }

      cameras.value = layout.cameraList;
    });
  };

  defineExpose({
    addBg,
    createMap,
    handleCameraClick,
    findCamera,
    resetMap,
    addCamera,
    resizeContainer,
    saveLayout,
  });

  onMounted(() => {
    window.addEventListener('keydown', handleKeyDown);
    camImg.src = cameraImgSrc;
  });

  onBeforeUnmount(() => {
    window.removeEventListener('keydown', handleKeyDown);
  });
</script>

<style scoped lang="scss">
  .opt-container {
    width: 160px;
    padding: 10px;
    border-radius: 5px;
    background-color: #ffffff;
    box-shadow: 5px 5px 5px #a3a5a5;
  }
  .opt-item {
    height: 30px;
    font-size: 14px;
    color: #404040;
    display: flex;
    justify-content: flex-start;
    align-items: center;
    padding-left: 8px;
    border-radius: 3px;
    cursor: pointer;

    &:hover {
      background-color: #f1f2f5;
    }
  }

  .disabled {
    background-color: #f1f2f5;
    color: #bcbdc0;
    cursor: not-allowed;
  }
</style>
