<template>
  <Modal :is-modal-open="mapEditorStore.isSettingsModalShown" @modal:close="() => mapEditorStore.toggleSettingsModal()" :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 Modal from '@/components/utilities/Modal.vue'
import { useMapEditorStore } from '@/stores/mapEditorStore'
import { ref, watch } from 'vue'

const mapEditorStore = useMapEditorStore()
const screen = ref('settings')

mapEditorStore.setMapName(mapEditorStore.map?.name)
mapEditorStore.setMapWidth(mapEditorStore.map?.width)
mapEditorStore.setMapHeight(mapEditorStore.map?.height)
mapEditorStore.setMapPvp(mapEditorStore.map?.pvp)
mapEditorStore.setMapEffects(mapEditorStore.map?.mapEffects)

const name = ref(mapEditorStore.mapSettings?.name)
const width = ref(mapEditorStore.mapSettings?.width)
const height = ref(mapEditorStore.mapSettings?.height)
const pvp = ref(mapEditorStore.mapSettings?.pvp)
const mapEffects = ref(mapEditorStore.mapSettings?.mapEffects || [])

watch(name, (value) => {
  mapEditorStore.setMapName(value)
})

watch(width, (value) => {
  mapEditorStore.setMapWidth(value)
})

watch(height, (value) => {
  mapEditorStore.setMapHeight(value)
})

watch(pvp, (value) => {
  mapEditorStore.setMapPvp(value)
})

watch(
  mapEffects,
  (value) => {
    mapEditorStore.setMapEffects(value)
  },
  { deep: true }
)

const addEffect = () => {
  mapEffects.value.push({
    id: Date.now().toString(), // Simple unique id generation
    mapId: mapEditorStore.map?.id,
    map: mapEditorStore.map,
    effect: '',
    strength: 1
  })
}

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