<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">
        <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>
        </div>
      </div>

      <button class="btn-cyan py-2 my-4" type="button" @click.prevent="addNewImage">New action</button>
      <Accordion v-for="action in spriteActions" :key="action.id">
        <template #header>
          <div class="flex items-center">
            {{ action.action }}
            <div class="ml-auto space-x-2">
              <button class="btn-cyan px-4 py-1.5 min-w-24" type="button" @click.stop.prevent="openPreviewModal(action)">View</button>
              <button class="btn-red px-4 py-1.5 min-w-24" type="button" @click.stop.prevent="() => spriteActions.splice(spriteActions.indexOf(action), 1)">Delete</button>
            </div>
          </div>
        </template>
        <template #content>
          <form class="flex gap-2.5 flex-wrap" @submit.prevent="saveSprite">
            <div class="form-field-full">
              <label for="action">Action</label>
              <input v-model="action.action" class="input-field" type="text" name="action" placeholder="Action" />
            </div>
            <div class="form-field-half">
              <label for="origin-x">Origin X</label>
              <input v-model.number="action.originX" class="input-field" type="number" step="any" name="origin-x" placeholder="Origin X" />
            </div>
            <div class="form-field-half">
              <label for="origin-y">Origin Y</label>
              <input v-model.number="action.originY" class="input-field" type="number" step="any" name="origin-y" placeholder="Origin Y" />
            </div>
            <div class="form-field-full">
              <label for="frame-speed">Frame rate</label>
              <input v-model.number="action.frameRate" class="input-field" type="number" step="any" name="frame-speed" placeholder="Frame rate" />
            </div>
            <div class="form-field-full">
              <SpriteActionsInput v-model="action.sprites" @tempOffsetChange="(index, offset) => handleTempOffsetChange(action, index, offset)" />
            </div>
          </form>
        </template>
      </Accordion>
      <SpritePreview
        v-if="selectedAction"
        :sprites="selectedAction.sprites"
        :frame-rate="selectedAction.frameRate"
        :is-modal-open="isModalOpen"
        :temp-offset-index="tempOffsetData.index"
        :temp-offset="tempOffsetData.offset"
        @update:frame-rate="updateFrameRate"
        @update:is-modal-open="isModalOpen = $event"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import { SocketEvent } from '@/application/enums'
import type { Sprite, SpriteAction, UUID } from '@/application/types'
import { uuidv4 } from '@/application/utilities'
import SpriteActionsInput from '@/components/gameMaster/assetManager/partials/sprite/partials/SpriteImagesInput.vue'
import SpritePreview from '@/components/gameMaster/assetManager/partials/sprite/partials/SpritePreview.vue'
import Accordion from '@/components/utilities/Accordion.vue'
import { socketManager } from '@/managers/SocketManager'
import { useAssetManagerStore } from '@/stores/assetManagerStore'
import { useGameStore } from '@/stores/gameStore'
import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue'

const gameStore = useGameStore()
const assetManagerStore = useAssetManagerStore()

const selectedSprite = computed(() => assetManagerStore.selectedSprite)

const spriteName = ref('')
const spriteActions = ref<SpriteAction[]>([])
const isModalOpen = ref(false)
const selectedAction = ref<SpriteAction | null>(null)

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

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

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

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

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

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

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, (response: boolean) => {
    if (!response) {
      console.error('Failed to save sprite')
      return
    }
    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 openPreviewModal(action: SpriteAction) {
  selectedAction.value = action
  isModalOpen.value = true
}

function updateFrameRate(value: number) {
  if (selectedAction.value) {
    selectedAction.value.frameRate = value
  }
}

const tempOffsetData = ref<{ index: number | undefined; offset: { x: number; y: number } | undefined }>({
  index: undefined,
  offset: undefined
})

function handleTempOffsetChange(action: SpriteAction, index: number, offset: { x: number; y: number }) {
  if (selectedAction.value === action) {
    tempOffsetData.value = { index, offset }
  }
}

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

watch(isModalOpen, (newValue) => {
  if (!newValue) {
    selectedAction.value = null
  }
})

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

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