<template>
  <Modal v-if="showTeleportModal" ref="modalRef" @modal:close="() => mapEditor.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.id">{{ 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 { useMapEditorComposable } from '@/composables/useMapEditorComposable'
import { MapStorage } from '@/storage/storages'
import { computed, onMounted, ref, useTemplateRef, watch } from 'vue'

const showTeleportModal = computed(() => mapEditor.tool.value === 'pencil' && mapEditor.drawMode.value === 'teleport')
const mapStorage = new MapStorage()
const mapEditor = useMapEditorComposable()
const modalRef = useTemplateRef('modalRef')
const mapList = ref<Map[]>([])

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

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

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

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

function updateTeleportSettings() {
  mapEditor.setTeleportSettings({
    toPositionX: toPositionX.value,
    toPositionY: toPositionY.value,
    toRotation: toRotation.value,
    toMap: toMap.value
  })
}

async function fetchMaps() {
  mapList.value = await mapStorage.getAll()
}

onMounted(async () => {
  await fetchMaps()
})
</script>
