package pw.hamzantal.bloodmoon.listeners

import org.bukkit.entity.Creeper
import org.bukkit.entity.EntityType
import org.bukkit.entity.Spider
import org.bukkit.entity.Zombie
import org.bukkit.event.entity.CreatureSpawnEvent
import org.bukkit.potion.PotionEffect
import org.bukkit.potion.PotionEffectType
import pw.hamzantal.bloodmoon.BMData
import pw.hamzantal.bloodmoon.Config
import kotlin.random.Random

val surrounding = listOf(
    0.0 to -1.0,
    0.0 to 1.0,
    1.0 to -1.0,
    1.0 to 0.0,
    1.0 to 1.0,
    -1.0 to -1.0,
    -1.0 to -0.0,
    -1.0 to 1.0
)

fun onCreatureSpawn(e: CreatureSpawnEvent) {
    if (!BMData.running || e.spawnReason != CreatureSpawnEvent.SpawnReason.NATURAL) return

    val loc = e.entity.location
    if (loc.world != Config.world || Random.nextDouble() > 0.9) return

    when (val en = e.entity) {
        is Zombie -> if (Config.zombieSwarm) surrounding.forEach {
            val (x, z) = it
            Config.world.spawnEntity(loc.clone().add(x, 0.0, z), EntityType.ZOMBIE)
        }
        is Creeper -> if (Config.chargedCreepers) en.isPowered = true
        is Spider -> {
            if (!Config.strongSpiders) return
            en.addPotionEffect(PotionEffect(PotionEffectType.SPEED, BMData.ticksLeft(), 3, true, true))
            en.addPotionEffect(PotionEffect(PotionEffectType.INCREASE_DAMAGE, BMData.ticksLeft(), 1, true, true))
        }
    }
}