<template>
  <Modal ref="modalRef" @modal:close="() => mapEditorStore.setTool('move')" :modal-width="300" :modal-height="350" :is-resizable="false" bg-style="none">
    <template #modalHeader>
      <h3 class="m-0 font-medium shrink-0 text-white">Teleport settings</h3>
    </template>
    <template #modalBody>
      <div class="m-4">
        <form method="post" @submit.prevent="" class="inline">
          <div class="gap-2.5 flex flex-wrap">
            <div class="form-field-half">
              <label for="positionX">Position X</label>
              <input class="input-field" v-model="toPositionX" name="positionX" id="positionX" type="number" />
            </div>
            <div class="form-field-half">
              <label for="positionY">Position Y</label>
              <input class="input-field" v-model="toPositionY" name="positionY" id="positionY" type="number" />
            </div>
            <div class="form-field-full">
              <label for="rotation">Rotation</label>
              <select v-model="toRotation" class="input-field" name="rotation" id="rotation">
                <option :value="0">North</option>
                <option :value="2">East</option>
                <option :value="4">South</option>
                <option :value="6">West</option>
              </select>
            </div>
            <div class="form-field-full">
              <label for="toMap">Map to teleport to</label>
              <select v-model="toMap" class="input-field" name="toMap" id="toMap">
                <option :value="null">Select map</option>
                <option v-for="map in mapList" :key="map.id" :value="map">{{ map.name }}</option>
              </select>
            </div>
          </div>
        </form>
      </div>
    </template>
  </Modal>
</template>

<script setup lang="ts">
import type { Map } from '@/application/types'
import Modal from '@/components/utilities/Modal.vue'
import { useGameStore } from '@/stores/gameStore'
import { useMapEditorStore } from '@/stores/mapEditorStore'
import { computed, onMounted, ref, useTemplateRef, watch } from 'vue'

const showTeleportModal = computed(() => mapEditorStore.tool === 'pencil' && mapEditorStore.drawMode === 'teleport')
const mapEditorStore = useMapEditorStore()
const gameStore = useGameStore()
const mapList = ref<Map[]>([])
const modalRef = useTemplateRef('modalRef')

defineExpose({
  open: () => modalRef.value?.open()
})

onMounted(fetchMaps)

function fetchMaps() {
  gameStore.connection?.emit('gm:map:list', {}, (response: Map[]) => {
    mapList.value = response
  })
}

const { toPositionX, toPositionY, toRotation, toMap } = useRefTeleportSettings()

function useRefTeleportSettings() {
  const settings = mapEditorStore.teleportSettings
  return {
    toPositionX: ref(settings.toPositionX),
    toPositionY: ref(settings.toPositionY),
    toRotation: ref(settings.toRotation),
    toMap: ref(settings.toMapId)
  }
}

watch([toPositionX, toPositionY, toRotation, toMap], updateTeleportSettings)

function updateTeleportSettings() {
  mapEditorStore.setTeleportSettings({
    toPositionX: toPositionX.value,
    toPositionY: toPositionY.value,
    toRotation: toRotation.value,
    toMapId: toMap.value
  })
}
</script>
