/*
 *   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.lobby.listener;

import de.steamwar.linkage.Linked;
import de.steamwar.lobby.jumpandrun.JumpAndRun;
import de.steamwar.lobby.util.LobbyPlayer;
import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerToggleFlightEvent;
import org.bukkit.util.Vector;

@Linked
public class DoubleJumpListener implements Listener {

    double multiplyer = 1.4;

    @EventHandler
    public void handlePlayerJoin(PlayerJoinEvent event) {
        Player player = event.getPlayer();
        player.setAllowFlight(true);
        player.setFlying(false);
    }

    @EventHandler
    public void handlePlayerToggleFlight(PlayerToggleFlightEvent event) {
        Player player = event.getPlayer();
        if (JumpAndRun.isPlayerInJumpAndRun(player)) return;
        if (player.getGameMode() != GameMode.ADVENTURE && player.getGameMode() != GameMode.SURVIVAL) {
            return;
        }
        if (LobbyPlayer.getLobbyPlayer(player).isFlying()) {
            return;
        }

        event.setCancelled(true);
        player.setAllowFlight(false);
        player.setFlying(false);

        Vector direction = player.getLocation().getDirection();
        direction.setX(direction.getX() * multiplyer);
        direction.setY(direction.getY() * (multiplyer / 2));
        direction.setZ(direction.getZ() * multiplyer);

        player.setVelocity(direction.add(new Vector(0, 1.2, 0)));
        player.playSound(player.getLocation(), Sound.ENTITY_FIREWORK_ROCKET_LAUNCH, 1.0F, 1.0F);
    }

    @EventHandler
    public void handlePlayerMove(PlayerMoveEvent event) {
        Player player = event.getPlayer();
        if (JumpAndRun.isPlayerInJumpAndRun(player)) return;

        if (player.getLocation().add(0, -1, 0).getBlock().getType() == Material.AIR) return;
        if (LobbyPlayer.getLobbyPlayer(player).isFlying()) return;

        player.setAllowFlight(true);
        if (player.getGameMode() == GameMode.ADVENTURE || player.getGameMode() == GameMode.SURVIVAL) {
            player.setFlying(false);
        }
    }

}
