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

public class NonImmuneWalker extends PlagueVictim {
  // OVERVIEW: A NonImmuneWalker is a healthy person who has no immunity to the
  //           virus. He walks around randomly. When he meets a person carrying
  //           the virus he becomes infected and will spread the virus to every
  //           healthy person he meets.

  private boolean inTurn; // Currently in the middle of a turn used for coloring.
  int timer;

  public NonImmuneWalker () {
    super();//used only for constructors. It inherits the state of the superclass.
    inTurn = false;
    PlagueSimulator.setNonImmuneCount(PlagueSimulator.nonImmuneCount + 1);
        }

  public Color getColor() {
    //@assume state !=null
    if (!inTurn && state.equals("uninfected")){
      //@assume Color.orange !=null
      return Color.orange.darker();
    }
    else if (inTurn && state.equals("uninfected")){
      return Color.orange;
    }
    else if (!inTurn && state.equals("infected")){
      //@assume Color.red !=null
      return Color.red.darker();
    }
    else if (inTurn && state.equals("infected")){
      return Color.red;
    }
    else if (state.equals("dead")){
      return Color.gray;
    }
    else
      return null;
    } //@nowarn NonNullResult // ESC/Java doesn't know Color constants are not null

    public synchronized void becomeInfected() {
      //@assume state != null
      if(state.equals("uninfected")){
        state= "infected";
        timer= (int)(PlagueSimulator.fitness * Math.random()) + PlagueSimulator.lifespan;
        PlagueSimulator.setInfectedCount(PlagueSimulator.infectedCount + 1);
        PlagueSimulator.setNonImmuneCount(PlagueSimulator.nonImmuneCount - 1);
      }
    }
    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, return true. If the move fails
      //          because the spot being attempted to move into is already
      //          occupied then return false.
    {
      //@assume state != null
      if (!state.equals("dead")){
        inTurn= true;
        super.executeTurn();
        infect();
        if (state.equals("infected")) {
          timer--;
          if (timer<=0){
            state="dead";
            PlagueSimulator.setDeadCount(PlagueSimulator.deadCount + 1);
            PlagueSimulator.setInfectedCount(PlagueSimulator.infectedCount - 1);
          }
        }
        inTurn= false;
      }
    }
}