/**
 * Nov 7, 2007
 * edu.drexel.cs485.assign4:FoodFinderTest.java
 * --------
 */
package edu.drexel.cs485.assign4;

import java.awt.Point;
import java.io.IOException;

import edu.drexel.cs485.env.Environment;

/**
 * FoodFinderTest
 * @author duc, Peter Thai
 * 
 * Description: 
 * 
 */
public class FoodFinderTest {
	private Environment env;
	private int currentX, currentY; // keeps track of current location
	
	public FoodFinderTest() throws IOException{
		env = new Environment();
		
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		FoodFinderTest test;

		/*
		try {
			test = new FoodFinderTest();
			test.findFood();
			test.printMap();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		*/

		while (test.findFood()){// search for food until food has been found
			System.out.println("We've found food!");
			
			printMap();
		}
		while (test.returnHome()){
			
		}
		while (test.followVirPPath()){
			
		}
	}

	/**
	 * 
	 */

	public void printMap() {
		env.drawEnvironment();
	}

	/**
	 * Algorithm repeated until food is found
	 */

	public boolean findFood() {
		//Point currentLocation = new Point(11,0);
		//int currX = 11;
		//int currY = 0;
		/*
		 * virp[] = {up, right, down, left}
		 */
		//long up = env.getCellData(currentLocation);
		
		long lowestPherVal; // the newest laid virp
		long pherVal;
		long[] possible movements;
		String directionMove;

		for (int i = 0; i < 4; i++){
			lowestPherVal = getPheromoneDataNorth();
			directionMove = "north";
					
			if (lowestpherval > getPheromoneDataSouth()){
				lowestPherVal = getPheromoneDataSouth();
				directionMove = "south";
			}
			else if (lowestpherval > getPheromoneDataEast()){
				lowestPherVal = getPheromoneDataEast();
				directionMove = "east";
			}
			else if (lowestpherval > getPheromoneDataWest()){
				directionMove = "west";
				lowestPherVal = getPheromoneDataWest();
			}
			environment.leaveVirP(currentX, currentY);
			move(directionMove);
			if (environment.isFood(currentX, currentY))
				return true;
			else return false;
		}
	}   

	/**
	 * Algorithm used to navigate robot back to its initial position
	 */

	public booloan returnHome(){
		long highestPherVal; // older virps
        long pherVal;
        long[] possible movements;
        String directionMove;

        for (int i = 0; i < 4; i++){
            highestPherVal = getPheromoneDataNorth();
            directionMove = "north";

            if (highestpherval > getPheromoneDataSouth()){
                highestPherVal = getPheromoneDataSouth();
                directionMove = "south";
            }
            else if (highestpherval > getPheromoneDataEast()){
                highestPherVal = getPheromoneDataEast();
                directionMove = "east";
            }
            else if (highestpherval > getPheromoneDataWest()){
                directionMove = "west";
                highestPherVal = getPheromoneDataWest();
            }
            environment.leaveVirP(currentX, currentY);
            move(directionMove);
            if (environment.isColony(currentX, currentY))
                return true;
            else return false;
        }
	}
	public boolean followVirPPath(){
		long lowestPherVal; // the newest laid virp
        long pherVal;
        long[] possible movements;
        String directionMove;

        for (int i = 0; i < 4; i++){
            lowestPherVal = getPheromoneDataNorth();
            directionMove = "north";

            if (lowestpherval > getPheromoneDataSouth()){
                lowestPherVal = getPheromoneDataSouth();
                directionMove = "south";
            }
            else if (lowestpherval > getPheromoneDataEast()){
                lowestPherVal = getPheromoneDataEast();
                directionMove = "east";
            }
            else if (lowestpherval > getPheromoneDataWest()){
                directionMove = "west";
                lowestPherVal = getPheromoneDataWest();
            }
            environment.leaveVirP(currentX, currentY);
            move(directionMove);
            if (environment.isFood(currentX, currentY))
                return true;
            else return false;
        }

	}

	/**
	 * Get the pheromone data from specific cells
	 */

	public long getPheromoneData(int x_, int y_){
		//long[] cellData = [4];
		return environment.getPheromone[x_][y_];
	}
	public long getPheromoneDataNorth(){
		return getPheromoneData(currentX, currentY + 1);
	}
	public long getPheromoneDataEast(){
		return getPheromoneData(currentX + 1, currentY);
	}
	public long getPheromoneDataWest(){
		return getPheromoneData(currentX-1, currentY);
	}
	public long getPheromoneDataSouth(){
		return getPheromoneData(currentX, currentY - 1);
	}

	/**
	 * Move in the direction specified
	 */

	public void move(String direction){
		switch (direction){
			case north:
				moveNorth();
				break;
			case south:
				moveSouth();
				break;
			case west:
				moveWest();
				break;
			case east:
				moveEast();
				break;
			default:
				break;
		}
	}	

	public void moveNorth(){
		currentY++;
		leavePheromoneCurrentPosition();
	}	
	public void moveSouth(){
		currentY--;
		leavePheromoneCurrentPosition();
	}
	public void moveWest(){
		currentX--;
		leavePheromoneCurrentPosition();
	}
	public void moveEast(){
		currentX++;
		leavePheromoneCurrentPosition();
	}
	public void leavePheromoneCurrentPosition()
	{
		environment.leavePheromone(currentX, currentY);
	}
}
