import java.awt.Color;
import java.util.Enumeration;

public class Crawler extends Player {
	//  OVERVIEW:  A Crawler is of type Player and randomly walks the corner,
	//	encountering bars, food, police, etc. and interacts with them.  

	private boolean/*@non_null@*/ inTurn;
//	Currently in the middle of a turn (used for coloring)
	public /*@non_null@*/String state;
	private int/*@non_null@*/ money;
	private int/*@non_null@*/ drinks;
	private Player Neighbor;

	public Crawler() {
		//Constructor
		inTurn = false;
		state = "Crawler";
		money = 50;
		Simulator.setMoney(money);
		drinks = 0;
		Neighbor = null;
	}

	public Color getColor() 
	//	Effects: returns the color associated with this
	{
		if (inTurn) {
			return Color.red;
		} else {
			return Color.red;
		}
	}//@nowarn Post // ESC/Java doesn't know Color constants are not null
	
	public void interact(SimObject/*@non_null@*/ neighbor) { //@nowarn Deadlock
		//  REQUIRES: Neighbor != null
		//  EFFECTS: Takes the surrounding neighbors and interacts with them
		//  (i.e. buys drinks and spends money at a bar if crawler has enough money, 
		//  gets more money at a bank, spends money on food, or goes home).	
		if (neighbor instanceof Bar) {
			if (drinks > 25) {
				System.out.println("Whoa...you are smashed.  Go home.");
				return;
			}
			if (money >= 5) {
				money = money - 5;
				drinks += 1;
				Simulator.setDrinks(drinks);
				Simulator.setMoney(money);
				System.err.println("Beer me Coach!");
				delay(600);
				System.err.println("Money:" + money);
				System.err.println("drink count:  " + drinks);

			} else
				System.err.println("Not Enough Money :( ");
		}
		if (neighbor instanceof Bank) {
			money = money + 30;
			Simulator.setMoney(money);
			System.err.println("You now have money...hit up the bars!");
			delay(200);
			System.err.println("Money:" + money);

		}
		if (neighbor instanceof Food) {
			if (money >= 4) {
				money = money - 4;
				Simulator.setMoney(money);
				System.err.println("Drunken eating is the best!");
				delay(700);
				System.err.println("Money:" + money);

			} else
				System.err.println("No Money = No Food");
		}
		if (neighbor instanceof Home) {
			if (drinks >= 15) {
				System.err.println("Congratulations: You are Drunk. Bedtime!");
				System.exit(0);
				System.err.println("You ended the night with");

				System.err.println("drink count:  " + drinks);
			} else if (drinks < 15) {
				System.err.println("You are not drunk ---> go back out!");
			}
			delay(300);
		}
		if (neighbor instanceof Students) {
			System.err.println("Hey! Let's go get drinks!");
			delay(600);
		}
		if (neighbor instanceof Police) {
			if (drinks > 15) {
				System.err.println("You're arrested. Game Over!");
				System.exit(0);
			}
			delay(600);
			if (drinks < 15) {
				if (money >= 15) {
					System.err.println("I'm sorry son, I'm ticketing you");
					money = money - 15;
					Simulator.setMoney(money);
					System.err.println("Money:" + money);
					System.err.println("drink count:  " + drinks);
				} else if (money < 15) {
					System.err.println("You're arrested. Game Over!");
					System.exit(0);
				}
			}
		}
		if (neighbor instanceof Townies) {
			if (drinks >= 3) {

				drinks = drinks - 3;
			}
			money = 0;
			System.err.println("Townies Rule!!!");
			System.err.println("We're beating your ass and robbing you!");
			Simulator.setMoney(money);
			Simulator.setDrinks(drinks);
			System.err.println("Money:" + money);
			System.err.println("drink count:  " + drinks);
		}

	}
	public void executeTurn() throws RuntimeException
			// Note: requires isInitialized is inherited from SimObject
			// EFFECTS: Picks a random direction and tries to move that way. 
			// 	If the move is successful, interacts with neighbor. If the neighbor
			//  is null, tries another direction.
		{		
				inTurn = true;
				synchronized(getGrid()){
				super.executeTurn();
				Enumeration neighbors = getNeighbors();
			
					while (neighbors.hasMoreElements()) {				
						SimObject neighbor = (SimObject)neighbors.nextElement();				
						if(neighbor != null){			
							interact(neighbor);
						}
					}
				}			
						inTurn = false;
		}
}
//Modeled after Philosopher.java and modified