package rip.tilly.bedwars.managers;

import lombok.Getter;
import org.bukkit.entity.Player;
import rip.tilly.bedwars.BedWars;
import rip.tilly.bedwars.game.Game;
import rip.tilly.bedwars.game.GameRequest;
import rip.tilly.bedwars.game.GameTeam;
import rip.tilly.bedwars.game.arena.Arena;
import rip.tilly.bedwars.game.events.GameEndEvent;
import rip.tilly.bedwars.game.events.GameStartEvent;
import rip.tilly.bedwars.player.PlayerData;
import rip.tilly.bedwars.utils.TtlHashMap;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

/**
 * Created by Lucanius
 * Project: BedWars
 */
public class GameManager {

    private final BedWars plugin = BedWars.getInstance();

    private final Map<UUID, Set<GameRequest>> gameRequests = new TtlHashMap<>(TimeUnit.SECONDS, 30);
    @Getter private final Map<UUID, Game> games = new ConcurrentHashMap<>();

    public int getFighters() {
        int i = 0;
        for (Game game : this.games.values()) {
            for (GameTeam team : game.getTeams()) {
                i += team.getPlayingPlayers().size();
            }
        }

        return i;
    }

    public Game getGame(PlayerData playerData) {
        return this.games.get(playerData.getCurrentGameId());
    }

    public Game getGame(UUID uuid) {
        return this.getGame(this.plugin.getPlayerDataManager().getPlayerData(uuid));
    }

    public Game getGameFromUUID(UUID uuid) {
        return this.games.get(uuid);
    }

    public void createGameRequest(Player requester, Player requested, Arena arena, boolean party) {
        GameRequest request = new GameRequest(requester.getUniqueId(), requested.getUniqueId(), arena, party);
        this.gameRequests.computeIfAbsent(requested.getUniqueId(), k -> new HashSet<>()).add(request);
    }

    public GameRequest getGameRequest(UUID requester, UUID requested) {
        Set<GameRequest> requests = this.gameRequests.get(requested);
        if (requests == null) {
            return null;
        }

        return requests.stream().filter(req -> req.getRequester().equals(requester)).findAny().orElse(null);
    }

    public void removeGameRequests(UUID uuid) {
        this.gameRequests.remove(uuid);
    }

    public void createGame(Game game) {
        this.games.put(game.getGameId(), game);
        this.plugin.getServer().getPluginManager().callEvent(new GameStartEvent(game));
    }

    public void removeGame(Game game) {
        this.games.remove(game.getGameId());
    }

    public void removePlayerFromGame(Player player, PlayerData playerData) {
        Game game = this.games.get(playerData.getCurrentGameId());
        Player killer = player.getKiller();

        if (player.isOnline() && killer != null) {
            killer.hidePlayer(player);
        }

        GameTeam losingTeam = game.getTeams().get(playerData.getTeamId());
        GameTeam winningTeam = game.getTeams().get(losingTeam.getId() == 0 ? 1 : 0);

        if (killer != null) {
            game.broadcast(losingTeam.getPlayerTeam().getChatColor() + player.getName() + " &ehas been killed by " + winningTeam.getPlayerTeam().getChatColor() + killer.getName() + "&e! &b&lFINAL KILL!");
        } else {
            game.broadcast(losingTeam.getPlayerTeam().getChatColor() + player.getName() + " &ehas died! &b&lFINAL KILL!");
        }

        losingTeam.killPlayer(player.getUniqueId());
        int remaining = losingTeam.getPlayingPlayers().size();

        if (remaining == 0) {
            this.plugin.getServer().getPluginManager().callEvent(new GameEndEvent(game, winningTeam, losingTeam));
        }
    }
}
