<template>
  <div class="h-full overflow-auto">
    <div class="p-2.5 block rounded-md default-border bg-gray">
      <form class="flex gap-2.5 flex-wrap" @submit.prevent="saveItem">
        <div class="form-field-full">
          <label for="name">Name</label>
          <input v-model="itemName" class="input-field" type="text" name="name" placeholder="Item Name" />
        </div>
        <div class="form-field-full">
          <label for="description">Description</label>
          <input v-model="itemDescription" class="input-field" type="text" name="description" placeholder="Item Description" />
        </div>
        <div class="form-field-full">
          <label for="itemType">Type</label>
          <select v-model="itemType" class="input-field" name="itemType">
            <option v-for="type in itemTypeOptions" :key="type" :value="type">{{ type }}</option>
          </select>
        </div>
        <div class="form-field-full">
          <label for="rarity">Rarity</label>
          <select v-model="itemRarity" class="input-field" name="rarity">
            <option v-for="rarity in rarityOptions" :key="rarity" :value="rarity">{{ rarity }}</option>
          </select>
        </div>
        <div class="form-field-full">
          <label for="stackable">Stackable</label>
          <select v-model="itemStackable" class="input-field" name="stackable">
            <option :value="false">No</option>
            <option :value="true">Yes</option>
          </select>
        </div>
        <div class="form-field-full">
          <label for="spriteId">Sprite</label>
          <select v-model="itemSpriteId" class="input-field" name="spriteId">
            <option disabled selected value="">Select sprite</option>
            <option v-for="sprite in assetManagerStore.spriteList" :key="sprite.id" :value="sprite.id">{{ sprite.name }}</option>
          </select>
        </div>
        <button class="btn-cyan px-4 py-1.5 min-w-24" type="submit">Save</button>
        <button class="btn-red px-4 py-1.5 min-w-24" type="button" @click.prevent="removeItem">Remove</button>
      </form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { SocketEvent } from '@/application/enums'
import type { Item, ItemRarity, ItemType, Sprite } from '@/application/types'
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 selectedItem = computed(() => assetManagerStore.selectedItem)

const itemName = ref('')
const itemDescription = ref('')
const itemType = ref<ItemType>('WEAPON' as ItemType)
const itemRarity = ref<ItemRarity>('COMMON' as ItemRarity)
const itemStackable = ref<boolean>(false)
const itemSpriteId = ref<string | null | undefined>(null)

const itemTypeOptions: ItemType[] = ['WEAPON', 'HELMET', 'CHEST', 'LEGS', 'BOOTS', 'GLOVES', 'RING', 'NECKLACE']
const rarityOptions: ItemRarity[] = ['COMMON', 'UNCOMMON', 'RARE', 'EPIC', 'LEGENDARY']

if (!selectedItem.value) {
  console.error('No item selected')
}

if (selectedItem.value) {
  itemName.value = selectedItem.value.name
  itemDescription.value = selectedItem.value.description || ''
  itemType.value = selectedItem.value.itemType
  itemRarity.value = selectedItem.value.rarity
  itemStackable.value = selectedItem.value.stackable
  itemSpriteId.value = selectedItem.value.spriteId
}

function removeItem() {
  if (!selectedItem.value) return

  socketManager.emit(SocketEvent.GM_ITEM_REMOVE, { id: selectedItem.value.id }, (response: boolean) => {
    if (!response) {
      console.error('Failed to remove item')
      return
    }
    refreshItemList()
  })
}

function refreshItemList(unsetSelectedItem = true) {
  socketManager.emit(SocketEvent.GM_ITEM_LIST, {}, (response: Item[]) => {
    assetManagerStore.setItemList(response)

    if (unsetSelectedItem) {
      assetManagerStore.setSelectedItem(null)
    }
  })
}

function saveItem() {
  const itemData = {
    id: selectedItem.value!.id,
    name: itemName.value,
    description: itemDescription.value,
    itemType: itemType.value,
    rarity: itemRarity.value,
    stackable: itemStackable.value,
    spriteId: itemSpriteId.value
  }

  socketManager.emit(SocketEvent.GM_ITEM_UPDATE, itemData, (response: boolean) => {
    if (!response) {
      console.error('Failed to save item')
      return
    }
    refreshItemList(false)
  })
}

watch(selectedItem, (item: Item | null) => {
  if (!item) return
  itemName.value = item.name
  itemDescription.value = item.description || ''
  itemType.value = item.itemType
  itemRarity.value = item.rarity
  itemStackable.value = item.stackable
  itemSpriteId.value = item.spriteId
})

onMounted(() => {
  if (!selectedItem.value) return

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

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