/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2025 SteamWar.de-Serverteam
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Affero General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *   GNU Affero General Public License for more details.
 *
 *   You should have received a copy of the GNU Affero General Public License
 *   along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package de.steamwar.misslewars.listener.special;

import de.steamwar.linkage.Linked;
import de.steamwar.misslewars.Config;
import de.steamwar.misslewars.FightState;
import de.steamwar.misslewars.MissileWars;
import de.steamwar.misslewars.listener.BasicListener;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.entity.TNTPrimed;
import org.bukkit.event.EventHandler;
import org.bukkit.event.entity.EntitySpawnEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.util.Vector;

import java.util.EnumSet;

@Linked
public class SpaceListener extends BasicListener {

    public SpaceListener() {
        super(EnumSet.allOf(FightState.class));
    }

    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent event) {
        if (!Config.Space) return;
        Bukkit.getScheduler().runTaskLater(MissileWars.getPlugin(), () -> {
            Player player = event.getPlayer();
            player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP_BOOST, Integer.MAX_VALUE, 3));
            player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW_FALLING, Integer.MAX_VALUE, 3));
            player.setHealth(0.5);
            player.setHealthScale(0.5);
        }, 5);
    }

    @EventHandler
    public void onPlayerRespawn(PlayerRespawnEvent event) {
        if (!Config.Space) return;
        Bukkit.getScheduler().runTaskLater(MissileWars.getPlugin(), () -> {
            Player player = event.getPlayer();
            player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP_BOOST, Integer.MAX_VALUE, 3));
            player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW_FALLING, Integer.MAX_VALUE, 3));
            player.setHealth(0.5);
            player.setHealthScale(0.5);
        }, 5);
    }

    @EventHandler
    public void onEntitySpawn(EntitySpawnEvent event) {
        if (!Config.Space) return;
        if (event.getEntity() instanceof Player) return;
        event.getEntity().setGravity(false);
        Vector vector = event.getEntity().getVelocity();
        if (event.getEntity() instanceof TNTPrimed) {
            vector.setY(0);
        }
        event.getEntity().setVelocity(vector);
    }
}
