package commons.cmd;
import basics.profiler.Profiler;
import commons.application.GLOBAL;
import commons.application.Application;
import commons.application.core.Command;
public class timer extends Command
{

private static final String MUSTHAVE = "do,id";
private static final String ARGS     = "do:report|resume|start|stop|reset;id:<id>;incr:<number>";

public void process () throws Exception
{
   String cmd = ctx.consume("do", "");
   String id = ctx.consume("id", "");
   long incr = ctx.consume("incr", 0L);
   long counter = ctx.consume("counter", 0L);
   long tsize = ctx.consume("tsize", 0L);
   boolean report = false;
//   boolean done = false;
   if (cmd.equals("resume"))
   {
      Profiler.resume(id);
   }
   else if (cmd.equals("start"))
   {
      Profiler.start(id);
   }
   else if (cmd.equals("stop"))
   {
      if (id.equals("*")) Profiler.stopAll();
      else
      {
         Profiler.stop(id);
//         done = true;
      }
//      report = true;
   }
   else if (cmd.equals("reset"))
   {
      if (id.equals("*")) Profiler.reset();
      else Profiler.reset(id);
   }
   else if (cmd.equals("report"))
   {
      report = true;
   }
   if (incr != 0) Profiler.increment(id, incr);
   if (counter != 0) Profiler.setCounter(id, counter);
   if (tsize != 0) Profiler.targetsize(id, tsize);
   if (report)
   {
      if (id.equals("*")) Application.ui().print(Profiler.report());
      else Application.ui().print(GLOBAL.VAL_PREFIX_DONE + Profiler.report(id));
   }
}

public void signContract ()
{
   demand(MUSTHAVE, null);
}

public String description ()
{
   return implName() + " Start/resume/stop/increment a timer with an id. " + ARGS;
}
}
