package com.github.apixandru.games.rummikub;

import com.github.apixandru.games.rummikub.model.Card;
import com.github.apixandru.games.rummikub.model2.ImplementationDetails;
import com.github.apixandru.games.rummikub.model2.Player;
import com.github.apixandru.games.rummikub.model2.Rummikub;
import com.github.apixandru.games.rummikub.model2.RummikubFactory;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

/**
 * @author Alexandru-Constantin Bledea
 * @since December 25, 2015
 */
public final class RummikubTest2 {

    @Test
    public void testGame() {
        final Rummikub game = RummikubFactory.newInstance();
        final Player first = game.addPlayer("John");
        final Player second = game.addPlayer("Johnny");

        assertSame(first, ImplementationDetails.currentPlayer(game));

        second.endTurn();
        assertSame(first, ImplementationDetails.currentPlayer(game));

        first.endTurn();
        assertSame(second, ImplementationDetails.currentPlayer(game));

        second.endTurn();
        assertSame(first, ImplementationDetails.currentPlayer(game));
    }

    @Test
    public void testPlaceCardOnBoard() {
        final Rummikub game = RummikubFactory.newInstance();
        final Player player = game.addPlayer("John");

        final int numberOfCardsBeforeEndTurn = ImplementationDetails.countCards(player);
        player.endTurn();
        assertEquals(numberOfCardsBeforeEndTurn + 1, ImplementationDetails.countCards(player));
    }

    @Test
    public void testCommitBoard() {
        final Rummikub rummikub = RummikubFactory.newInstance();
        final Player player = rummikub.addPlayer("Player");
        final List<Card> group = ImplementationDetails.endTurnUntilValidGroup(player);
        for (int i = 0; i < group.size(); i++) {
            player.placeCardOnBoard(group.get(i), i, 0);
        }
        final List<Card> cardsBeforeEndTurn = ImplementationDetails.cloneCards(player);
        player.endTurn();
        assertEquals(cardsBeforeEndTurn, ImplementationDetails.getCards(player));
    }


    @Test
    public void testPutCardOnBoard() {
        final Rummikub rummikub = RummikubFactory.newInstance();
        final Player player = rummikub.addPlayer("Player");
        final Card card = ImplementationDetails.getFirstCard(player);
        final Card[][] cards = ImplementationDetails.cloneBoard(rummikub);
        player.placeCardOnBoard(card, 0, 0);
        assertFalse(Arrays.deepEquals(ImplementationDetails.cloneBoard(rummikub), cards));
    }

    @Test
    public void testUndo() {
        final Rummikub rummikub = RummikubFactory.newInstance();
        final Player player = rummikub.addPlayer("Player");
        final Card card = ImplementationDetails.getFirstCard(player);
        final Card[][] cards = ImplementationDetails.cloneBoard(rummikub);
        player.placeCardOnBoard(card, 0, 0);
        player.endTurn();
        assertTrue(Arrays.deepEquals(ImplementationDetails.cloneBoard(rummikub), cards));
    }

}

@@ -2252,17 +2252,16 @@
 %0A    %7D%0A%0A
-%0A
     @Tes
@@ -3191,10 +3191,520 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void testTakeCardFromBoard() %7B%0A        final Rummikub rummikub = RummikubFactory.newInstance();%0A        final Player player = rummikub.addPlayer(%22Player%22);%0A        final Card card = ImplementationDetails.getFirstCard(player);%0A        final Card%5B%5D%5B%5D cards = ImplementationDetails.cloneBoard(rummikub);%0A        player.placeCardOnBoard(card, 0, 0);%0A        player.takeCardFromBoard(card, 0, 0);%0A        assertTrue(Arrays.deepEquals(ImplementationDetails.cloneBoard(rummikub), cards));%0A    %7D%0A%0A
 %7D%0A

test take card after placing it