package com.lemenzo.clock.timer;import org.json.*;
public final class TimerItem{
 public long id,durationMs,endAt,remainingMs;public String label="";public boolean running=true,paused=false,finished=false;
 public long remaining(long now){if(finished)return 0;if(paused||!running)return Math.max(0,remainingMs>0?remainingMs:durationMs);return Math.max(0,endAt-now);}
 public JSONObject json()throws JSONException{return new JSONObject().put("id",id).put("duration",durationMs).put("end",endAt).put("remaining",remainingMs).put("label",label).put("running",running).put("paused",paused).put("finished",finished);}
 public static TimerItem from(JSONObject o)throws JSONException{TimerItem t=new TimerItem();t.id=o.getLong("id");t.durationMs=o.getLong("duration");t.endAt=o.optLong("end",0);t.remainingMs=o.optLong("remaining",0);t.label=o.optString("label","");t.running=o.optBoolean("running",true);t.paused=o.optBoolean("paused",false);t.finished=o.optBoolean("finished",false);if(t.paused&&t.remainingMs<=0)t.remainingMs=t.durationMs;return t;}
}