import java.awt.Color;
import java.awt.Graphics;
import java.io.File;
import java.util.Scanner;

/**
 * Incomplete class for displaying greedy paths in a topographic map.
 *   @author Dave Reed
 *   @version 3/10/16
 */
public class TopoMap {
  private int[][] grid;
  private int numRows;
  private int numCols;
  private Graphics g;

  /**
   * Constructs a topographic map with elevations read in from a file.
   *   @param filename the file containing the elevations
   */
  public TopoMap(String filename) {
      try {
          Scanner infile = new Scanner(new File(filename));  
          this.numCols = infile.nextInt();
          this.numRows = infile.nextInt();
          
          this.grid = new int[this.numRows][this.numCols];
          for(int row=0; row < this.numRows; row++){
              for(int col=0; col<this.numCols; col++){
                 this.grid[row][col] = infile.nextInt(); 
              }
          }
          
          DrawingPanel panel = new DrawingPanel(this.numCols, this.numRows);
          this.g = panel.getGraphics();
      }
      catch (java.io.FileNotFoundException e) {
          System.out.println("File not found: " + filename);
      }
  }
  
  /**
   * Accessor for the number of rows in the map.
   *   @return the number of rows (i.e., y-coordinate range)
   */
  public int getNumRows() {
      return this.numRows;
  }

  /**
   * Accessor for the number of columns in the map.
   *   @return the number of columns (i.e., x-coordinate range)
   */
  public int getNumCols() {
      return this.numCols;
  }
  
  /**
   * Draws the map with elevations scaled to 0..255 grayscale.
   */
  public void draw(){        
      for(int row=0; row < this.numRows; row++){
          for(int col=0; col < this.numCols; col++){
              int val = (int)(255*this.grid[row][col]/9000);
              this.g.setColor(new Color(val,val,val));
              this.g.fillRect(col,row,1,1);
          }
      }      
  }
  
  /**
   * Draws a greedy path starting at coordinate (0, row).
   *   @param row the row number (i.e., y-coordinate)
   *   @param pathColor the color for the path (e.g., Color.GREEN)
   *   @return the sum of elevation changes for all steps in the path
   */
  public int drawGreedyPath(int row, Color pathColor) {
      return -1;
  }
   
  ///////////////////////////////////////////////////////////////////////
  
  public static void main(String[] args) {
      System.out.print("Enter the map file name: ");
      Scanner input = new Scanner(System.in);
      String filename = input.next();
      
      TopoMap map = new TopoMap(filename);
      
      map.draw();
  }
}
