<template>
  <div class="h-full overflow-auto">
    <div class="relative p-2.5 flex flex-col items-center justify-between h-72">
      <div class="filler"></div>
      <img class="max-h-56" :src="`${config.server_endpoint}/assets/sprites/${selectedSprite?.id}.png`" :alt="'Sprite ' + selectedSprite?.id" />
      <button class="btn-bordeaux px-4 py-1.5 min-w-24" type="button" @click.prevent="removeSprite">Remove</button>
      <div class="absolute left-0 bottom-0 w-full h-px bg-cyan-200"></div>
    </div>
    <div class="m-2.5 p-2.5 block">
      <form class="flex gap-2.5 flex-wrap" @submit.prevent="saveSprite">
        <div class="w-full flex flex-col mb-5">
          <label class="mb-1.5 font-titles" for="name">Name</label>
          <input v-model="spriteName" class="input-cyan" type="text" name="name" placeholder="Wall #1" />
        </div>
        <div class="w-[calc(50%_-_5px)] flex flex-col mb-5">
          <label class="mb-1.5 font-titles" for="origin-x">Origin X</label>
          <input v-model="spriteOriginX" class="input-cyan" type="number" step="any" name="origin-x" placeholder="Origin X" />
        </div>
        <div class="w-[calc(50%_-_5px)] flex flex-col mb-5">
          <label class="mb-1.5 font-titles" for="origin-y">Origin Y</label>
          <input v-model="spriteOriginY" class="input-cyan" type="number" step="any" name="origin-y" placeholder="Origin Y" />
        </div>
        <div class="w-full flex flex-col mb-5">
          <label class="mb-1.5 font-titles" for="frame-speed">Frame speed</label>
          <input v-model="spriteFrameSpeed" class="input-cyan" type="number" step="any" name="frame-speed" placeholder="Frame speed" />
        </div>
        <div class="w-[calc(50%_-_5px)] flex flex-col mb-5">
          <label class="mb-1.5 font-titles" for="frame-width">Frame width</label>
          <input v-model="spriteFrameWidth" class="input-cyan" type="number" step="any" name="frame-width" placeholder="Frame width" />
        </div>
        <div class="w-[calc(50%_-_5px)] flex flex-col mb-5">
          <label class="mb-1.5 font-titles" for="frame-height">Frame height</label>
          <input v-model="spriteFrameHeight" class="input-cyan" type="number" step="any" name="frame-height" placeholder="Frame height" />
        </div>
        <div class="w-full flex flex-col mb-5">
          <label class="mb-1.5 font-titles" for="is-looping">Is looping</label>
          <select v-model="spriteIsLooping" class="input-cyan" name="is-looping">
            <option :value="false">No</option>
            <option :value="true">Yes</option>
          </select>
        </div>
        <button class="btn-cyan px-4 py-1.5 min-w-24" type="submit">Save</button>
      </form>
    </div>
  </div>
</template>

<script setup lang="ts">
import type { Sprite } from '@/types'
import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue'
import { useAssetManagerStore } from '@/stores/assetManager'
import { useGameStore } from '@/stores/game'
import config from '@/config'

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

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

const spriteName = ref('')
const spriteOriginX = ref(0)
const spriteOriginY = ref(0)
const spriteFrameSpeed = ref(0)
const spriteFrameWidth = ref(0)
const spriteFrameHeight = ref(0)
const spriteIsLooping = ref(false)

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

if (selectedSprite.value) {
  spriteName.value = selectedSprite.value.name
  spriteOriginX.value = selectedSprite.value.origin_x
  spriteOriginY.value = selectedSprite.value.origin_y
  spriteFrameSpeed.value = selectedSprite.value.frameSpeed
  spriteFrameWidth.value = selectedSprite.value.frameWidth
  spriteFrameHeight.value = selectedSprite.value.frameHeight
  spriteIsLooping.value = selectedSprite.value.isLooping
}

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

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
  }

  gameStore.connection?.emit(
    'gm:sprite:update',
    {
      id: selectedSprite.value.id,
      name: spriteName.value,
      origin_x: spriteOriginX.value,
      origin_y: spriteOriginY.value,
      frameSpeed: spriteFrameSpeed.value,
      frameWidth: spriteFrameWidth.value,
      frameHeight: spriteFrameHeight.value,
      isLooping: spriteIsLooping.value
    },
    (response: boolean) => {
      if (!response) {
        console.error('Failed to save sprite')
        return
      }
      refreshSpriteList(false)
    }
  )
}

watch(selectedSprite, (sprite: Sprite | null) => {
  if (!sprite) return
  spriteName.value = sprite.name
  spriteOriginX.value = sprite.origin_x
  spriteOriginY.value = sprite.origin_y
  spriteFrameSpeed.value = sprite.frameSpeed
  spriteFrameWidth.value = sprite.frameWidth
  spriteFrameHeight.value = sprite.frameHeight
  spriteIsLooping.value = sprite.isLooping
})

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

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