<template>
  <div class="editable-element-frame"
    :style="{
      top: elementInfo.top + 'px',
      left: elementInfo.left + 'px',
      width: elementInfo.width + 'px',
      height: elementInfo.height + 'px',
      overflow: 'hidden',
    }"
  >
    <div
      class="rotate-wrapper"
      :style="{ transform: `rotate(${elementInfo.rotate}deg)` }"
    >
      <div 
        class="element-content" 
        v-contextmenu="contextmenus"
        @mousedown="$event => handleSelectElement($event)"
        @touchstart="$event => handleSelectElement($event)"
      >
        <!-- 视频类型（type 74）：使用 video 标签 -->
        <video 
          v-if="elementInfo.toolType === 74"
          :src="elementInfo.url"
          :width="elementInfo.width"
          :height="elementInfo.height"
          controls
          :style="{ width: '100%', height: '100%', objectFit: 'contain' }"
        ></video>
        <!-- B站视频类型（type 75）：使用 iframe -->
        <iframe 
          v-else-if="elementInfo.toolType === 75"
          :key="'bilibili-' + iframeKey"
          :src="elementInfo.url"
          :width="elementInfo.width"
          :height="elementInfo.height"
          :frameborder="0" 
          :allowfullscreen="true"
          allow="camera *; microphone *; display-capture; midi; encrypted-media; fullscreen; geolocation; clipboard-read; clipboard-write; accelerometer; autoplay; gyroscope; payment; picture-in-picture; usb; xr-spatial-tracking;"
        ></iframe>
        <!-- 其他类型：保持原有逻辑 -->
        <iframe 
          v-else-if="elementInfo.isHTML"
          :key="'html-' + iframeKey"
          :srcdoc="elementInfo.url"
          :width="elementInfo.width"
          :height="elementInfo.height"
          :frameborder="0" 
          :allowfullscreen="true"
          allow="camera *; microphone *; display-capture; midi; encrypted-media; fullscreen; geolocation; clipboard-read; clipboard-write; accelerometer; autoplay; gyroscope; payment; picture-in-picture; usb; xr-spatial-tracking;"
        ></iframe>
        <iframe 
          v-else
          :key="'src-' + iframeKey"
          :src="elementInfo.url"
          :width="elementInfo.width"
          :height="elementInfo.height"
          :frameborder="0" 
          :allowfullscreen="true"
          allow="camera *; microphone *; display-capture; midi; encrypted-media; fullscreen; geolocation; clipboard-read; clipboard-write; accelerometer; autoplay; gyroscope; payment; picture-in-picture; usb; xr-spatial-tracking;"
        ></iframe>

        <div class="drag-handler top"></div>
        <div class="drag-handler bottom"></div>
        <div class="drag-handler left"></div>
        <div class="drag-handler right"></div>

        <div class="mask" 
          v-if="handleElementId !== elementInfo.id"
          @mousedown="$event => handleSelectElement($event, false)"
          @touchstart="$event => handleSelectElement($event, false)"
        ></div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import type { PropType } from 'vue'
import { storeToRefs } from 'pinia'
import { useMainStore } from '@/store'
import type { PPTFrameElement } from '@/types/slides'
import type { ContextmenuItem } from '@/components/Contextmenu/types'
import { ref, watch } from 'vue'

const props = defineProps({
  elementInfo: {
    type: Object as PropType<PPTFrameElement>,
    required: true,
  },
  selectElement: {
    type: Function as PropType<(e: MouseEvent | TouchEvent, element: PPTFrameElement, canMove?: boolean) => void>,
    required: true,
  },
  contextmenus: {
    type: Function as PropType<() => ContextmenuItem[] | null>,
    required: true,
  },
})

const { handleElementId } = storeToRefs(useMainStore())

const iframeKey = ref(0)

watch(() => props.elementInfo.url, (newUrl, oldUrl) => {
  if (newUrl !== oldUrl) {
    iframeKey.value++
  }
})

const handleSelectElement = (e: MouseEvent | TouchEvent, canMove = true) => {
  e.stopPropagation()
  props.selectElement(e, props.elementInfo, canMove)
}
</script>

<style lang="scss" scoped>
.editable-element-frame {
  position: absolute;
  overflow: hidden;
}
.element-content {
  width: 100%;
  height: 100%;
  cursor: move;
  overflow: hidden;
  position: absolute;
}
.element-content iframe{
  width: 100%;
  height: 100%;
  cursor: move;
  overflow: hidden;
  position: absolute;
}
.element-content video {
  width: 100%;
  height: 100%;
  cursor: move;
  object-fit: contain;
  background-color: #000;
  position: absolute;
}
.drag-handler {
  position: absolute;

  &.top {
    height: 20px;
    left: 0;
    right: 0;
    top: 0;
  }
  &.bottom {
    height: 20px;
    left: 0;
    right: 0;
    bottom: 0;
  }
  &.left {
    width: 20px;
    top: 0;
    bottom: 0;
    left: 0;
  }
  &.right {
    width: 20px;
    top: 0;
    bottom: 0;
    right: 0;
  }
}
.mask {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  width: 100%;
  height: 100%;
}
.rotate-wrapper {
  width: 100%;
  height: 100%;
  overflow: hidden;
}
</style> 