package com.humbughq.android;

import java.util.Date;

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

import android.text.TextUtils;

public class Message {
    public static final int STREAM_MESSAGE = 1;
    public static final int HUDDLE_MESSAGE = 2;

    private String sender;
    private int type;
    private String content;
    private String subject;
    private String senderEmail;
    private Date curDateTime;
    private String[] recipients;

    public Message() {
        // Default constructor
    }

    public Message(JSONObject message) throws JSONException {
        this.populate(message);
    }

    public void populate(JSONObject message) throws JSONException {
        this.setSender(message.getString("sender_full_name"));
        this.setSenderEmail(message.getString("sender_email"));
        if (message.getString("type").equals("stream")) {
            this.setType(Message.STREAM_MESSAGE);
            recipients = new String[1];
            recipients[0] = message.getString("display_recipient");
        } else if (message.getString("type").equals("huddle")) {
            this.setType(Message.HUDDLE_MESSAGE);
            JSONArray jsonRecipients = message
                    .getJSONArray("display_recipient");

            recipients = new String[jsonRecipients.length()];

            for (int i = 0; i < jsonRecipients.length(); i++) {
                recipients[i] = jsonRecipients.getJSONObject(i).getString(
                        "short_name");
            }
        }
        this.setContent(message.getString("content"));
        if (this.getType() == Message.STREAM_MESSAGE) {
            this.setSubject(message.getString("subject"));
        } else {
            this.setSubject(null);
        }

        this.setCurDateTime(new Date(message.getInt("timestamp")));
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public String getRecipient() {
        return TextUtils.join(", ", recipients);
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getSender() {
        return sender;
    }

    public void setSender(String sender) {
        this.sender = sender;
    }

    public String getSenderEmail() {
        return senderEmail;
    }

    public void setSenderEmail(String senderEmail) {
        this.senderEmail = senderEmail;
    }

    public Date getCurDateTime() {
        return curDateTime;
    }

    public void setCurDateTime(Date curDateTime) {
        this.curDateTime = curDateTime;
    }
}

@@ -285,16 +285,66 @@
 AGE = 2;
+%0A    public static final int PERSONAL_MESSAGE = 3;
 %0A%0A    pr
@@ -1329,17 +1329,16 @@
 ient%22);%0A
-%0A
         
@@ -1566,24 +1566,24 @@
 ort_name%22);%0A
-
             
@@ -1576,32 +1576,308 @@
 ;%0A            %7D%0A
+        %7D else if (message.getString(%22type%22).equals(%22personal%22)) %7B%0A            this.setType(Message.PERSONAL_MESSAGE);%0A            recipients = new String%5B1%5D;%0A            recipients%5B0%5D = message.getJSONObject(%22display_recipient%22)%0A                    .getString(%22short_name%22);%0A
         %7D%0A      

Include PERSONAL_MESSAGE to prevent crashes on receiving a personal.