<template>
  <div class="h-full overflow-auto">
    <div class="relative flex flex-col">
      <div class="flex flex-wrap gap-2 p-2.5 rounded-md default-border bg-gray mb-4">
        <div class="w-full flex flex-col">
          <label class="mb-1.5 font-titles" for="name">Name</label>
          <input v-model="spriteName" class="input-field" type="text" name="name" placeholder="New sprite" />
        </div>
        <div class="w-full flex gap-2 mt-2 pb-4 relative">
          <button class="btn-cyan px-4 py-2 flex-1 sm:flex-none sm:min-w-24" type="button" @click.prevent="saveSprite">Save</button>
          <button class="btn-red px-4 py-2 flex-1 sm:flex-none sm:min-w-24" type="button" @click.prevent="deleteSprite">Delete</button>
          <button class="btn-indigo px-4 py-2 flex-1 sm:flex-none" type="button" @click.prevent="copySprite">
            <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
              <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z" />
            </svg>
          </button>
          <button class="btn-cyan px-4" type="button" @click.prevent="addNewImage">New action</button>
        </div>
      </div>
      <div v-for="action in spriteActions" :key="action.id">
        <div class="flex flex-wrap gap-3 mb-3">
          <div v-for="(image, index) in action.sprites" :key="index" class="h-20 w-20 p-4 bg-gray-300 bg-opacity-50 rounded text-center relative group">
            <img :src="image.url" class="max-w-full max-h-full object-contain pointer-events-none" alt="Uploaded image" @load="updateImageDimensions($event, index)" />
            <div v-if="imageDimensions[index]" class="absolute bottom-1 right-1 bg-black/50 text-white text-xs px-1 py-0.5 rounded transition-opacity font-default">{{ imageDimensions[index].width }}x{{ imageDimensions[index].height }}</div>
          </div>
        </div>
        <div class="flex items-center mb-3">
          <div class="mr-3 space-x-2">
            <button class="btn-cyan px-4 py-1.5 min-w-24 text-left" type="button" @click.stop.prevent="openEditorModal(action)">
              Editor
              <div class="flex">
                <small class="text-xs font-default">{{ action.action }}</small>
              </div>
            </button>
          </div>
        </div>
      </div>
      <SpriteEditor
        v-for="[actionId, editorData] in Array.from(openEditors.entries())"
        :key="actionId"
        :sprite="selectedSprite!"
        :sprites="editorData.action.sprites"
        :frame-rate="editorData.action.frameRate"
        :is-modal-open="editorData.isOpen"
        :temp-offset-index="getTempOffsetIndex(editorData.action)"
        :temp-offset="getTempOffset(editorData.action)"
        @update:frame-rate="(value) => updateFrameRate(editorData.action, value)"
        @update:is-modal-open="(value) => handleEditorModalClose(editorData.action, value)"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import { SocketEvent } from '@/application/enums'
import type { Sprite, SpriteAction } from '@/application/types'
import { downloadCache, uuidv4 } from '@/application/utilities'
import SpriteEditor from '@/components/gameMaster/assetManager/partials/sprite/partials/SpriteEditor.vue'
import { socketManager } from '@/managers/SocketManager'
import { SpriteStorage } from '@/storage/storages'
import { useAssetManagerStore } from '@/stores/assetManagerStore'
import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue'

const assetManagerStore = useAssetManagerStore()

const selectedSprite = computed(() => assetManagerStore.selectedSprite)
const tempOffsetData = ref<Map<string, { index: number | undefined; offset: { x: number; y: number } | undefined }>>(new Map())
const spriteName = ref('')
const spriteActions = ref<SpriteAction[]>([])

const openEditors = ref(new Map<string, { action: SpriteAction; isOpen: boolean }>())

if (!selectedSprite.value) {
  console.error('No sprite selected')
}

if (selectedSprite.value) {
  spriteName.value = selectedSprite.value.name
  spriteActions.value = sortSpriteActions(selectedSprite.value.spriteActions)
}

async function deleteSprite() {
  socketManager.emit(SocketEvent.GM_SPRITE_DELETE, { id: selectedSprite.value?.id }, async (response: boolean) => {
    if (!response) {
      console.error('Failed to delete sprite')
      return
    }

    await downloadCache('sprites', new SpriteStorage())
    await refreshSpriteList()
  })
}

