<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 bg-indigo-500 hover:bg-indigo-600 rounded text-white 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 justify-between items-center">
            {{ action.action }}
            <button class="btn-red px-4 py-1.5 min-w-24" type="button" @click.prevent="() => spriteActions.splice(spriteActions.indexOf(action), 1)">Delete</button>
          </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-half">
              <label for="is-animated">Is animated</label>
              <select v-model="action.isAnimated" class="input-field" name="is-animated">
                <option :value="false">No</option>
                <option :value="true">Yes</option>
              </select>
            </div>
            <div class="form-field-half" v-if="action.isAnimated">
              <label for="is-looping">Is looping</label>
              <select v-model="action.isLooping" class="input-field" name="is-looping">
                <option :value="false">No</option>
                <option :value="true">Yes</option>
              </select>
            </div>
            <div class="form-field-full" v-if="action.isAnimated">
              <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" />
            </div>
          </form>
        </template>
      </Accordion>
    </div>
  </div>
</template>

<script setup lang="ts">
import type { Sprite, SpriteAction } from '@/application/types'
import { uuidv4 } from '@/application/utilities'
import SpriteActionsInput from '@/components/gameMaster/assetManager/partials/sprite/partials/SpriteImagesInput.vue'
import Accordion from '@/components/utilities/Accordion.vue'
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[]>([])

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

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

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

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

function refreshSpriteList(unsetSelectedSprite = true) {
  gameStore.connection?.emit('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,
          isAnimated: action.isAnimated,
          isLooping: action.isLooping,
          frameRate: action.frameRate,
          frameWidth: action.frameWidth,
          frameHeight: action.frameHeight
        }
      }) ?? []
  }

  gameStore.connection?.emit('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(),
    spriteId: selectedSprite.value.id,
    sprite: selectedSprite.value,
    action: 'new_action',
    sprites: [],
    originX: 0,
    originY: 0,
    isAnimated: false,
    isLooping: false,
    frameRate: 0,
    frameWidth: 0,
    frameHeight: 0
  }

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

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

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

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

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

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