package rip.tilly.bedwars.listeners.game;

import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.github.paperspigot.Title;
import rip.tilly.bedwars.BedWars;
import rip.tilly.bedwars.events.GameEndEvent;
import rip.tilly.bedwars.game.Game;
import rip.tilly.bedwars.game.GameState;
import rip.tilly.bedwars.game.GameTeam;
import rip.tilly.bedwars.playerdata.PlayerData;
import rip.tilly.bedwars.utils.CC;
import rip.tilly.bedwars.utils.PlayerUtil;

public class GameEndListener implements Listener {

    private final BedWars plugin = BedWars.getInstance();

    @EventHandler
    public void onGameEnd(GameEndEvent event) {
        Game game = event.getGame();
        game.setGameState(GameState.ENDING);
        game.setCountdown(3);

        GameTeam winningTeam = event.getWinningTeam();
        GameTeam losingTeam = event.getLosingTeam();

        game.setWinningTeamId(winningTeam.getId());

        game.getTeams().forEach(team -> team.allPlayers().forEach(player -> {
            PlayerUtil.clearPlayer(player);

            PlayerData playerData = this.plugin.getPlayerDataManager().getPlayerData(player.getUniqueId());
            playerData.setLastDamager(null);
            playerData.setGameKills(0);

            String winnerTitle = CC.translate("&aVICTORY!");
            String losingTitle = CC.translate("&cDEFEAT!");
            String subTitle = CC.translate(winningTeam.getPlayerTeam().getChatColor() + winningTeam.getPlayerTeam().getName() + " &fhas won the game!");

            if (winningTeam.getAllPlayers().contains(player.getUniqueId())) {
                player.sendTitle(new Title(winnerTitle, subTitle, 5, 20, 5));
                playerData.setWins(playerData.getWins() + 1);
                playerData.addRandomXp(player);
            } else if (losingTeam.getAllPlayers().contains(player.getUniqueId())) {
                player.sendTitle(new Title(losingTitle, subTitle, 5, 20, 5));
                playerData.setLosses(playerData.getLosses() + 1);
            }

            playerData.setGamesPlayed(playerData.getGamesPlayed() + 1);
        }));
    }
}
