<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="saveCharacterType">
        <div class="form-field-full">
          <label for="name">Name</label>
          <input v-model="characterName" class="input-field" type="text" name="name" placeholder="Character Type Name" />
        </div>
        <div class="form-field-half">
          <label for="gender">Gender</label>
          <select v-model="characterGender" class="input-field" name="gender">
            <option v-for="gender in genderOptions" :key="gender" :value="gender">{{ gender }}</option>
          </select>
        </div>
        <div class="form-field-half">
          <label for="race">Race</label>
          <select v-model="characterRace" class="input-field" name="race">
            <option v-for="race in raceOptions" :key="race" :value="race">{{ race }}</option>
          </select>
        </div>
        <div class="form-field-full">
          <label for="isSelectable">Is selectable</label>
          <select v-model="characterIsSelectable" class="input-field" name="isSelectable">
            <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="characterSpriteId" 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="removeCharacterType">Remove</button>
      </form>
    </div>
  </div>
</template>

<script setup lang="ts">
import type { CharacterGender, CharacterRace, CharacterType, Sprite } from '@/application/types'
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 selectedCharacterType = computed(() => assetManagerStore.selectedCharacterType)

const characterName = ref('')
const characterGender = ref<CharacterGender>('MALE' as CharacterGender.MALE)
const characterRace = ref<CharacterRace>('HUMAN' as CharacterRace.HUMAN)
const characterIsSelectable = ref<boolean>(false)
const characterSpriteId = ref<string | null | undefined>(null)

const genderOptions: CharacterGender[] = ['MALE' as CharacterGender.MALE, 'FEMALE' as CharacterGender.FEMALE]
const raceOptions: CharacterRace[] = ['HUMAN' as CharacterRace.HUMAN, 'ELF' as CharacterRace.ELF, 'DWARF' as CharacterRace.DWARF, 'ORC' as CharacterRace.ORC, 'GOBLIN' as CharacterRace.GOBLIN]

if (!selectedCharacterType.value) {
  console.error('No character type selected')
}

if (selectedCharacterType.value) {
  characterName.value = selectedCharacterType.value.name
  characterGender.value = selectedCharacterType.value.gender
  characterRace.value = selectedCharacterType.value.race
  characterIsSelectable.value = selectedCharacterType.value.isSelectable
  characterSpriteId.value = selectedCharacterType.value.sprite?.id
}

function removeCharacterType() {
  if (!selectedCharacterType.value) return

  gameStore.connection?.emit('gm:characterType:remove', { id: selectedCharacterType.value.id }, (response: boolean) => {
    if (!response) {
      console.error('Failed to remove character type')
      return
    }
    refreshCharacterTypeList()
  })
}

function refreshCharacterTypeList(unsetSelectedCharacterType = true) {
  gameStore.connection?.emit('gm:characterType:list', {}, (response: CharacterType[]) => {
    assetManagerStore.setCharacterTypeList(response)

    if (unsetSelectedCharacterType) {
      assetManagerStore.setSelectedCharacterType(null)
    }
  })
}

function saveCharacterType() {
  const characterTypeData = {
    id: selectedCharacterType.value!.id,
    name: characterName.value,
    gender: characterGender.value,
    race: characterRace.value,
    isSelectable: characterIsSelectable.value,
    spriteId: characterSpriteId.value
  }

  gameStore.connection?.emit('gm:characterType:update', characterTypeData, (response: boolean) => {
    if (!response) {
      console.error('Failed to save character type')
      return
    }
    refreshCharacterTypeList(false)
  })
}

watch(selectedCharacterType, (characterType: CharacterType | null) => {
  if (!characterType) return
  characterName.value = characterType.name
  characterGender.value = characterType.gender
  characterRace.value = characterType.race
  characterIsSelectable.value = characterType.isSelectable
  characterSpriteId.value = characterType.sprite?.id
})

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

  gameStore.connection?.emit('gm:sprite:list', {}, (response: Sprite[]) => {
    assetManagerStore.setSpriteList(response)
  })
})

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