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 static final String TAG_KEY = "tags";

	private int tagId;

	private long clientId;

	private String value;

	public Tag() {
		super();
	}

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

	public static Tag create(String clientId, String credentialId, String name, String value) {

		Map<String, Object> params = new HashMap<String, Object>();
		if (clientId != null)
			params.put("clientId", clientId);
		if (credentialId != null)
			params.put("credentialId", credentialId);
		if (name != null)
			params.put("name", name);
		if (value != null)
			params.put("value", value);

		JSONObject jsonObject = GrowthPush.getInstance().getHttpClient().post("3/tags", params);
		if (jsonObject == null)
			return null;

		return new Tag(jsonObject);

	}

	private static JSONObject tags() {
		JSONObject tags = GrowthPush.getInstance().getPreference().get(TAG_KEY);
		if (tags == null)
			return new JSONObject();

		return tags;
	}

	public static void save(Tag tag, String name) {

		if (tag == null)
			return;

		JSONObject tags = Tag.tags();
		try {
			tags.put(name, tag.getJsonObject());
		} catch (JSONException e) {
		}

		GrowthPush.getInstance().getPreference().save(TAG_KEY, tags);

	}

	public static Tag load(String name) {

		JSONObject tags = GrowthPush.getInstance().getPreference().get(TAG_KEY);
		if (tags == null)
			return null;

		if (!tags.has(name))
			return null;


		Tag tag = null;
		try {
			tag = new Tag(tags.getJSONObject(name));
		} catch (JSONException e) {
		}

		return tag;

	}

	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());
			if (value != null)
				jsonObject.put("value", getValue());
		} catch (JSONException e) {
			return null;
		}

		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) {
			throw new IllegalArgumentException("Failed to parse JSON.");
		}

	}
}

@@ -322,16 +322,23 @@
 _KEY
+_FORMAT
  = %22tags
 %22;%0A%0A
@@ -333,16 +333,19 @@
  = %22tags
+:%25s
 %22;%0A%0A%09pri
@@ -1088,760 +1088,435 @@
 %0A%0A%09p
-rivate static JSONObject tags() %7B%0A%09%09JSONObject tags = GrowthPush.getInstance().getPreference().get(TAG_KEY);%0A%09%09if (tags == null)%0A%09%09%09return new JSONObject();%0A%0A%09%09return tags;%0A%09%7D%0A%0A%09public static void save(Tag tag, String name) %7B%0A%0A%09%09if (tag == null)%0A%09%09%09return;%0A%0A%09%09JSONObject tags = Tag.tags();%0A%09%09try %7B%0A%09%09%09tags.put(name, tag.getJsonObject());%0A%09%09%7D catch (JSONException e) %7B%0A%09%09%7D%0A%0A%09%09GrowthPush.getInstance().getPreference().save(TAG_KEY, tags);%0A%0A%09%7D%0A%0A%09public static Tag load(String name) %7B%0A%0A%09%09JSONObject tags = GrowthPush.getInstance().getPreference().get(TAG_KEY);%0A%09%09if (tags == null)%0A%09%09%09return null;%0A%0A%09%09if (!tags.has(name))%0A%09%09%09return null;%0A%0A%0A%09%09Tag tag = null;%0A%09%09try %7B%0A%09%09%09tag = new Tag(tags.getJSONObject(name));%0A%09%09%7D catch (JSONException e) %7B%0A%09%09%7D%0A%0A%09%09return tag
+ublic static void save(Tag tag, String name) %7B%0A%0A%09%09if (tag == null %7C%7C name == null %7C%7C name.length() == 0)%0A%09%09%09return;%0A%0A%09%09GrowthPush.getInstance().getPreference().save(String.format(TAG_KEY_FORMAT, name), tag.getJsonObject());%0A%0A%09%7D%0A%0A%09public static Tag load(String name) %7B%0A%0A%09%09if (name == null %7C%7C name.length() == 0)%0A%09%09%09return null;%0A%0A%09%09return new Tag(GrowthPush.getInstance().getPreference().get(String.format(TAG_KEY_FORMAT, name)))
 ;%0A%0A%09

Modify preference behavior of tag