package ca.recrown.islandsurvivalcraft.utilities.pathfinding;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Scanner;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;

import ca.recrown.islandsurvivalcraft.utilities.pathfinding.CoordinateTargetValidatable;
import ca.recrown.islandsurvivalcraft.utilities.pathfinding.CoordinateValidatable;

/**
 * Unit test for simple App.
 */
@TestInstance(Lifecycle.PER_CLASS)
public class DepthFirstSearchTest {

    private class Validator implements CoordinateValidatable, CoordinateTargetValidatable {
        private byte[][] map;

        public Validator(byte[][] map) {
            this.map = map;
        }

        @Override
        public boolean validate(int x, int y) {
            try {
                return map[y][x] >= 1;
            } catch (IndexOutOfBoundsException e) {
                return false;
            }
        }

        @Override
        public boolean isCoordinateTarget(int x, int y) {
            try {
                return map[y][x] == 2;
            } catch (IndexOutOfBoundsException e) {
                return false;
            }
        }
    }

    private DepthFirstSearch dfs;

    private byte[][] mapA;
    private byte[][] mapB;
    private byte[][] mapC;
    private byte[][] mapD;
    private byte[][] mapE;
    private byte[][] mapF;
    private byte[][] mapG;
    private byte[][] mapH;

    @BeforeAll
    public void setUp() throws IOException {
        dfs = new DepthFirstSearch();
        mapA = readMap("DFSTestMapSmallA.txt");
        mapB = readMap("DFSTestMapSmallB.txt");
        mapC = readMap("DFSTestMapSmallC.txt");
        mapD = readMap("DFSTestMapSmallD.txt");
        mapE = readMap("DFSTestMapSmallE.txt");

        mapF = readMap("DFSTestMapLargeA.txt");
        mapG = readMap("DFSTestMapLargeB.txt");
        mapH = readMap("DFSTestMapLargeC.txt");
    }

    private byte[][] readMap(String filename) throws IOException {
        byte[][] map = null;
        InputStream stream = getClass().getClassLoader().getResourceAsStream((filename));
        Scanner scanner = new Scanner(stream);
        String line = null;
        ArrayList<byte[]> rows = new ArrayList<>();
        while (scanner.hasNextLine()) {
            line = scanner.nextLine();
            char[] chars = line.toCharArray();
            byte[] rowBytes = new byte[chars.length];
            for (int i = 0; i < chars.length; i++) {
                rowBytes[i] = (byte) Character.getNumericValue(chars[i]);
            }
            rows.add(rowBytes);
        }
        scanner.close();
        map = new byte[rows.size()][rows.get(0).length];
        for (int row = 0; row < rows.size(); row++) {
            map[row] = rows.get(row);
        }
        return map;
    }

    @Test
    public void testDFSBuildPathToEndNodeMapAValid()
    {
        dfs.setValidatable(new Validator(mapA));
        dfs.setStartPosition(1, 2);
        dfs.setEndPosition(1, 0);
        assertTrue(dfs.buildPathToEndNode());
    }

    @Test
    public void testDFSBuildPathToEndNodeMapBValid()
    {
        dfs.setValidatable(new Validator(mapB));
        dfs.setStartPosition(3, 0);
        dfs.setEndPosition(3, 2);
        assertTrue(dfs.buildPathToEndNode());
    }

    @Test
    public void testDFSBuildPathToEndNodeMapDValid()
    {
        dfs.setValidatable(new Validator(mapD));
        dfs.setStartPosition(0, 0);
        dfs.setEndPosition(0, 3);
        assertTrue(dfs.buildPathToEndNode());
    }

    @Test
    public void testDFSBuildPathToEndNodeMapCInvalid()
    {
        dfs.setValidatable(new Validator(mapC));
        dfs.setStartPosition(3, 0);
        dfs.setEndPosition(3, 2);
        assertFalse(dfs.buildPathToEndNode());
    }

    @Test
    public void testDFSBuildPathToEndNodeMapEInvalid()
    {
        dfs.setValidatable(new Validator(mapE));
        dfs.setStartPosition(3, 0);
        dfs.setEndPosition(3, 2);
        assertFalse(dfs.buildPathToEndNode());
    }

    @Test
    public void testDFSFindEndNodeMapBValid() {
        Validator validator = new Validator(mapB);
        dfs.setValidatable(validator);
        dfs.setStartPosition(3, 0);
        
        assertTrue(dfs.findTarget(validator));
        assertEquals(1, dfs.getFoundNode().getX());
        assertEquals(2, dfs.getFoundNode().getY());
    }

    @Test
    public void testDFSFindEndNodeMapDValid() {
        Validator validator = new Validator(mapD);
        dfs = new DepthFirstSearch(validator);
        dfs.setValidatable(validator);
        dfs.setStartPosition(0, 0);

        assertTrue(dfs.findTarget(validator));
        assertEquals(0, dfs.getFoundNode().getX());
        assertEquals(3, dfs.getFoundNode().getY());
    }

    @Test
    public void testDFSFindEndNodeMapHValid() {
        Validator validator = new Validator(mapH);
        dfs.setValidatable(validator);
        dfs.setStartPosition(3, 0);

        assertTrue(dfs.findTarget(validator));
        assertEquals(95, dfs.getFoundNode().getX());
        assertEquals(49, dfs.getFoundNode().getY());
    }

    @Test
    public void testDFSFindEndNodeMapHInvalidLimited() {
        Validator validator = new Validator(mapH);
        DepthFirstSearch temporary = new DepthFirstSearch(128, validator);
        temporary.setStartPosition(3, 0);
        assertFalse(temporary.findTarget(validator));
    }


    @Test
    public void testDFSFindEndNodeMapEInvalid() {
        Validator validator = new Validator(mapE);
        dfs = new DepthFirstSearch();
        dfs.setValidatable(validator);
        dfs.setStartPosition(0, 0);
        
        assertFalse(dfs.findTarget(validator));
    }

    @Test
    public void testDFSFindEndNodeMapFValid() {
        Validator validator = new Validator(mapF);
        dfs.setValidatable(validator);
        dfs.setStartPosition(0, 0);

        assertTrue(dfs.findTarget(validator));
        assertEquals(26, dfs.getFoundNode().getX());
        assertEquals(32, dfs.getFoundNode().getY());
    }

    @Test
    public void testDFSFindEndNodeMapGInvalid() {
        Validator validator = new Validator(mapG);
        dfs.setValidatable(validator);
        dfs.setStartPosition(0, 0);

        assertFalse(dfs.findTarget(validator));
    }

    @Test
    public void testDFSFindEndNodeMapHWithAssistValid() {
        Validator validator = new Validator(mapH);
        dfs.setValidatable(validator);
        dfs.setStartPosition(3, 0);
        dfs.setEndPosition(42, 84);

        assertTrue(dfs.findTarget(validator));
        assertEquals(95, dfs.getFoundNode().getX());
        assertEquals(49, dfs.getFoundNode().getY());
    }
}
