<template>
  <Modal ref="modalRef" :modal-width="600" :modal-height="430" :bg-style="'none'">
    <template #modalHeader>
      <h3 class="m-0 font-medium shrink-0 text-white">Map settings</h3>
    </template>

    <template #modalBody>
      <div class="m-4">
        <div class="space-x-2">
          <button class="btn-cyan py-1.5 px-4" type="button" @click.prevent="screen = 'settings'">Settings</button>
          <button class="btn-cyan py-1.5 px-4" type="button" @click.prevent="screen = 'effects'">Effects</button>
        </div>
        <form method="post" @submit.prevent="" class="inline" v-if="screen === 'settings'">
          <div class="gap-2.5 flex flex-wrap mt-4">
            <div class="form-field-full">
              <label for="name">Name</label>
              <input class="input-field" v-model="name" name="name" id="name" />
            </div>
            <div class="form-field-half">
              <label for="width">Width</label>
              <input class="input-field" v-model="width" name="width" id="width" type="number" />
            </div>
            <div class="form-field-half">
              <label for="height">Height</label>
              <input class="input-field" v-model="height" name="height" id="height" type="number" />
            </div>
            <div class="form-field-full">
              <label for="pvp">PVP enabled</label>
              <select v-model="pvp" class="input-field" name="pvp" id="pvp">
                <option :value="false">No</option>
                <option :value="true">Yes</option>
              </select>
            </div>
          </div>
        </form>
        <form method="post" @submit.prevent="" class="inline" v-if="screen === 'effects'">
          <div v-for="(effect, index) in mapEffects" :key="effect.id" class="mb-2 flex items-center space-x-2 mt-4">
            <input class="input-field flex-grow" v-model="effect.effect" placeholder="Effect name" />
            <input class="input-field w-20" v-model.number="effect.strength" type="number" placeholder="Strength" />
            <button class="btn-red py-1 px-2" type="button" @click="removeEffect(index)">Delete</button>
          </div>
          <button class="btn-green py-1 px-2 mt-2" type="button" @click="addEffect">Add Effect</button>
        </form>
      </div>
    </template>
  </Modal>
</template>

<script setup lang="ts">
import type { UUID } from '@/application/types'
import { uuidv4 } from '@/application/utilities'
import Modal from '@/components/utilities/Modal.vue'
import { useMapEditorComposable } from '@/composables/useMapEditorComposable'
import { ref, useTemplateRef, watch } from 'vue'

const mapEditor = useMapEditorComposable()
const screen = ref('settings')

const name = ref(mapEditor.currentMap.value?.name)
const width = ref(mapEditor.currentMap.value?.width)
const height = ref(mapEditor.currentMap.value?.height)
const pvp = ref(mapEditor.currentMap.value?.pvp)
const mapEffects = ref(mapEditor.currentMap.value?.mapEffects || [])
const modalRef = useTemplateRef('modalRef')

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

watch(name, (value) => {
  mapEditor.updateProperty('name', value!)
})

watch(width, (value) => {
  mapEditor.updateProperty('width', value!)
})

watch(height, (value) => {
  mapEditor.updateProperty('height', value!)
})

watch(pvp, (value) => {
  mapEditor.updateProperty('pvp', value!)
})

watch(mapEffects, (value) => {
  mapEditor.updateProperty('mapEffects', value!)
})

const addEffect = () => {
  mapEffects.value.push({
    id: uuidv4() as UUID, // Simple unique id generation
    map: mapEditor.currentMap.value!,
    effect: '',
    strength: 1
  })
}

const removeEffect = (index: number) => {
  mapEffects.value.splice(index, 1)
}
</script>
