<template>
  <div class="h-full overflow-auto">
    <div class="relative p-2.5 flex flex-col items-center justify-center h-72 rounded-md border border-solid border-gray-500 bg-gray-700">
      <img class="max-h-56" :src="`${config.server_endpoint}/assets/objects/${selectedObject?.id}.png`" :alt="'Object ' + selectedObject?.id" />
    </div>
    <div class="mt-5 block">
      <form class="flex gap-2.5 flex-wrap" @submit.prevent="saveObject">
        <div class="form-field-full">
          <label for="name">Name</label>
          <input v-model="objectName" class="input-field" type="text" name="name" placeholder="Wall #1" />
        </div>
        <div class="form-field-half">
          <label for="origin-x">Origin X</label>
          <input v-model="objectOriginX" class="input-field" type="number" step="any" name="origin-x" placeholder="Origin X" />
        </div>
        <div class="form-field-half">
          <label for="origin-y">Origin Y</label>
          <input v-model="objectOriginY" class="input-field" type="number" step="any" name="origin-y" placeholder="Origin Y" />
        </div>
        <div class="form-field-full">
          <label for="tags">Tags</label>
          <ChipsInput v-model="objectTags" @update:modelValue="objectTags = $event" />
        </div>
        <div class="form-field-full">
          <label for="is-animated">Is animated</label>
          <select v-model="objectIsAnimated" class="input-field" name="is-animated">
            <option :value="false">No</option>
            <option :value="true">Yes</option>
          </select>
        </div>
        <div class="form-field-full">
          <label for="frame-speed">Frame rate</label>
          <input v-model="objectFrameRate" class="input-field" type="number" step="any" name="frame-speed" placeholder="Frame rate" />
        </div>
        <div class="form-field-half">
          <label for="frame-width">Frame width</label>
          <input v-model="objectFrameWidth" class="input-field" type="number" step="any" name="frame-width" placeholder="Frame width" />
        </div>
        <div class="form-field-half">
          <label for="frame-height">Frame height</label>
          <input v-model="objectFrameHeight" class="input-field" type="number" step="any" name="frame-height" placeholder="Frame height" />
        </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="removeObject">Delete</button>
        </div>
      </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/assetManagerStore'
import { useZoneEditorStore } from '@/stores/zoneEditorStore'
import { useGameStore } from '@/stores/gameStore'
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<string[]>([])
const objectOriginX = ref(0)
const objectOriginY = ref(0)
const objectIsAnimated = ref(false)
const objectFrameRate = 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.originX
  objectOriginY.value = selectedObject.value.originY
  objectIsAnimated.value = selectedObject.value.isAnimated
  objectFrameRate.value = selectedObject.value.frameRate
  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) {
      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,
      tags: objectTags.value,
      originX: objectOriginX.value,
      originY: objectOriginY.value,
      isAnimated: objectIsAnimated.value,
      frameRate: objectFrameRate.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.originX
  objectOriginY.value = object.originY
  objectIsAnimated.value = object.isAnimated
  objectFrameRate.value = object.frameRate
  objectFrameWidth.value = object.frameWidth
  objectFrameHeight.value = object.frameHeight
})

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

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