package eu.jonahbauer.wizard.common.messages.server;

import eu.jonahbauer.wizard.common.messages.data.PlayerData;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;
import org.jetbrains.annotations.Unmodifiable;

import java.util.List;
import java.util.UUID;

@Getter
@EqualsAndHashCode(callSuper = true)
public final class SessionJoinedMessage extends ServerMessage implements Response {
    /**
     * UUID of joined session
     */
    private final @NonNull UUID session;
    /**
     * UUID assigned to the player
     */
    private final @NonNull UUID player;
    /**
     * List of players already in the session
     */
    private final @Unmodifiable @NonNull List<@NonNull PlayerData> players;
    /**
     * Secret notified to player in order to ensure that if necessary the correct player rejoins
     */
    private final @NonNull String secret;

    public SessionJoinedMessage(@NonNull UUID session, @NonNull UUID player, @NonNull List<@NonNull PlayerData> players, @NonNull String secret) {
        this.session = session;
        this.player = player;
        this.players = List.copyOf(players);
        this.secret = secret;

        boolean found = false;
        for (var playerData : players) {
            if (playerData.getUuid().equals(player)) {
                found = true;
                break;
            }
        }
        if (!found) {
            throw new IllegalArgumentException("Player not contained in players.");
        }
    }
}
