<template>
  <div 
    class="editable-element-shape"
    :class="{
      'lock': elementInfo.lock,
      'format-painter': shapeFormatPainter,
    }"
    :style="{
      top: elementInfo.top + 'px',
      left: elementInfo.left + 'px',
      width: elementInfo.width + 'px',
      height: elementInfo.height + 'px',
    }"
  >
    <div
      class="rotate-wrapper"
      :style="{ transform: `rotate(${elementInfo.rotate}deg)` }"
    >
      <div 
        class="element-content" 
        :style="{
          opacity: elementInfo.opacity,
          filter: shadowStyle ? `drop-shadow(${shadowStyle})` : '',
          transform: flipStyle,
          color: text.defaultColor,
          fontFamily: text.defaultFontName,
        }"
        v-contextmenu="contextmenus"
        @mousedown="$event => handleSelectElement($event)"
        @mouseup="execFormatPainter()"
        @touchstart="$event => handleSelectElement($event)"
        @dblclick="startEdit()"
      >
        <svg 
          overflow="visible" 
          :width="elementInfo.width"
          :height="elementInfo.height"
        >
          <defs>
            <PatternDefs
              v-if="elementInfo.pattern"
              :id="`editable-pattern-${elementInfo.id}`" 
              :src="elementInfo.pattern"
            />
            <GradientDefs
              v-else-if="elementInfo.gradient"
              :id="`editable-gradient-${elementInfo.id}`" 
              :type="elementInfo.gradient.type"
              :colors="elementInfo.gradient.colors"
              :rotate="elementInfo.gradient.rotate"
            />
          </defs>
          <g 
            :transform="`scale(${elementInfo.width / elementInfo.viewBox[0]}, ${elementInfo.height / elementInfo.viewBox[1]}) translate(0,0) matrix(1,0,0,1,0,0)`"
          >
            <path 
              class="shape-path"
              vector-effect="non-scaling-stroke" 
              stroke-linecap="butt" 
              stroke-miterlimit="8"
              :d="elementInfo.path" 
              :fill="fill"
              :stroke="outlineColor"
              :stroke-width="outlineWidth" 
              :stroke-dasharray="strokeDashArray" 
            ></path>
          </g>
        </svg>

        <div class="shape-text" :class="[text.align, { 'editable': editable || text.content }]">
          <ProsemirrorEditor
            ref="prosemirrorEditorRef"
            v-if="editable || text.content"
            :elementId="elementInfo.id"
            :defaultColor="text.defaultColor"
            :defaultFontName="text.defaultFontName"
            :editable="!elementInfo.lock"
            :value="text.content"
            @update="({ value, ignore }) => updateText(value, ignore)"
            @blur="checkEmptyText()"
            @mousedown="$event => handleSelectElement($event, false)"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { computed, nextTick, ref, watch } from 'vue'
import { storeToRefs } from 'pinia'
import { useMainStore, useSlidesStore } from '@/store'
import type { PPTShapeElement, ShapeText } from '@/types/slides'
import type { ContextmenuItem } from '@/components/Contextmenu/types'
import useElementOutline from '@/views/components/element/hooks/useElementOutline'
import useElementShadow from '@/views/components/element/hooks/useElementShadow'
import useElementFlip from '@/views/components/element/hooks/useElementFlip'
import useElementFill from '@/views/components/element/hooks/useElementFill'
import useHistorySnapshot from '@/hooks/useHistorySnapshot'

import GradientDefs from './GradientDefs.vue'
import PatternDefs from './PatternDefs.vue'
import ProsemirrorEditor from '@/views/components/element/ProsemirrorEditor.vue'

const props = defineProps<{
  elementInfo: PPTShapeElement
  selectElement: (e: MouseEvent | TouchEvent, element: PPTShapeElement, canMove?: boolean) => void
  contextmenus: () => ContextmenuItem[] | null
}>()

const mainStore = useMainStore()
const slidesStore = useSlidesStore()
const { theme } = storeToRefs(slidesStore)
const { handleElementId, shapeFormatPainter } = storeToRefs(mainStore)

const { addHistorySnapshot } = useHistorySnapshot()

