package eu.jonahbauer.wizard.client.cli.commands;

import eu.jonahbauer.wizard.client.cli.Client;
import eu.jonahbauer.wizard.client.cli.state.Game;
import eu.jonahbauer.wizard.client.cli.util.Pair;
import eu.jonahbauer.wizard.common.model.Card;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

import static picocli.CommandLine.Command;

@Command(name = "show")
public class ShowCommand {
    private final Client client;
    private final Game game;

    public ShowCommand(Client client, Game game) {
        this.client = client;
        this.game = game;
    }

    @Command(name = "players")
    public void players() {
        StringBuilder builder = new StringBuilder();
        for (Map.Entry<UUID, String> player : game.getPlayers().entrySet()) {
            builder.append(player.getValue()).append('\t').append(player.getKey()).append('\n');
        }
        client.println(builder.toString());
    }

    @Command(name = "stack")
    public void stack() {
        var stack = game.getStack();
        String[] col0 = new String[stack.size()];
        String[] col1 = new String[stack.size()];
        int i = 0;
        for (Pair<UUID, Card> entry : game.getStack()) {
            col0[i] = Objects.toString(entry.getValue());
            col1[i] = Objects.toString(game.nameOf(entry.getKey()));
            i++;
        }

        client.print(col0, col1);
    }

    @Command(name = "hand")
    public void hand() {
        client.println(game.getHands().get(game.getSelf()));
    }

    @Command(name = "predictions", aliases = "tricks")
    public void predictions() {
        var players = game.getPlayers().keySet();
        String[] col0 = new String[players.size() + 1];
        String[] col1 = new String[players.size() + 1];
        String[] col2 = new String[players.size() + 1];
        col0[0] = "";
        col1[0] = "Prediction";
        col2[0] = "Tricks";
        int i = 1;
        for (UUID player : players) {
            col0[i] = game.nameOf(player);
            col1[i] = Objects.toString(game.getPredictions().get(player));
            col2[i] = Objects.toString(game.getTricks().getOrDefault(player, List.of()).stream().filter(l -> !l.contains(Card.BOMB)).count());
            i++;
        }

        client.print(col0, col1, col2);
    }

    @Command(name = "trump")
    public void trump() {
        client.printfln("%s (%s)", game.getTrumpSuit(), game.getTrumpCard());
    }
}
