package com.uservoice.uservoicesdk.model;

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

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

import com.uservoice.uservoicesdk.rest.Callback;
import com.uservoice.uservoicesdk.rest.RestTask;
import com.uservoice.uservoicesdk.rest.RestTaskCallback;

public class Article extends BaseModel {

    private String title;
    private String html;
    private String topicName;
    private int weight;

    public static void loadAll(final Callback<List<Article>> callback) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("sort", "ordered");
        doGet(apiPath("/articles.json"), params, new RestTaskCallback(callback) {
            @Override
            public void onComplete(JSONObject result) throws JSONException {
                callback.onModel(deserializeList(result, "articles", Article.class));
            }
        });
    }

    public static void loadForTopic(int topicId, final Callback<List<Article>> callback) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("sort", "ordered");
        doGet(apiPath("/topics/%d/articles.json", topicId), params, new RestTaskCallback(callback) {
            @Override
            public void onComplete(JSONObject result) throws JSONException {
                callback.onModel(deserializeList(result, "articles", Article.class));
            }
        });
    }

    public static RestTask loadInstantAnswers(String query, final Callback<List<BaseModel>> callback) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("per_page", "3");
        params.put("forum_id", String.valueOf(getConfig().getForumId()));
        params.put("query", query);
        if (getConfig().getTopicId() != -1) {
            params.put("topic_id", String.valueOf(getConfig().getTopicId()));
        }
        return doGet(apiPath("/instant_answers/search.json"), params, new RestTaskCallback(callback) {
            @Override
            public void onComplete(JSONObject result) throws JSONException {
                callback.onModel(deserializeHeterogenousList(result, "instant_answers"));
            }
        });
    }

    @Override
    public void load(JSONObject object) throws JSONException {
        super.load(object);
        title = getString(object, "question");
        html = getHtml(object, "answer_html");
        if (object.has("normalized_weight")) {
            weight = object.getInt("normalized_weight");
        }
        if (!object.isNull("topic")) {
            JSONObject topic = object.getJSONObject("topic");
            topicName = topic.getString("name");
        }
    }

    public String getTitle() {
        return title;
    }

    public String getHtml() {
        return html;
    }

    public String getTopicName() {
        return topicName;
    }

    public int getWeight() {
        return weight;
    }
}

@@ -36,16 +36,73 @@
 model;%0A%0A
+import android.os.Parcel;%0Aimport android.os.Parcelable;%0A%0A
 import j
@@ -420,16 +420,38 @@
 seModel 
+implements Parcelable 
 %7B%0A%0A    p
@@ -551,16 +551,47 @@
 eight;%0A%0A
+    public Article() %7B%0A%0A    %7D%0A%0A
     publ
@@ -3080,11 +3080,748 @@
 t;%0A    %7D
+%0A%0A    public int describeContents() %7B%0A        return 0;%0A    %7D%0A%0A    public void writeToParcel(Parcel out, int flags) %7B%0A        out.writeString(title);%0A        out.writeString(html);%0A        out.writeString(topicName);%0A        out.writeInt(weight);%0A    %7D%0A%0A    public static final Parcelable.Creator%3CArticle%3E CREATOR%0A            = new Parcelable.Creator%3CArticle%3E() %7B%0A        public Article createFromParcel(Parcel in) %7B%0A            return new Article(in);%0A        %7D%0A%0A        public Article%5B%5D newArray(int size) %7B%0A            return new Article%5Bsize%5D;%0A        %7D%0A    %7D;%0A%0A    private Article(Parcel in) %7B%0A        title = in.readString();%0A        html = in.readString();%0A        topicName = in.readString();%0A        weight = in.readInt();%0A    %7D
 %0A%7D%0A

Make Article implement Parcelable