<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-56" :src="`${config.server_endpoint}/textures/map_objects/${selectedMapObject?.id}.png`" :alt="'Object ' + selectedMapObject?.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="mapObjectName" 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="mapObjectOriginX" 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="mapObjectOriginY" 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="mapObjectTags" @update:modelValue="mapObjectTags = $event" />
        </div>
        <div class="form-field-full">
          <label for="frame-speed">Frame rate</label>
          <input v-model="mapObjectFrameRate" 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="mapObjectFrameWidth" 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="mapObjectFrameHeight" 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 config from '@/application/config'
import type { MapObject } from '@/application/types'
import ChipsInput from '@/components/forms/ChipsInput.vue'
import { useAssetManagerStore } from '@/stores/assetManagerStore'
import { useGameStore } from '@/stores/gameStore'
import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue'

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

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

const mapObjectName = ref('')
const mapObjectTags = ref<string[]>([])
const mapObjectOriginX = ref(0)
const mapObjectOriginY = ref(0)
const mapObjectFrameRate = ref(0)
const mapObjectFrameWidth = ref(0)
const mapObjectFrameHeight = ref(0)

if (!selectedMapObject.value) {
  console.error('No map mapObject selected')
}

if (selectedMapObject.value) {
  mapObjectName.value = selectedMapObject.value.name
  mapObjectTags.value = selectedMapObject.value.tags
  mapObjectOriginX.value = selectedMapObject.value.originX
  mapObjectOriginY.value = selectedMapObject.value.originY
  mapObjectFrameRate.value = selectedMapObject.value.frameRate
  mapObjectFrameWidth.value = selectedMapObject.value.frameWidth
  mapObjectFrameHeight.value = selectedMapObject.value.frameHeight
}

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

function refreshObjectList(unsetSelectedMapObject = true) {
  gameStore.connection?.emit('gm:mapObject:list', {}, (response: MapObject[]) => {
    assetManagerStore.setMapObjectList(response)

    if (unsetSelectedMapObject) {
      assetManagerStore.setSelectedMapObject(null)
    }
  })
}

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

  gameStore.connection?.emit(
    'gm:mapObject:update',
    {
      id: selectedMapObject.value.id,
      name: mapObjectName.value,
      tags: mapObjectTags.value,
      originX: mapObjectOriginX.value,
      originY: mapObjectOriginY.value,
      frameRate: mapObjectFrameRate.value,
      frameWidth: mapObjectFrameWidth.value,
      frameHeight: mapObjectFrameHeight.value
    },
    (response: boolean) => {
      if (!response) {
        console.error('Failed to save mapObject')
        return
      }
      refreshObjectList(false)
    }
  )
}

watch(selectedMapObject, (mapObject: MapObject | null) => {
  if (!mapObject) return
  mapObjectName.value = mapObject.name
  mapObjectTags.value = mapObject.tags
  mapObjectOriginX.value = mapObject.originX
  mapObjectOriginY.value = mapObject.originY
  mapObjectFrameRate.value = mapObject.frameRate
  mapObjectFrameWidth.value = mapObject.frameWidth
  mapObjectFrameHeight.value = mapObject.frameHeight
})

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

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