<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-72" :src="`${config.server_endpoint}/assets/objects/${selectedObject?.id}.png`" :alt="'Object ' + selectedObject?.id" />
      <button class="btn-bordeaux px-4 py-1.5 min-w-24" type="button" @click.prevent="removeObject">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="saveObject">
        <div class="w-full flex flex-col mb-5">
          <label class="mb-1.5 font-titles" for="name">Name</label>
          <input v-model="objectName" 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="objectOriginX" 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="objectOriginY" 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="origin-x">Tags</label>
          <ChipsInput v-model="objectTags" @update:modelValue="objectTags = $event" />
        </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 { Object } from '@/types'
import { computed, onBeforeMount, onBeforeUnmount, onMounted, ref, watch } from 'vue'
import { useAssetManagerStore } from '@/stores/assetManager'
import { useZoneEditorStore } from '@/stores/zoneEditor'
import { useGameStore } from '@/stores/game'
import config from '@/config'
import ChipsInput from '@/components/forms/ChipsInput.vue'

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

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

const objectName = ref('')
const objectTags = ref([] as string[])
const objectOriginX = ref(0)
const objectOriginY = ref(0)

if (!selectedObject.value) {
  console.error('No object selected')
}

if (selectedObject.value) {
  objectName.value = selectedObject.value.name
  objectTags.value = selectedObject.value.tags
  objectOriginX.value = selectedObject.value.origin_x
  objectOriginY.value = selectedObject.value.origin_y
}

function removeObject() {
  gameStore.connection?.emit('gm:object:remove', { object: selectedObject.value?.id }, (response: boolean) => {
    if (!response) {
      console.error('Failed to remove object')
      return
    }
    refreshObjectList()
  })
}

function refreshObjectList(unsetSelectedObject = true) {
  gameStore.connection?.emit('gm:object:list', {}, (response: Object[]) => {
    assetManagerStore.setObjectList(response)

    if (unsetSelectedObject) {
      assetManagerStore.setSelectedObject(null)
    }

    if (zoneEditorStore.active) {
      console.log('Refreshing object list for zone store')
      zoneEditorStore.setObjectList(response)
    }
  })
}

function saveObject() {
  if (!selectedObject.value) {
    console.error('No object selected')
    return
  }

  console.log(objectTags.value)
  gameStore.connection?.emit(
    'gm:object:update',
    {
      id: selectedObject.value.id,
      name: objectName.value,
      tags: objectTags.value,
      origin_x: objectOriginX.value,
      origin_y: objectOriginY.value
    },
    (response: boolean) => {
      if (!response) {
        console.error('Failed to save object')
        return
      }
      refreshObjectList(false)
    }
  )
}

watch(selectedObject, (object: Object | null) => {
  if (!object) return
  objectName.value = object.name
  objectTags.value = object.tags
  objectOriginX.value = object.origin_x
  objectOriginY.value = object.origin_y
})

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

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