package com.growthbeat.analytics.model;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

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

import com.growthbeat.model.Model;
import com.growthbeat.utils.DateUtils;
import com.growthbeat.utils.JSONObjectUtils;

public class ClientEvent extends Model {

	private String id;

	private String clientId;

	private String eventId;

	private Map<String, String> properties;

	private Date created;

	public ClientEvent() {
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getClientId() {
		return clientId;
	}

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

	public String getEventId() {
		return eventId;
	}

	public void setEventId(String eventId) {
		this.eventId = eventId;
	}

	public Map<String, String> getProperties() {
		return properties;
	}

	public void setProperties(Map<String, String> properties) {
		this.properties = properties;
	}

	public Date getCreated() {
		return created;
	}

	public void setCreated(Date created) {
		this.created = created;
	}

	@Override
	public JSONObject getJsonObject() {

		JSONObject jsonObject = new JSONObject();
		try {
			jsonObject.put("id", getId());
			jsonObject.put("clientId", clientId);
			jsonObject.put("eventId", eventId);
			JSONObject propertiesJsonObject = new JSONObject();
			for (Map.Entry<String, String> entry : properties.entrySet())
				propertiesJsonObject.put(entry.getKey(), entry.getValue());
			jsonObject.put("properties", propertiesJsonObject);
			jsonObject.put("created", DateUtils.formatToDateTimeString(getCreated()));
		} catch (JSONException e) {
			return null;
		}

		return jsonObject;

	}

	@Override
	public void setJsonObject(JSONObject jsonObject) {

		if (jsonObject == null)
			return;

		try {
			if (JSONObjectUtils.hasAndIsNotNull(jsonObject, "id"))
				setId(jsonObject.getString("id"));
			if (JSONObjectUtils.hasAndIsNotNull(jsonObject, "clientId"))
				setClientId(jsonObject.getString("clientId"));
			if (JSONObjectUtils.hasAndIsNotNull(jsonObject, "eventId"))
				setEventId(jsonObject.getString("eventId"));
			if (JSONObjectUtils.hasAndIsNotNull(jsonObject, "properties")) {
				Map<String, String> map = new HashMap<String, String>();
				JSONObject propertiesJsonObject = jsonObject.getJSONObject("properties");
				Iterator<String> iterator = propertiesJsonObject.keys();
				while (iterator.hasNext()) {
					String key = iterator.next();
					String value = propertiesJsonObject.getString(key);
					map.put(key, value);
				}
				setProperties(map);
			}
			if (JSONObjectUtils.hasAndIsNotNull(jsonObject, "created"))
				setCreated(DateUtils.parseFromDateTimeString(jsonObject.getString("created")));
		} catch (JSONException e) {
			throw new IllegalArgumentException("Failed to parse JSON.");
		}

	}
}

@@ -193,16 +193,65 @@
 bject;%0A%0A
+import com.growthbeat.analytics.GrowthAnalytics;%0A
 import c
@@ -568,16 +568,582 @@
 ent() %7B%0A
+%09%09super();%0A%09%7D%0A%0A%09public ClientEvent create(String clientId, String eventId, Map%3CString, String%3E properties) %7B%0A%0A%09%09Map%3CString, Object%3E params = new HashMap%3CString, Object%3E();%0A%09%09params.put(%22clientId%22, clientId);%0A%09%09params.put(%22eventId%22, eventId);%0A%09%09for (Map.Entry%3CString, String%3E entry : properties.entrySet())%0A%09%09%09params.put(String.format(%22properties%5B%25s%5D%22, entry.getKey()), entry.getValue());%0A%09%09JSONObject jsonObject = GrowthAnalytics.getInstance().getHttpClient().post(%221/client_events%22, params);%0A%09%09if (jsonObject != null)%0A%09%09%09setJsonObject(jsonObject);%0A%0A%09%09return this;%0A%0A
 %09%7D%0A%0A%09pub

Implement ClientEvent#create