package example;
import cruise.util.FileTracer;

public class Tracer
{

  //------------------------
  // MEMBER VARIABLES
  //------------------------

  //Tracer Attributes
  private String name;
  private int id;

  //Tracer State Machines
  public enum Bulb { Off, On }
  public enum BulbOn { Null, Normal, Dimmed }
  private Bulb bulb;
  private BulbOn bulbOn;

  //------------------------
  // CONSTRUCTOR
  //------------------------

  public Tracer(String aName, int aId)
  {
    name = aName;
    id = aId;
    setBulbOn(BulbOn.Null);
    setBulb(Bulb.Off);
  }

  //------------------------
  // INTERFACE
  //------------------------

  public boolean setName(String aName)
  {
    boolean wasSet = false;
    name = aName;
    wasSet = true;
    return wasSet;
  }

  public boolean setId(int aId)
  {
    boolean wasSet = false;
    id = aId;
    wasSet = true;
    return wasSet;
  }

  public String getName()
  {
    return name;
  }

  public int getId()
  {
    return id;
  }

  public String getBulbFullName()
  {
    String answer = bulb.toString();
    if (bulbOn != BulbOn.Null) { answer += "." + bulbOn.toString(); }
    return answer;
  }

  public Bulb getBulb()
  {
    return bulb;
  }

  public BulbOn getBulbOn()
  {
    return bulbOn;
  }

  public boolean flip()
  {
    boolean wasEventProcessed = false;
    
    Bulb aBulb = bulb;
    switch (aBulb)
    {
      case Off:
    FileTracer.handle( System.currentTimeMillis()+","+Thread.currentThread().getId()+",TraceCaseAttrStm.ump,21,Tracer,"+System.identityHashCode(this)+",sm_t,Off,flip,On" );
        setBulb(Bulb.On);
        wasEventProcessed = true;
        break;
      case On:
        exitBulb();
    FileTracer.handle( System.currentTimeMillis()+","+Thread.currentThread().getId()+",TraceCaseAttrStm.ump,21,Tracer,"+System.identityHashCode(this)+",sm_t,On,flip,Off" );
        setBulb(Bulb.Off);
        wasEventProcessed = true;
        break;
      default:
        // Other states do respond to this event
    }

    return wasEventProcessed;
  }

  private boolean enterOn()
  {
    boolean wasEventProcessed = false;
    
    BulbOn aBulbOn = bulbOn;
    switch (aBulbOn)
    {
      case Null:
        setBulbOn(BulbOn.Normal);
        wasEventProcessed = true;
        break;
      default:
        // Other states do respond to this event
    }

    return wasEventProcessed;
  }

  private boolean exitOn()
  {
    boolean wasEventProcessed = false;
    
    BulbOn aBulbOn = bulbOn;
    switch (aBulbOn)
    {
      case Normal:
        setBulbOn(BulbOn.Null);
        wasEventProcessed = true;
        break;
      case Dimmed:
        setBulbOn(BulbOn.Null);
        wasEventProcessed = true;
        break;
      default:
        // Other states do respond to this event
    }

    return wasEventProcessed;
  }

  public boolean touch()
  {
    boolean wasEventProcessed = false;
    
    BulbOn aBulbOn = bulbOn;
    switch (aBulbOn)
    {
      case Normal:
    FileTracer.handle( System.currentTimeMillis()+","+Thread.currentThread().getId()+",TraceCaseAttrStm.ump,21,Tracer,"+System.identityHashCode(this)+",sm_t,Normal,touch,Dimmed" );
        setBulbOn(BulbOn.Dimmed);
        wasEventProcessed = true;
        break;
      case Dimmed:
        exitBulb();
    FileTracer.handle( System.currentTimeMillis()+","+Thread.currentThread().getId()+",TraceCaseAttrStm.ump,21,Tracer,"+System.identityHashCode(this)+",sm_t,Dimmed,touch,Off" );
        setBulb(Bulb.Off);
        wasEventProcessed = true;
        break;
      default:
        // Other states do respond to this event
    }

    return wasEventProcessed;
  }

  private void exitBulb()
  {
    switch(bulb)
    {
      case On:
        exitOn();
        break;
    }
  }

  private void setBulb(Bulb aBulb)
  {
    bulb = aBulb;

    // entry actions and do activities
    switch(bulb)
    {
      case On:
        if (bulbOn == BulbOn.Null) { setBulbOn(BulbOn.Normal); }
        break;
    }
  }

  private void setBulbOn(BulbOn aBulbOn)
  {
    bulbOn = aBulbOn;
    if (bulb != Bulb.On && aBulbOn != BulbOn.Null) { setBulb(Bulb.On); }
  }

  public void delete()
  {}


  public String toString()
  {
    return super.toString() + "["+
            "name" + ":" + getName()+ "," +
            "id" + ":" + getId()+ "]";
  }
}
