package server.automaton;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import server.*;
import server.cards.BuildingCard;
import server.cards.Card;
import server.cards.CharacterCard;

import java.util.ArrayList;
import java.util.List;

import static server.utils.GameUtils.formatTurnTile;

class GameTest {

    private Game game;

    @BeforeEach
    public void setUp(){
        List<Player> players = new ArrayList<>();
        players.add(new Player("Yellow", TotemColor.YELLOW));
        players.add(new Player("Blue", TotemColor.BLUE));
        players.add(new Player("Red", TotemColor.RED));
        //players.add(new Player("Green", TotemColor.GREEN));
        //players.add(new Player("Purple", TotemColor.PURPLE));


        game = new Game(players);
        game.newGame(getClass().getResourceAsStream("/files/cards.csv"));
        System.out.println("Current Player " +game.getCurrentPlayer());
    }

    @Test
    void newGame() {
        System.out.println("Round " + game.getRound());
        System.out.println("Game State " + game.getState());

        System.out.println("-- Turn Tile --\n" +formatTurnTile( game.getGameBoard().getTurnTile()));
        System.out.println("---- Offering Tiles ----\n" + game.getGameBoard().getOfferingTiles());

        System.out.println("______ Top _________    \n" + game.getGameBoard().getTopRow());
        System.out.println("______ Bottom _________ \n" + game.getGameBoard().getBottomRow());


    }

    @Test
    void placeTotem() {
        int i=0;System.out.println(game.getGameBoard().getTurnTile().getReturnedCount());
        System.out.println(game.getGameBoard().getTurnTile().getPositions().length);
        for(Player p : game.getGameBoard().getTurnTile().getPositions()){
            Player  current = game.getCurrentPlayer();
            System.out.println((i++)+" " +current);

            OfferingTile ot =game.getGameBoard().getOfferingTiles().stream().filter(o->o.isEmpty()).findFirst().orElse(null);
            int idx =game.getGameBoard().getOfferingIdxFromLetter(ot.getLetter());
            boolean ok =game.placeTotem(current, idx);
            System.out.println("-- Turn Tile --\n" +formatTurnTile( game.getGameBoard().getTurnTile()));
            System.out.println("---- Offering Tiles ----\n" + game.getGameBoard().getOfferingTiles());
        }

    }

    @Test
    void resolveCardAction() {
        placeTotem();

        System.out.println(game.getState());
        game.getGameBoard()
                .getOfferingTiles()
                .stream()
                .filter(o -> !o.isEmpty())
                .forEach(
                        o -> {
                            Player p = o.getOccupant();
                            if (o.getActions().size()==3){
                                System.out.println("AZIONE  FOOD ");

                            } else {
                                for (Symbol act : o.getActions()){
                                    if (Symbol.UP.equals(act)){
                                        Card ctop = game.getGameBoard().getTopRow().stream().filter(s -> s instanceof CharacterCard || s instanceof BuildingCard).findFirst().orElse(null);
                                        ActionResult result = game.resolveCardAction(p, true, ctop.getCardId());
                                        System.out.println(result);
                                    }
                                    if (Symbol.DOWN.equals(act)){
                                        Card cdown = game.getGameBoard().getBottomRow().stream().filter(s -> s instanceof CharacterCard  || s instanceof  BuildingCard).findFirst().orElse(null);
                                        ActionResult result = game.resolveCardAction(p, false, cdown.getCardId());
                                        System.out.println(result);
                                    }
                                }
                            }
                        }
               );
    }

    @Test
    void extraDrawAction() {
    }

    @Test
    void skipExtraDraw() {
    }

    @Test
    void endGame() {
    }

    @Test
    void getPendingActions() {
    }
}