<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-56" :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>
        <div class="w-full flex flex-col mb-5">
          <label class="mb-1.5 font-titles" for="origin-x">Is animated</label>
          <select v-model="objectIsAnimated" class="input-cyan" name="is-animated">
            <option :value="false">No</option>
            <option :value="true">Yes</option>
          </select>
        </div>
        <div class="w-full flex flex-col mb-5">
          <label class="mb-1.5 font-titles" for="frame-speed">Frame speed</label>
          <input v-model="objectFrameSpeed" class="input-cyan" type="number" step="any" name="frame-speed" placeholder="Frame speed" />
        </div>
        <div class="w-[calc(50%_-_5px)] flex flex-col mb-5">
          <label class="mb-1.5 font-titles" for="frame-width">Frame width</label>
          <input v-model="objectFrameWidth" class="input-cyan" type="number" step="any" name="frame-width" placeholder="Frame width" />
        </div>
        <div class="w-[calc(50%_-_5px)] flex flex-col mb-5">
          <label class="mb-1.5 font-titles" for="frame-height">Frame height</label>
          <input v-model="objectFrameHeight" class="input-cyan" type="number" step="any" name="frame-height" placeholder="Frame height" />
        </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, 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)
const objectIsAnimated = ref(false)
const objectFrameSpeed = ref(0)
const objectFrameWidth = ref(0)
const objectFrameHeight = 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
  objectIsAnimated.value = selectedObject.value.isAnimated
  objectFrameSpeed.value = selectedObject.value.frameSpeed
  objectFrameWidth.value = selectedObject.value.frameWidth
  objectFrameHeight.value = selectedObject.value.frameHeight
}

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,
      isAnimated: objectIsAnimated.value,
      frameSpeed: objectFrameSpeed.value,
      frameWidth: objectFrameWidth.value,
      frameHeight: objectFrameHeight.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
  objectIsAnimated.value = object.isAnimated
  objectFrameSpeed.value = object.frameSpeed
  objectFrameWidth.value = object.frameWidth
  objectFrameHeight.value = object.frameHeight
})

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

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