const handleSelectElement = (e: MouseEvent | TouchEvent, canMove = true) => {
  if (props.elementInfo.lock) return
  e.stopPropagation()

  props.selectElement(e, props.elementInfo, canMove)
}

const execFormatPainter = () => {
  if (!shapeFormatPainter.value) return
  const { keep, ...newProps } = shapeFormatPainter.value

  slidesStore.updateElement({
    id: props.elementInfo.id, 
    props: newProps,
  })
  
  addHistorySnapshot()
  if (!keep) mainStore.setShapeFormatPainter(null)
}

const element = computed(() => props.elementInfo)
const { fill } = useElementFill(element, 'editable')

const outline = computed(() => props.elementInfo.outline)
const { outlineWidth, outlineColor, strokeDashArray } = useElementOutline(outline)

const shadow = computed(() => props.elementInfo.shadow)
const { shadowStyle } = useElementShadow(shadow)

const flipH = computed(() => props.elementInfo.flipH)
const flipV = computed(() => props.elementInfo.flipV)
const { flipStyle } = useElementFlip(flipH, flipV)

const editable = ref(false)

watch(handleElementId, () => {
  if (handleElementId.value !== props.elementInfo.id) {
    if (editable.value) editable.value = false
  }
})

const text = computed<ShapeText>(() => {
  const defaultText: ShapeText = {
    content: '',
    align: 'middle',
    defaultFontName: theme.value.fontName,
    defaultColor: theme.value.fontColor,
  }
  if (!props.elementInfo.text) return defaultText

  return props.elementInfo.text
})

const updateText = (content: string, ignore = false) => {
  const _text = { ...text.value, content }
  slidesStore.updateElement({
    id: props.elementInfo.id, 
    props: { text: _text },
  })
  
  if (!ignore) addHistorySnapshot()
}

const checkEmptyText = () => {
  if (!props.elementInfo.text) return

  const pureText = props.elementInfo.text.content.replace(/<[^>]+>/g, '')
  if (!pureText) {
    slidesStore.removeElementProps({ id: props.elementInfo.id, propName: 'text' })
    addHistorySnapshot()
  }
}

const prosemirrorEditorRef = ref<InstanceType<typeof ProsemirrorEditor>>()
const startEdit = () => {
  editable.value = true
  nextTick(() => prosemirrorEditorRef.value && prosemirrorEditorRef.value.focus())
}
</script>

<style lang="scss" scoped>
.editable-element-shape {
  position: absolute;
  pointer-events: none;
  background-size: contain;

  &.lock .element-content {
    cursor: default;
  }
  &.format-painter .element-content {
    cursor: url(data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMzQiIGhlaWdodD0iMTYiIGZpbGw9Im5vbmUiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+PHBhdGggZD0iTTIuNzUgMTMuNzY0VjEuNDIxYS4zLjMgMCAwMS40NDgtLjI2bDEwLjkxIDYuMTk3YS4zLjMgMCAwMS0uMTE2LjU1OWwtNC4xOTYuNDQyIDIuNTgyIDQuNDcyYS4zLjMgMCAwMS0uMTEuNDFsLTMuMTg0IDEuODM4YS4zLjMgMCAwMS0uNDEtLjExbC0yLjU4MS00LjQ3Mi0yLjgxIDMuNDU2YS4zLjMgMCAwMS0u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MDEwIDIuMTIxbC0uNjguNjh6IiBzdHJva2U9IiMzMzMiIHN0cm9rZS13aWR0aD0iMS41IiBzdHJva2UtbGluZWNhcD0icm91bmQiLz48L3N2Zz4=) 2 5, default !important;
  }
}
.rotate-wrapper {
  width: 100%;
  height: 100%;
}
.element-content {
  width: 100%;
  height: 100%;
  position: relative;
  cursor: move;

  svg {
    transform-origin: 0 0;
    overflow: visible;
    display: block;
  }

  .shape-path {
    pointer-events: all;
  }
}
.shape-text {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  flex-direction: column;
  padding: 10px;
  line-height: 1.2;
  word-break: break-word;
  pointer-events: none;

  &.editable {
    pointer-events: all;
  }

  &.top {
    justify-content: flex-start;
  }
  &.middle {
    justify-content: center;
  }
  &.bottom {
    justify-content: flex-end;
  }
}
</style>
