package com.growthpush.model;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.growthbeat.model.Model;
import com.growthbeat.utils.JSONObjectUtils;
import com.growthpush.GrowthPush;

public class Event extends Model {

	private int goalId;

	private long clientId;

	private long timestamp;

	private String value;

	public Event(JSONObject jsonObject) {
		super();
		setJsonObject(jsonObject);
	}

	public static Event create(String clientId, String credentialId, String name, String value) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("clientId", clientId);
		params.put("credentialId", credentialId);
		params.put("name", name);
		if (value != null)
			params.put("value", value);
		JSONObject jsonObject = GrowthPush.getInstance().getHttpClient().post("3/events", params);
		return new Event(jsonObject);
	}

	public int getGoalId() {
		return goalId;
	}

	public void setGoalId(int goalId) {
		this.goalId = goalId;
	}

	public long getClientId() {
		return clientId;
	}

	public void setClientId(long clientId) {
		this.clientId = clientId;
	}

	public long getTimestamp() {
		return timestamp;
	}

	public void setTimestamp(long timestamp) {
		this.timestamp = timestamp;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	@Override
	public JSONObject getJsonObject() {
		JSONObject jsonObject = new JSONObject();

		try {
			jsonObject.put("goalId", getGoalId());
			jsonObject.put("clientId", getClientId());
			jsonObject.put("timestamp", getTimestamp());
			jsonObject.put("value", getValue());
		} catch (JSONException e) {
		}

		return jsonObject;
	}

	@Override
	public void setJsonObject(JSONObject jsonObject) {
		try {
			if (JSONObjectUtils.hasAndIsNotNull(jsonObject, "goalId"))
				setGoalId(jsonObject.getInt("goalId"));
			if (JSONObjectUtils.hasAndIsNotNull(jsonObject, "clientId"))
				setClientId(jsonObject.getLong("clientId"));
			if (JSONObjectUtils.hasAndIsNotNull(jsonObject, "timestamp"))
				setTimestamp(jsonObject.getLong("timestamp"));
			if (JSONObjectUtils.hasAndIsNotNull(jsonObject, "value"))
				setValue(jsonObject.getString("value"));
		} catch (JSONException e) {
		}
	}

}

@@ -381,16 +381,49 @@
 value;%0A%0A
+%09public Event() %7B%0A%09%09super();%0A%09%7D%0A%0A
 %09public 
@@ -451,29 +451,28 @@
 Object) %7B%0A%09%09
-super
+this
 ();%0A%09%09setJso
@@ -591,16 +591,17 @@
 alue) %7B%0A
+%0A
 %09%09Map%3CSt
@@ -654,16 +654,41 @@
 ect%3E();%0A
+%09%09if (clientId != null)%0A%09
 %09%09params
@@ -711,24 +711,53 @@
  clientId);%0A
+%09%09if (credentialId != null)%0A%09
 %09%09params.put
@@ -788,16 +788,37 @@
 ialId);%0A
+%09%09if (name != null)%0A%09
 %09%09params
@@ -889,16 +889,17 @@
 value);%0A
+%0A
 %09%09JSONOb
@@ -983,16 +983,424 @@
 arams);%0A
+%09%09if (jsonObject == null)%0A%09%09%09return null;%0A%0A%09%09return new Event(jsonObject);%0A%0A%09%7D%0A%0A%09public static void save(Event event, String name) %7B%0A%09%09if (event == null)%0A%09%09%09return;%0A%09%09GrowthPush.getInstance().getPreference().save(name, event.getJsonObject());%0A%09%7D%0A%0A%09public static Event load(String name) %7B%0A%09%09JSONObject jsonObject = GrowthPush.getInstance().getPreference().get(name);%0A%09%09if (jsonObject == null)%0A%09%09%09return null;%0A
 %09%09return
@@ -2142,24 +2142,47 @@
 mestamp());%0A
+%09%09%09if (value != null)%0A%09
 %09%09%09jsonObjec
@@ -2235,24 +2235,40 @@
 eption e) %7B%0A
+%09%09%09return null;%0A
 %09%09%7D%0A%0A%09%09retur
@@ -2348,16 +2348,55 @@
 ject) %7B%0A
+%0A%09%09if (jsonObject == null)%0A%09%09%09return;%0A%0A
 %09%09try %7B%0A
@@ -2866,16 +2866,80 @@
 on e) %7B%0A
+%09%09%09throw new IllegalArgumentException(%22Failed to parse JSON.%22);%0A
 %09%09%7D%0A%09%7D%0A%0A

Add save and load preference into Event