<template>
  <Modal :is-modal-open="showTeleportModal" @modal:close="() => zoneEditorStore.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="toZoneId">Zone to teleport to</label>
              <select v-model="toZoneId" class="input-field" name="toZoneId" id="toZoneId">
                <option :value="0">Select zone</option>
                <option v-for="zone in zoneEditorStore.zoneList" :key="zone.id" :value="zone.id">{{ zone.name }}</option>
              </select>
            </div>
          </div>
        </form>
      </div>
    </template>
  </Modal>
</template>

<script setup lang="ts">
import { computed, onMounted, ref, watch } from 'vue'
import Modal from '@/components/utilities/Modal.vue'
import { useZoneEditorStore } from '@/stores/zoneEditorStore'
import { useGameStore } from '@/stores/gameStore'
import type { Zone } from '@/types'

const showTeleportModal = computed(() => zoneEditorStore.tool === 'pencil' && zoneEditorStore.drawMode === 'teleport')
const zoneEditorStore = useZoneEditorStore()
const gameStore = useGameStore()

onMounted(fetchZones)

function fetchZones() {
  gameStore.connection?.emit('gm:zone_editor:zone:list', {}, (response: Zone[]) => {
    zoneEditorStore.setZoneList(response)
  })
}

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

function useRefTeleportSettings() {
  const settings = zoneEditorStore.teleportSettings
  return {
    toPositionX: ref(settings.toPositionX),
    toPositionY: ref(settings.toPositionY),
    toRotation: ref(settings.toRotation),
    toZoneId: ref(settings.toZoneId)
  }
}

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

function updateTeleportSettings() {
  zoneEditorStore.setTeleportSettings({
    toPositionX: toPositionX.value,
    toPositionY: toPositionY.value,
    toRotation: toRotation.value,
    toZoneId: toZoneId.value
  })
}
</script>
