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

/**
 * 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<Integer> coords = new ArrayList<Integer>();
            
            while (infile.hasNextInt()) {
                for (int i = 0; i < 4; i++) {
                    coords.add(infile.nextInt());
                }
            }

            RoboDriver.showGrid(numRows, numCols, coords);

            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, coords)) {
                    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 coords list of all the obstacle coordinates in the grid
     */
    private static void showGrid(int numRows, int numCols, ArrayList<Integer> coords) {
        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, coords)) {
                    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 coords list of obstacle coordinates in the grid
     *   @return true if (r,c) is in an obstacle; else, false
     */
    private static boolean isInObstacle(int r, int c, ArrayList<Integer> coords) {
        for (int i = 0; i < coords.size(); i += 4) {
            int topRow = coords.get(i);
            int leftCol = coords.get(i+1);
            int bottomRow = coords.get(i+2);
            int rightCol = coords.get(i+3);
            if (r >= topRow && r <= bottomRow && c >= leftCol && c <= rightCol) {
                return true;
            }
        }
        return false;        
    }
}
