package eu.mhsl.craftattack.teamLobby;

import eu.mhsl.craftattack.teamLobby.data.Team;
import net.minestom.server.entity.Player;
import org.jetbrains.annotations.NotNull;

import java.util.*;

public class LobbyManager {
    private final Set<Team> teams = new HashSet<>() {
        {
            this.add(new Team(UUID.randomUUID(), "Testerr", "#123123", List.of(
                UUID.fromString("c291290d-cffc-4649-aeec-d6f4417896ea"),
                UUID.fromString("959ed433-14ea-38fe-918b-75b7d09466af")
            )));
        }
    };
    private final Map<Team, Lobby> instances = new HashMap<>();

    public synchronized @NotNull Lobby getPlayerInstance(Player player) {
        UUID playerId = player.getUuid();
        Team targetTeam = this.teams.stream()
            .filter(team -> team.players().contains(playerId))
            .findAny()
            .orElseThrow(() -> new NoSuchElementException("Player is not in any Team!"));

        if(!this.instances.containsKey(targetTeam)) {
            Lobby instance = new Lobby(targetTeam);
            this.instances.put(targetTeam, instance);
        }

        return this.instances.get(targetTeam);
    }
}
