package server.utils;

import server.GameBoard;
import server.OfferingTile;
import server.Player;
import server.TurnTile;
import server.cards.BuildingCard;
import server.cards.Card;
import server.cards.CharacterCard;
import server.cards.EventCard;

import java.util.Arrays;

public class GameUtils {

    public static String formatCard(Card c) {
        if (c instanceof EventCard e) {
            return "(E "+e.getCardId() + "  " + e.getEvent()+ " - " +e.getEra()  +")";
        }
        if (c instanceof CharacterCard cc) {
            return "(C "+cc.getCardId() + " " +cc.getCharacterType() + " - " +cc.getEra()  + ")";
        }
        if (c instanceof BuildingCard b) {
            return "(B "+b.getCardId() + " - " +b.getEra() + ")";
        }
        return "(#" + c.getCardId() + " UNKNOWN)";
    }

    public static String formatTurnTile(TurnTile t) {
        //for (Player p : t.getPositions()){" : "occupied");
        StringBuilder sb = new StringBuilder();
        sb.append("Turn Tile\n");
        for (int i = 0; i < t.getPositions().length; i++) {
            Player p = t.getPositions()[i];

            sb.append(" ").append(i + 1).append(". ")
                    .append((p!=null? p.toString():"")+ "\n");
        }
        return sb.toString();
    }

    private void pickBottomCardAction(Player player, GameBoard board, int cardid){
        Card card = board.getBottomRow().stream()
                .filter(CharacterCard.class::isInstance)
                .map(CharacterCard.class::cast)
                .filter(c -> c.getCardId() == cardid)
                .findFirst()
                .orElse(null);
        if (card != null) {
            board.getBottomRow().remove(card);
            player.addCharacterToTribe((CharacterCard) card);
        }
    }

    private void pickTopCardAction(Player player, GameBoard board, int cardid){
        Card card = board.getTopRow().stream()
                .filter(CharacterCard.class::isInstance)
                .map(CharacterCard.class::cast)
                .filter(c -> c.getCardId() == cardid)
                .findFirst()
                .orElse(null);

        if (card != null) {
            board.getTopRow().remove(card);
            CharacterCard charCard = (CharacterCard)card;
            player.addCharacterToTribe((CharacterCard) card);
        }
    }

}
