<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="saveCharacterHair">
        <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-full">
          <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-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="removeCharacterHair">Remove</button>
      </form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { SocketEvent } from '@/application/enums';
import type { CharacterGender, CharacterHair, 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 selectedCharacterHair = computed(() => assetManagerStore.selectedCharacterHair)

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

const genderOptions: CharacterGender[] = ['MALE' as CharacterGender.MALE, 'FEMALE' as CharacterGender.FEMALE]

if (!selectedCharacterHair.value) {
  console.error('No character hair selected')
}

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

function removeCharacterHair() {
  if (!selectedCharacterHair.value) return

  gameStore.connection?.emit(SocketEvent.GM_CHARACTERHAIR_REMOVE, { id: selectedCharacterHair.value.id }, (response: boolean) => {
    if (!response) {
      console.error('Failed to remove character hair')
      return
    }
    refreshCharacterHairList()
  })
}

function refreshCharacterHairList(unsetSelectedCharacterHair = true) {
  gameStore.connection?.emit(SocketEvent.GM_CHARACTERHAIR_LIST, {}, (response: CharacterHair[]) => {
    assetManagerStore.setCharacterHairList(response)

    if (unsetSelectedCharacterHair) {
      assetManagerStore.setSelectedCharacterHair(null)
    }
  })
}

function saveCharacterHair() {
  const characterHairData = {
    id: selectedCharacterHair.value!.id,
    name: characterName.value,
    gender: characterGender.value,
    isSelectable: characterIsSelectable.value,
    spriteId: characterSpriteId.value
  }

  gameStore.connection?.emit(SocketEvent.GM_CHARACTERHAIR_UPDATE, characterHairData, (response: boolean) => {
    if (!response) {
      console.error('Failed to save character type')
      return
    }
    refreshCharacterHairList(false)
  })
}

watch(selectedCharacterHair, (characterHair: CharacterHair | null) => {
  if (!characterHair) return
  characterName.value = characterHair.name
  characterGender.value = characterHair.gender
  characterIsSelectable.value = characterHair.isSelectable
  characterSpriteId.value = characterHair.sprite?.id
})

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

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

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