async function copySprite() {
  socketManager.emit(SocketEvent.GM_SPRITE_COPY, { id: selectedSprite.value?.id }, async (response: boolean) => {
    if (!response) {
      console.error('Failed to copy sprite')
      return
    }

    await downloadCache('sprites', new SpriteStorage())
    await refreshSpriteList(false)
  })
}

async function refreshSpriteList(unsetSelectedSprite = true) {
  socketManager.emit(SocketEvent.GM_SPRITE_LIST, {}, (response: Sprite[]) => {
    assetManagerStore.setSpriteList(response)

    if (unsetSelectedSprite) {
      assetManagerStore.setSelectedSprite(null)
    }
  })
}

async function saveSprite() {
  if (!selectedSprite.value) {
    console.error('No sprite selected')
    return
  }

  const updatedSprite = {
    id: selectedSprite.value.id,
    name: spriteName.value,
    spriteActions:
      spriteActions.value?.map((action) => {
        return {
          action: action.action,
          sprites: action.sprites,
          originX: action.originX,
          originY: action.originY,
          frameRate: action.frameRate,
          frameWidth: action.frameWidth,
          frameHeight: action.frameHeight
        }
      }) ?? []
  }

  socketManager.emit(SocketEvent.GM_SPRITE_UPDATE, updatedSprite, async (response: boolean) => {
    if (!response) {
      console.error('Failed to save sprite')
      return
    }

    await downloadCache('sprites', new SpriteStorage())
    await refreshSpriteList(false)
  })
}

function addNewImage() {
  if (!selectedSprite.value) return

  const newImage: SpriteAction = {
    id: uuidv4(),
    sprite: selectedSprite.value.id,
    action: 'new_action',
    sprites: [],
    originX: 0,
    originY: 0,
    frameRate: 0,
    frameWidth: 0,
    frameHeight: 0
  }

  if (!spriteActions.value) {
    spriteActions.value = []
  }

  spriteActions.value = sortSpriteActions([...spriteActions.value, newImage])
}

function sortSpriteActions(actions: SpriteAction[]): SpriteAction[] {
  if (!actions) return []
  return [...actions].sort((a, b) => a.action.localeCompare(b.action))
}

function openEditorModal(action: SpriteAction) {
  const newOpenEditors = new Map(openEditors.value)
  newOpenEditors.set(action.id, { action, isOpen: true })
  openEditors.value = newOpenEditors
}

function updateFrameRate(action: SpriteAction, value: number) {
  action.frameRate = value
}

function handleEditorModalClose(action: SpriteAction, isOpen: boolean) {
  if (isOpen) return
  const newOpenEditors = new Map(openEditors.value)
  newOpenEditors.delete(action.id)
  openEditors.value = newOpenEditors
}

function handleTempOffsetChange(action: SpriteAction, index: number, offset: { x: number; y: number }) {
  const newTempOffsetData = new Map(tempOffsetData.value)
  newTempOffsetData.set(action.id, { index, offset })
  tempOffsetData.value = newTempOffsetData
}

function getTempOffsetIndex(action: SpriteAction): number | undefined {
  return tempOffsetData.value.get(action.id)?.index
}

function getTempOffset(action: SpriteAction): { x: number; y: number } | undefined {
  return tempOffsetData.value.get(action.id)?.offset
}

watch(selectedSprite, (sprite: Sprite | null) => {
  if (!sprite) return
  spriteName.value = sprite.name
  spriteActions.value = sortSpriteActions(sprite.spriteActions)
  openEditors.value = new Map()
})

interface SpriteImage {
  url: string
  offset: {
    x: number
    y: number
  }
}

const imageDimensions = ref<{ [key: number]: { width: number; height: number } }>({})

const updateImageDimensions = (event: Event, index: number) => {
  const img = event.target as HTMLImageElement
  imageDimensions.value[index] = {
    width: img.naturalWidth,
    height: img.naturalHeight
  }
}

onMounted(() => {
  if (!selectedSprite.value) return
})

onBeforeUnmount(() => {
  assetManagerStore.setSelectedSprite(null)
})
</script>
