<template>
  <Modal :is-modal-open="zoneEditorStore.isSettingsModalShown" @modal:close="() => zoneEditorStore.toggleSettingsModal()" :modal-width="600" :modal-height="350">
    <template #modalHeader>
      <h3 class="m-0 font-medium shrink-0 text-white">Zone 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 zoneEffects" :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>
import { ref, watch } from 'vue'
import Modal from '@/components/utilities/Modal.vue'
import { useZoneEditorStore } from '@/stores/zoneEditorStore'

const zoneEditorStore = useZoneEditorStore()
const screen = ref('settings')

zoneEditorStore.setZoneName(zoneEditorStore.zone?.name)
zoneEditorStore.setZoneWidth(zoneEditorStore.zone?.width)
zoneEditorStore.setZoneHeight(zoneEditorStore.zone?.height)
zoneEditorStore.setZonePvp(zoneEditorStore.zone?.pvp)
zoneEditorStore.setZoneEffects(zoneEditorStore.zone?.zoneEffects)

const name = ref(zoneEditorStore.zoneSettings?.name)
const width = ref(zoneEditorStore.zoneSettings?.width)
const height = ref(zoneEditorStore.zoneSettings?.height)
const pvp = ref(zoneEditorStore.zoneSettings?.pvp)
const zoneEffects = ref(zoneEditorStore.zoneSettings?.zoneEffects || [])

watch(name, (value) => {
  zoneEditorStore.setZoneName(value)
})

watch(width, (value) => {
  zoneEditorStore.setZoneWidth(value)
})

watch(height, (value) => {
  zoneEditorStore.setZoneHeight(value)
})

watch(pvp, (value) => {
  zoneEditorStore.setZonePvp(value)
})

watch(
  zoneEffects,
  (value) => {
    zoneEditorStore.setZoneEffects(value)
  },
  { deep: true }
)

const addEffect = () => {
  zoneEffects.value.push({
    id: Date.now().toString(), // Simple unique id generation
    zoneId: zoneEditorStore.zone?.id,
    zone: zoneEditorStore.zone,
    effect: '',
    strength: 1
  })
}

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