package rip.tilly.bedwars.listeners.game;

import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
import rip.tilly.bedwars.BedWars;
import rip.tilly.bedwars.events.GameStartEvent;
import rip.tilly.bedwars.game.Game;
import rip.tilly.bedwars.game.GameType;
import rip.tilly.bedwars.playerdata.PlayerData;
import rip.tilly.bedwars.playerdata.PlayerState;
import rip.tilly.bedwars.runnables.GameRunnable;
import rip.tilly.bedwars.utils.CC;
import rip.tilly.bedwars.utils.CustomLocation;
import rip.tilly.bedwars.utils.PlayerUtil;

import java.util.HashSet;
import java.util.Set;

public class GameStartListener implements Listener {

    private BedWars plugin = BedWars.getInstance();

    @EventHandler
    public void onGameStart(GameStartEvent event) {
        Game game = event.getGame();
        GameType gameType = game.getGameType();

        if (game.getArena().getAvailableArenas().size() > 0) {
            game.setCopiedArena(game.getArena().getAvailableArena());
            this.plugin.getArenaManager().setArenaGameUUIDs(game.getCopiedArena(), game.getGameId());
        } else {
            game.broadcast(CC.translate("&cThere are no available arenas at this moment!"));
            this.plugin.getGameManager().removeGame(game);
            return;
        }

        Set<Player> gamePlayers = new HashSet<>();
        game.getTeams().forEach(team -> team.playingPlayers().forEach(player -> {
            gamePlayers.add(player);
            this.plugin.getGameManager().removeGameRequests(player.getUniqueId());

            PlayerUtil.clearPlayer(player);

            PlayerData playerData = this.plugin.getPlayerDataManager().getPlayerData(player.getUniqueId());
            playerData.setCurrentGameId(game.getGameId());
            playerData.setTeamId(team.getId());
            playerData.setPlayerTeam(team.getPlayerTeam());

            playerData.setPlayerState(PlayerState.PLAYING);

            CustomLocation locationA = game.getCopiedArena().getA();
            CustomLocation locationB = game.getCopiedArena().getB();

            player.teleport(team.getId() == 1 ? locationA.toBukkitLocation() : locationB.toBukkitLocation());

            player.getInventory().setArmorContents(this.plugin.getGameManager().getGameArmor(playerData));
            for (ItemStack stack : this.plugin.getGameManager().getGameItems()) {
                player.getInventory().addItem(stack);
            }
        }));

        for (Player player : gamePlayers) {
            for (Player online : this.plugin.getServer().getOnlinePlayers()) {
                online.hidePlayer(player);
                player.hidePlayer(online);
            }
        }

        for (Player player : gamePlayers) {
            for (Player other : gamePlayers) {
                player.showPlayer(other);
            }
        }

        new GameRunnable(game).runTaskTimer(this.plugin, 20, 20);
    }
}
