/**
 * Oct 31, 2007
 * edu.drexel.cs485.env:Environment.java
 * --------
 */
package edu.drexel.cs485.env;

import java.awt.Point;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Date;

/**
 * Environment
 * @author Duc Nguyen, Peter Thai
 * 
 * Description: 
 * 
 */
public class Environment {
	public static final long WALL = -99;
	public static final long FOOD = -999;
	public static final long COLONY = -9;
	
	public static final char WHITE = ' ';
	public static final char BLACK = '@';
	
	
	//private Cell[][] environment;
	private long[][] environment;
	private static final String environmentFileName = "environment.txt";
	
	/**
	 * constructs our environment with no pheromones set
	 * @throws IOException
	 */
	public Environment() throws IOException{
		environment = readFile();
	}
	
	/**
	 * Returns a 2D array of characters in text file (environmentFileName)
	 * @return
	 */
	private long[][] readFile() throws IOException{
		long[][] retVal = null;
		BufferedReader in = new BufferedReader(new FileReader(environmentFileName));
		
		String line = in.readLine();
		int cols = Integer.parseInt(line);
		line = in.readLine();
		int rows = Integer.parseInt(line);
		
		retVal = new long[rows][cols];
		
		line = in.readLine();
		int y = 0;
		while( line != null ){
			
			String[] chr = line.split(",");
			for( int x=0; x < chr.length; x++){
				if( "F".equals(chr)){
					Point cellLocation = new Point(x, y);
					retVal[y][x] = FOOD;
				} else {
					int value = Integer.parseInt(chr[x]);
					if( value == 1 ){//wall
						retVal[y][x] = WALL;
					} else {
						retVal[y][x] = 0;
					}
				}
			}
			y++;
			line = in.readLine();
		}
		
		in.close();
		return retVal;
	}

	/**
	 * leaves a virtual pheromone at the given location
	 * @param location
	 * @param robotName
	 */
	public void leavePheromone(Point location, String robotName){
		environment[location.x][location.y] = this.getCurrentTime();
	}	
	public void leavePheromone(int x_, int y_){
		environment[x_][y_] = this.getCurrentTime();
	}
	
	/**
	 * 
	 * @param location
	 * @return TRUE if there is a obstacle(ie a wall) at this location, false otherwise
	 */
	public boolean isObstacle(Point location){
		return (environment[location.x][location.y] == WALL);
	}
	
	public boolean isObstacle(int x_, int y_){
		return (environment[x_][y_] == WALL);
	}
	/**
	 * 
	 * @param location
	 * @return TRUE if there is food at the given location, false otherwise
	 * TODO Remove this method
	 */
	public boolean isFood(Point location){
		return (environment[location.x][location.y] == FOOD);
	}
	public boolean isFood(int x_, int y_){
		return (environment[x_][y_] == FOOD);
	}
	
	/**
	 * 
	 */

	public boolean isColony(int x_, int y_){
		return (environment[x_][y_] == COLONY);
	}	
		

	/**
	 * sets an obstacle
	 * @param location
	 * @param robotName
	 */
	public void foundObstacle(Point location, String robotName){
		environment[location.x][location.y] = WALL;
	}

	

	/**
	 * @param location
	 * @return the pheromone value at this cell location, null if there is none.
	 */

	public long getCellData(Point location){
		return environment[location.x][location.y];
	}

	public long getCellData(int _x, int _y){
		return environment[_x][-y];
	}

	/**
	 * Gets the difference in timestamp
	 */
	
	public long getPheromone(int x_, int y_){
		long pherVal = (this.getCurrentTime()) - environment[x_][y_];
		return pherVal;
	}

	/**
	 * Get current time
	 */

	public long getCurrentTime(){
		return ((new Date()).getTime());
	}		

	/**
	 * draws the environment (in XPM format) to std out
	 */
	public void drawEnvironment(String outputFileName){
		
		System.err.println("canvas size: X"+environment[0].length+" Y"+environment.length);
		
		printHeader(environment[0].length, environment.length);
		int rows = 0, cols = 0;
		for( int i = environment.length-1; i >= 0; i--){
		//for( int i = lengthY-1; i>=0; i--){
			System.out.print("\"");
			for( int j = 0; j < environment[i].length; j++){
				System.out.print(environment[i][j]);
				if( i == 0 ) cols++;
			}
			if( i > 0){
				System.out.println("\",");
			} 
			rows++;
		}
		System.err.println("rows: "+rows+" cols: "+cols);
		printFooter();
	}
	
	/**
	 * prints a last " and };
	 */
	private void printFooter() {
		System.out.println("\"\n};");
		
	}
	/**
	 * prints the header
	 */
	private void printHeader(int lengthX, int lengthY) {
		System.out.println("/* XPM */\nstatic char *quad_bw[] = {\n/* columns rows colors chars-per-pixel */");
		System.out.println("\""+lengthX+" "+lengthY+" "+3+" "+1+"\","); // width, height, # of colors, chars per pixel
		System.out.println("/* pixels */\n" +
				"\""+BLACK+" c #000000\",\n" +
				"\""+WHITE+" c #FFFFFF\"," + 
			    "\""+RED+" c #FF0000\","
						   );
		
	}
}
