<template>
  <div class="h-full overflow-auto">
    <div class="relative p-2.5 flex flex-col items-center justify-between h-[300px]">
      <div class="filler"></div>
      <img class="max-h-[280px]" :src="`${config.server_endpoint}/assets/objects/${selectedObject?.id}.png`" :alt="'Object ' + selectedObject?.id" />
      <button class="btn-bordeaux px-[15px] py-1.5 min-w-[100px]" type="button" @click.prevent="removeObject">Remove</button>
      <div class="absolute left-0 bottom-0 w-full h-[1px] 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>
        <button class="btn-cyan px-[15px] py-1.5 min-w-[100px]" type="submit">Save</button>
      </form>
    </div>
  </div>
</template>

<script setup lang="ts">
import type { Object } from '@/types'
import { computed, 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'

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

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

const objectName = ref('')
const objectOriginX = ref(0)
const objectOriginY = ref(0)

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

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

watch(selectedObject, (object: Object | null) => {
  if (!object) return
  objectName.value = object.name
  objectOriginX.value = object.origin_x
  objectOriginY.value = object.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() {
  gameStore.connection.emit('gm:object:list', {}, (response: Object[]) => {
    assetManagerStore.setObjectList(response)
    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
  }

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

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

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