package de.sopa.helper;

import org.junit.Before;
import org.junit.Test;

import de.sopa.model.game.Level;
import de.sopa.model.game.Tile;
import de.sopa.model.game.TileType;

import static de.sopa.model.game.TileType.FINISH;
import static de.sopa.model.game.TileType.PUZZLE;
import static de.sopa.model.game.TileType.START;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.*;

/**
 * @author David Schilling - davejs92@gmail.com
 */
public class LevelTranslatorTest {

    private LevelTranslator sut;
    private static final String[] levelAsString = {
        "5",
        "2",
        "6",
        "#",
        "nnnsnn",
        "ncooen",
        "noooin",
        "noooin",
        "ngooun",
        "nnnfnn"
    };
    private Level level;

    @Before
    public void setUp() {
        sut = new LevelTranslator();
        level = new Level();
        setupLevel();
    }

    @Test
    public void fromString() {
        Level resultingLevel = sut.fromString(levelAsString);
        assertThat(resultingLevel, is(level));
    }

    @Test
    public void fromGameField() {
        String[] fromGameField = sut.fromGameField(level);
        assertThat(fromGameField, is(levelAsString));
    }

    private void setupLevel() {
        level.setId(5);
        level.setMinimumMovesToSolve(2);
        level.setTilesCount(6);
        level.setStartX(3);
        level.setStartY(0);
        Tile[][] field = new Tile[][]{
                new Tile[]{new Tile(), new Tile(),                                                  new Tile(),             new Tile(false, true, false, false, START, 's'),    new Tile(),                                         new Tile()},
                new Tile[]{new Tile(), new Tile(false, true, true, false, TileType.PUZZLE, 'c'),    new Tile(PUZZLE, 'o'),  new Tile(PUZZLE, 'o'),                              new Tile(true, false, false, true, PUZZLE, 'e'),    new Tile()},
                new Tile[]{new Tile(), new Tile(TileType.PUZZLE, 'o'),                              new Tile(PUZZLE, 'o'),  new Tile(PUZZLE, 'o'),                              new Tile(true, true, false, false, PUZZLE, 'i'),    new Tile()},
                new Tile[]{new Tile(), new Tile(TileType.PUZZLE, 'o'),                              new Tile(PUZZLE, 'o'),  new Tile(PUZZLE, 'o'),                              new Tile(true, true, false, false, PUZZLE, 'i'),    new Tile()},
                new Tile[]{new Tile(), new Tile(true, false, true, false, TileType.PUZZLE, 'g'),    new Tile(PUZZLE, 'o'),  new Tile(PUZZLE, 'o'),                              new Tile(false, true, false, true, PUZZLE, 'u'),    new Tile()},
                new Tile[]{new Tile(), new Tile(),                                                  new Tile(),             new Tile(true, false, false, false, FINISH, 'f'),   new Tile(),                                         new Tile()},
        };
        level.setField(swapColumnAndRow(field));
    }

    private Tile[][] swapColumnAndRow(Tile[][] field) {
        Tile[][] fieldSwapped = new Tile[field[0].length][];
        for(int row = 0; row < field[0].length; row++) {
            fieldSwapped[row] = new Tile[field.length];
        }
        for (int row = 0; row < field.length; row++){
            for (int col = 0; col < field[row].length; col++){
                fieldSwapped[col][row] = field[row][col];
            }
        }
        return fieldSwapped;


    }
}
@@ -736,24 +736,249 @@
 fnn%22%0A    %7D;%0A
+    private static final String%5B%5D invalidLevelAsString = %7B%0A        %225%22,%0A        %222%22,%0A        %226%22,%0A        %22#%22,%0A        %22nonsnn%22,%0A        %22ncooen%22,%0A        %22noooin%22,%0A        %22noooin%22,%0A        %22ngooun%22,%0A        %22nnnfnn%22%0A    %7D;%0A
     private 
@@ -1273,32 +1273,176 @@
 level));%0A    %7D%0A%0A
+    @Test(expected = IllegalArgumentException.class)%0A    public void fromStringInvalid() %7B%0A        sut.fromString(invalidLevelAsString);%0A    %7D%0A%0A
     @Test%0A    pu

add missing test case for level translator