<template>
  <div class="h-full overflow-auto">
    <div class="relative p-2.5 flex flex-col items-center justify-center h-72 rounded-md default-border bg-gray">
      <img class="max-h-72" :src="`${config.server_endpoint}/textures/tiles/${selectedTile?.id}.png`" :alt="'Tile ' + selectedTile?.id" />
    </div>
    <div class="mt-5 block">
      <form class="flex gap-2.5 flex-wrap" @submit.prevent="saveTile">
        <div class="form-field-full">
          <label for="name">Name</label>
          <input v-model="tileName" class="input-field" type="text" name="name" placeholder="Tile #1" />
        </div>
        <div class="form-field-full">
          <label for="origin-x">Tags</label>
          <ChipsInput v-model="tileTags" @update:modelValue="tileTags = $event" />
        </div>
        <div class="flex gap-4">
          <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="deleteTile">Delete</button>
        </div>
      </form>
    </div>
  </div>
</template>

<script setup lang="ts">
import config from '@/application/config'
import type { Tile } from '@/application/types'
import ChipsInput from '@/components/forms/ChipsInput.vue'
import { TileStorage } from '@/storage/storages'
import { useAssetManagerStore } from '@/stores/assetManagerStore'
import { useGameStore } from '@/stores/gameStore'
import { computed, onBeforeUnmount, onMounted, ref, toRaw, watch } from 'vue'

const gameStore = useGameStore()
const assetManagerStore = useAssetManagerStore()
const tileStorage = new TileStorage()

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

const tileName = ref('')
const tileTags = ref<string[]>([])

if (!selectedTile.value) {
  console.error('No tile selected')
}

if (selectedTile.value) {
  tileName.value = selectedTile.value.name
  tileTags.value = selectedTile.value.tags
}

watch(selectedTile, (tile: Tile | null) => {
  if (!tile) return
  tileName.value = tile.name
  tileTags.value = tile.tags
})

async function deleteTile() {
  gameStore.connection?.emit('gm:tile:delete', { id: selectedTile.value?.id }, async (response: boolean) => {
    if (!response) {
      console.error('Failed to delete tile')
      return
    }
    await tileStorage.delete(selectedTile.value!.id)
    refreshTileList()
  })
}

function refreshTileList(unsetSelectedTile = true) {
  gameStore.connection?.emit('gm:tile:list', {}, (response: Tile[]) => {
    assetManagerStore.setTileList(response)

    if (unsetSelectedTile) {
      assetManagerStore.setSelectedTile(null)
    }
  })
}

function saveTile() {
  if (!selectedTile.value) {
    console.error('No tile selected')
    return
  }

  gameStore.connection?.emit(
    'gm:tile:update',
    {
      id: selectedTile.value.id,
      name: tileName.value,
      tags: tileTags.value
    },
    (response: boolean) => {
      if (!response) {
        console.error('Failed to save tile')
        return
      }
      refreshTileList(false)
    }
  )
}

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

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