import java.awt.*;
import java.util.*;

public class GridBarEater implements GridThing {
    boolean loaded = false, dropping=false, switched=false;
    int duration=0;
    int wanderLust = 3;
    double lastDouble=-1;
    int starvation = 50;
    int nourishment = 12;
    int snackSize=20;
    int babyThresh = 15;
    int babyCost = 4;
    static Random r = new Random();
    int lastMove;
    int ateAt;

    public void behave(GridSquare[][] g, int mx, int my, int move){
	int gridSize = g[0].length;
	if(move == lastMove)
	    return;
	lastMove = move;
	nourishment--;
	if(nourishment < 0){
	    if(!switched){
		GridFooEater gbe;
		g[mx][my].addThing(gbe=new GridFooEater());
		gbe.nourishment=20;
		gbe.loaded = loaded;
		gbe.switched = true;
	    }
	    else if(loaded)
		g[mx][my].addThing(new GridBar());
	    else
		g[mx][my].addThing(null);
	    return;
	}
	GridSquare right = g[(mx+1)%gridSize][my];
	GridSquare left = g[(mx+(gridSize-1))%gridSize][my];
	GridSquare up = g[mx][(my+1)%gridSize];
	GridSquare down = g[mx][(my+(gridSize-1))%gridSize];
	if((duration < wanderLust) || loaded){
	    // Wander around
	    wander(g, mx, my);
	    duration++;
	}
	/*	else if(loaded){
	    // Look for a place to put it down
	    loaded = false;
	    dropping = true;
	    if((right.thing instanceof GridDirt) ||
	       (left.thing instanceof GridDirt) ||
	       (down.thing instanceof GridDirt) ||
	       (up.thing instanceof GridDirt)){
		if(right.thing == null){
		    right.addThing(new GridDirt());
		    loaded = false;
		    duration = 0;
		}
		else if(left.thing == null){
		    left.addThing(new GridDirt());
		    loaded = false;
		    duration = 0;
		}
		else if(down.thing == null){
		    down.addThing(new GridDirt());
		    loaded = false;
		    duration = 0;
		}
		else if(up.thing == null){
		    up.addThing(new GridDirt());
		    loaded = false;
		    duration = 0;
		}
	    }
	    else{
		wander(g, mx, my);
	    }
	}
	    */
	else{
	    GridSquare trysq;
	    for(int t=0;t<5;t++){
		double rn = r.nextDouble();
		String dir;
		if(rn > .75){
		    trysq = up;
		    dir = new String("up");
		}
		else if(rn > .5){
		    trysq = down;
		    dir = new String("down");
		}
		else if(rn > .25){
		    trysq = left;
		    dir = new String("left");
		}
		else{
		    trysq = right;
		    dir = new String("right");
		}
		
		if(trysq.thing instanceof GridBar){
		    trysq.addThing(null);
		    nourishment+=snackSize;
		    loaded = true;
		    duration = 0;
		    return;
		}
	    }
	    duration++;
	    wander(g, mx, my);
	}
    }

    public void wander(GridSquare[][] g, int mx, int my){
	int gridSize = g[0].length;
	GridSquare right = g[(mx+1)%gridSize][my];
	GridSquare left = g[(mx+(gridSize-1))%gridSize][my];
	GridSquare up = g[mx][(my+1)%gridSize];
	GridSquare down = g[mx][(my+(gridSize-1))%gridSize];

	GridSquare trysq;
	for(int t=0;t<5;t++){
	    double rn = r.nextDouble();
	    if((lastDouble > 0) &&
	       (rn > .05))
		rn = lastDouble;
	    else
		rn = r.nextDouble();

	    lastDouble = rn;
	    String dir;
	    if(rn > .75){
		trysq = up;
		dir = new String("up");
	    }
	    else if(rn > .5){
		trysq = down;
		dir = new String("down");
	    }
	    else if(rn > .25){
		trysq = left;
		dir = new String("left");
	    }
	    else{
		trysq = right;
		dir = new String("right");
	    }

	    if(trysq.thing == null){
		//		System.out.println("Wandering "+dir+", dropping "+dropping);
		trysq.addThing(this);
		if(loaded && ((right.thing instanceof GridDirt) ||
				(left.thing instanceof GridDirt) ||
				(down.thing instanceof GridDirt) ||
				(up.thing instanceof GridDirt))) {
		    if(nourishment > (babyThresh*snackSize)) {
			//			System.out.println("Bar, I'm well fed!");
			nourishment -= ((babyCost)*snackSize);
			GridBarEater gbe;
			g[mx][my].addThing(gbe=new GridBarEater());
			gbe.loaded = true;
		    }
		    else{
			g[mx][my].addThing(new GridFoo());
		    }
		    loaded = false;
		    duration  = 0;
		}
		else
		    g[mx][my].addThing(null);

		return;
	    }
	}
	lastMove = -1;
    }

    public Color getColor() {
	return Color.blue;
    }
}
