<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" @input="updateValue" name="name" id="name" />
            </div>
            <div class="form-field-half">
              <label for="width">Width</label>
              <input class="input-field" v-model="width" @input="updateValue" name="width" id="width" type="number" />
            </div>
            <div class="form-field-half">
              <label for="height">Height</label>
              <input class="input-field" v-model="height" @input="updateValue" name="height" id="height" type="number" />
            </div>
            <div>
              <label class="mr-4" for="pvp">PVP enabled</label>
              <input type="checkbox" v-model="pvp" @input="updateValue" class="input-field scale-125" name="pvp" id="pvp" />
            </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 { onMounted, ref, useTemplateRef, watch } from 'vue'

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

const name = ref<string | undefined>('Map')
const width = ref<number>(0)
const height = ref<number>(0)
const pvp = ref<boolean>(false)
const mapEffects = ref(mapEditor.currentMap.value?.mapEffects || [])
const modalRef = useTemplateRef('modalRef')

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

function updateValue(event: Event) {
  let ev = event.target as HTMLInputElement
  const value = ev.name === 'pvp' ? (ev.checked ? 1 : 0) : ev.value
  mapEditor.updateProperty(ev.name as 'name' | 'width' | 'height' | 'pvp' | 'mapEffects', value)
}

watch(
  () => mapEditor.currentMap.value,
  (map) => {
    if (!map) return
    name.value = map.name
    width.value = map.width
    height.value = map.height
    pvp.value = map.pvp
    mapEffects.value = map.mapEffects
  }
)

const addEffect = () => {
  mapEffects.value.push({
    id: uuidv4(),
    effect: '',
    strength: 1
  })
  mapEditor.updateProperty('mapEffects', mapEffects.value)
}

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