package main;

import grid.GridGraph;
import grid.StartGoalPoints;

import java.awt.Color;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

import uiandio.GraphImporter;
import algorithms.datatypes.SnapshotItem;
import draw.DrawCanvas;
import draw.GridLineSet;
import draw.GridObjects;

public class TextOutputVisualisation {

    public static void run() {
        loadFromFile("currmap.txt");
    }

    private static void loadFromFile(String mazeFileName) {
        String textData = readStandardInput();

        GridGraph gridGraph = GraphImporter.importGraphFromFile(mazeFileName);
        StartGoalPoints p = new StartGoalPoints(0,0,0,0);
        displayTextVisualisation(gridGraph, p, textData);
    }
    
    private static String readStandardInput() {
        Scanner sc = new Scanner(System.in);
        StringBuilder sb = new StringBuilder();
        String line = sc.nextLine();
        while (!"#".equals(line)) {
            sb.append(line);
            sb.append("\n");
            line = sc.nextLine();
        }
        return sb.toString();
    }

    private static void displayTextVisualisation(GridGraph gridGraph, StartGoalPoints p, String textData) {
        GridLineSet gridLineSet = new GridLineSet();
        
        String[] args = textData.split("\n");
        Integer[][] paths = new Integer[args.length][];
        for (int i=0;i<args.length;++i) {
            String[] tokens = args[i].split(" ");
            Integer[] path = new Integer[tokens.length];
            for (int j=0;j<tokens.length;++j) {
                path[j] = Integer.parseInt(tokens[j]);
            }
            paths[i] = path;
        }

        ArrayList<GridObjects> lineSetList = new ArrayList<>();
        for (int i=1;i<args.length;++i) {
            List<SnapshotItem> snapshot = new ArrayList<>();
            for (int j=0;j<i;++j) {
                Color col = Color.red;
                if (j == i-1) col = Color.green;
                
                SnapshotItem e = SnapshotItem.generate(paths[j], col);
                snapshot.add(e);
            }
            
            lineSetList.add(GridObjects.create(snapshot));
        }
        
        lineSetList.add(new GridObjects(gridLineSet, null));
        DrawCanvas drawCanvas = new DrawCanvas(gridGraph, gridLineSet);
        drawCanvas.setStartAndEnd(p.sx, p.sy, p.ex, p.ey);
        
        Visualisation.setupMainFrame(drawCanvas, lineSetList);
    }
}

@@ -116,33 +116,8 @@
 st;%0A
-import java.util.Arrays;%0A
 impo
@@ -406,15 +406,18 @@
 le(%22
-currmap
+anyacont2b
 .txt
@@ -1755,17 +1755,17 @@
  (int i=
-1
+0
 ;i%3Cargs.
@@ -1866,16 +1866,17 @@
 t j=0;j%3C
+=
 i;++j) %7B
@@ -1945,10 +1945,8 @@
 == i
--1
 ) co

Fix small bug in textoutputvisualisation which prevents it from displaying the last state