import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * Driver class for navigating a robot grid.
 *   @author Dave Reed
 *   @version 8/17/17
 */
public class RoboDriver {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the grid file name: ");
        String filename = input.next();

        try {
            Scanner infile = new Scanner(new File(filename));

            int numRows = infile.nextInt();
            int numCols = infile.nextInt();
            ArrayList<Obstacle> obstacles = new ArrayList<Obstacle>();
            
            while (infile.hasNextInt()) {
                Obstacle obs = new Obstacle(infile.nextInt(), infile.nextInt(),
                                            infile.nextInt(), infile.nextInt());
                obstacles.add(obs);
            }

            RoboDriver.showGrid(numRows, numCols, obstacles);

            System.out.print("Enter a row and column: ");
            int row = input.nextInt();
            int col = input.nextInt();
            while (row != 0 || col != 0) {
                if (row <= 0 || row > numRows || col <= 0 || col > numCols) {
                    System.out.println("    (" + row + "," + col +") IS OUTSIDE THE GRID.");
                }
                else if (RoboDriver.isInObstacle(row, col, obstacles)) {
                    System.out.println("    (" + row + "," + col +") IS WITHIN AN OBSTACLE.");
                }
                else {
                    System.out.println("    (" + row + "," + col +") IS VACANT IN THE GRID.");
                }
                
                System.out.print("Enter a row and col (zeros to end): ");
                row = input.nextInt();
                col = input.nextInt();
            }
            System.out.println("    DONE");
        } catch (java.io.FileNotFoundException e) {
            System.out.println("File not found.");
        }
    }
    
    /**
     * Displays the grid using '*' to denote obstacles.
     *   @param numRows number of rows in the grid
     *   @param numCols number of columns in the grid
     *   @param obstacles a list of all the obstacles in the grid
     */
    private static void showGrid(int numRows, int numCols, ArrayList<Obstacle> obstacles) {
        System.out.print("+");
        for (int c = 1; c <= numCols; c++) {
            System.out.print("-");
        }
        System.out.println("+");
        
        for (int r = 1; r <= numRows; r++) {
            System.out.print("|");
            for (int c = 1; c <= numCols; c++) {
                if (RoboDriver.isInObstacle(r, c, obstacles)) {
                    System.out.print("*");
                }
                else {
                    System.out.print(" ");
                }
            }
            System.out.println("|");
        }
        
        System.out.print("+");
        for (int c = 1; c <= numCols; c++) {
            System.out.print("-");
        }
        System.out.println("+");     
    }
    
    /**
     * Determines if a row/col is within an obstacle in the grid.
     *   @param r row number
     *   @param c column number
     *   @param obstacles list of obstacles in the grid
     *   @return true if (r,c) is in an obstacle; else, false
     */
    private static boolean isInObstacle(int r, int c, ArrayList<Obstacle> obstacles) {
        for (Obstacle ob : obstacles) {
            if (ob.contains(r, c)) {
                return true;
            }
        }
        return false;        
    }
}
