package com.growthbeat.message.model;

import java.util.Date;

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

	private String id;
	private String applicationId;
	private Extension extension;
	private int width;
	private int height;
	private String name;
	private String url;
	private Date created;
	private Date updated;

	public Picture() {
		super();
	}

	public Picture(JSONObject jsonObject) {
		super(jsonObject);
	}

	public String getId() {
		return id;
	}

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

	public String getApplicationId() {
		return applicationId;
	}

	public void setApplicationId(String applicationId) {
		this.applicationId = applicationId;
	}

	public Extension getExtension() {
		return extension;
	}

	public void setExtension(Extension extension) {
		this.extension = extension;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public Date getCreated() {
		return created;
	}

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

	public Date getUpdated() {
		return updated;
	}

	public void setUpdated(Date updated) {
		this.updated = updated;
	}

	@Override
	public JSONObject getJsonObject() {

		JSONObject jsonObject = new JSONObject();

		try {
			if (id != null)
				jsonObject.put("id", id);
			if (applicationId != null)
				jsonObject.put("applicationId", applicationId);
			if (extension != null)
				jsonObject.put("extension", extension.toString());
			jsonObject.put("width", width);
			jsonObject.put("height", height);
			if (name != null)
				jsonObject.put("name", name);
			if (url != null)
				jsonObject.put("url", url);
			if (created != null)
				jsonObject.put("created", DateUtils.formatToDateTimeString(created));
			if (updated != null)
				jsonObject.put("updated", DateUtils.formatToDateTimeString(updated));
		} catch (Exception e) {
			throw new IllegalArgumentException("Failed to get JSON.");
		}

		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, "applicationId"))
				setApplicationId(jsonObject.getString("applicationId"));
			if (JSONObjectUtils.hasAndIsNotNull(jsonObject, "extension"))
				setExtension(Extension.valueOf(jsonObject.getString("extension")));
			if (JSONObjectUtils.hasAndIsNotNull(jsonObject, "width"))
				setWidth(jsonObject.getInt("width"));
			if (JSONObjectUtils.hasAndIsNotNull(jsonObject, "height"))
				setHeight(jsonObject.getInt("height"));
			if (JSONObjectUtils.hasAndIsNotNull(jsonObject, "name"))
				setName(jsonObject.getString("name"));
			if (JSONObjectUtils.hasAndIsNotNull(jsonObject, "url"))
				setUrl(jsonObject.getString("url"));
			if (JSONObjectUtils.hasAndIsNotNull(jsonObject, "created"))
				setCreated(DateUtils.parseFromDateTimeString(jsonObject.getString("created")));
			if (JSONObjectUtils.hasAndIsNotNull(jsonObject, "updated"))
				setUpdated(DateUtils.parseFromDateTimeString(jsonObject.getString("updated")));
		} catch (JSONException e) {
			throw new IllegalArgumentException("Failed to parse JSON.", e);
		}

	}

	public enum Extension {
		png,
		jpg
	}

}

@@ -329,101 +329,8 @@
 Id;%0A
-%09private Extension extension;%0A%09private int width;%0A%09private int height;%0A%09private String name;%0A
 %09pri
@@ -372,31 +372,8 @@
 ted;
-%0A%09private Date updated;
 %0A%0A%09p
@@ -729,474 +729,8 @@
 %09%7D%0A%0A
-%09public Extension getExtension() %7B%0A%09%09return extension;%0A%09%7D%0A%0A%09public void setExtension(Extension extension) %7B%0A%09%09this.extension = extension;%0A%09%7D%0A%0A%09public int getWidth() %7B%0A%09%09return width;%0A%09%7D%0A%0A%09public void setWidth(int width) %7B%0A%09%09this.width = width;%0A%09%7D%0A%0A%09public int getHeight() %7B%0A%09%09return height;%0A%09%7D%0A%0A%09public void setHeight(int height) %7B%0A%09%09this.height = height;%0A%09%7D%0A%0A%09public String getName() %7B%0A%09%09return name;%0A%09%7D%0A%0A%09public void setName(String name) %7B%0A%09%09this.name = name;%0A%09%7D%0A%0A
 %09pub
@@ -949,128 +949,8 @@
 %09%7D%0A%0A
-%09public Date getUpdated() %7B%0A%09%09return updated;%0A%09%7D%0A%0A%09public void setUpdated(Date updated) %7B%0A%09%09this.updated = updated;%0A%09%7D%0A%0A
 %09@Ov
@@ -1182,216 +1182,8 @@
 d);%0A
-%09%09%09if (extension != null)%0A%09%09%09%09jsonObject.put(%22extension%22, extension.toString());%0A%09%09%09jsonObject.put(%22width%22, width);%0A%09%09%09jsonObject.put(%22height%22, height);%0A%09%09%09if (name != null)%0A%09%09%09%09jsonObject.put(%22name%22, name);%0A
 %09%09%09i
@@ -1332,106 +1332,8 @@
 ));%0A
-%09%09%09if (updated != null)%0A%09%09%09%09jsonObject.put(%22updated%22, DateUtils.formatToDateTimeString(updated));%0A
 %09%09%7D 
@@ -1788,457 +1788,8 @@
 ));%0A
-%09%09%09if (JSONObjectUtils.hasAndIsNotNull(jsonObject, %22extension%22))%0A%09%09%09%09setExtension(Extension.valueOf(jsonObject.getString(%22extension%22)));%0A%09%09%09if (JSONObjectUtils.hasAndIsNotNull(jsonObject, %22width%22))%0A%09%09%09%09setWidth(jsonObject.getInt(%22width%22));%0A%09%09%09if (JSONObjectUtils.hasAndIsNotNull(jsonObject, %22height%22))%0A%09%09%09%09setHeight(jsonObject.getInt(%22height%22));%0A%09%09%09if (JSONObjectUtils.hasAndIsNotNull(jsonObject, %22name%22))%0A%09%09%09%09setName(jsonObject.getString(%22name%22));%0A
 %09%09%09i
@@ -2035,155 +2035,8 @@
 ));%0A
-%09%09%09if (JSONObjectUtils.hasAndIsNotNull(jsonObject, %22updated%22))%0A%09%09%09%09setUpdated(DateUtils.parseFromDateTimeString(jsonObject.getString(%22updated%22)));%0A
 %09%09%7D 
@@ -2141,48 +2141,6 @@
 %09%7D%0A%0A
-%09public enum Extension %7B%0A%09%09png,%0A%09%09jpg%0A%09%7D%0A%0A
 %7D%0A

refactor Picture