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 Tag extends Model {

	private int tagId;

	private long clientId;

	private String value;

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

	public static Tag 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/tags", params);
		return new Tag(jsonObject);
	}

	public int getTagId() {
		return tagId;
	}

	public void setTagId(int tagId) {
		this.tagId = tagId;
	}

	public long getClientId() {
		return clientId;
	}

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

	public String getValue() {
		return value;
	}

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

	@Override
	public JSONObject getJsonObject() {
		JSONObject jsonObject = new JSONObject();
		try {
			jsonObject.put("tagId", getTagId());
			jsonObject.put("clientId", getClientId());
			jsonObject.put("value", getValue());
		} catch (JSONException e) {
		}

		return jsonObject;
	}

	@Override
	public void setJsonObject(JSONObject jsonObject) {

		try {
			if (JSONObjectUtils.hasAndIsNotNull(jsonObject, "tagId"))
				setTagId(jsonObject.getInt("tagId"));
			if (JSONObjectUtils.hasAndIsNotNull(jsonObject, "clientId"))
				setClientId(jsonObject.getLong("clientId"));
			if (JSONObjectUtils.hasAndIsNotNull(jsonObject, "value"))
				setValue(jsonObject.getString("value"));
		} catch (JSONException e) {
		}

	}
}

@@ -526,16 +526,17 @@
 alue) %7B%0A
+%0A
 %09%09Map%3CSt
@@ -589,16 +589,41 @@
 ect%3E();%0A
+%09%09if (clientId != null)%0A%09
 %09%09params
@@ -646,24 +646,53 @@
  clientId);%0A
+%09%09if (credentialId != null)%0A%09
 %09%09params.put
@@ -723,16 +723,37 @@
 ialId);%0A
+%09%09if (name != null)%0A%09
 %09%09params
@@ -824,16 +824,17 @@
 value);%0A
+%0A
 %09%09JSONOb
@@ -916,16 +916,412 @@
 arams);%0A
+%09%09if (jsonObject == null)%0A%09%09%09return null;%0A%0A%09%09return new Tag(jsonObject);%0A%0A%09%7D%0A%0A%09public static void save(Tag tag, String name) %7B%0A%09%09if (tag == null)%0A%09%09%09return;%0A%09%09GrowthPush.getInstance().getPreference().save(name, tag.getJsonObject());%0A%09%7D%0A%0A%09public static Tag 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
@@ -1872,24 +1872,47 @@
 lientId());%0A
+%09%09%09if (value != null)%0A%09
 %09%09%09jsonObjec
@@ -1935,24 +1935,24 @@
 etValue());%0A
-
 %09%09%7D catch (J
@@ -1965,24 +1965,40 @@
 eption e) %7B%0A
+%09%09%09return null;%0A
 %09%09%7D%0A%0A%09%09retur
@@ -2405,24 +2405,24 @@
 (%22value%22));%0A
-
 %09%09%7D catch (J
@@ -2439,16 +2439,80 @@
 on e) %7B%0A
+%09%09%09throw new IllegalArgumentException(%22Failed to parse JSON.%22);%0A
 %09%09%7D%0A%0A%09%7D%0A

Add save and load preference into Tag