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;
    public static final int PERSONAL_MESSAGE = 3;

    private String sender;
    private int type;
    private String content;
    private String subject;
    private String senderEmail;
    private Date timestamp;
    private String[] recipients;
    private int id;
    private String your_email;

    public Message(HumbugActivity context, JSONObject message)
            throws JSONException {
        this.your_email = context.email;
        this.populate(message);
    }

    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() - 1];

            for (int i = 0; i < jsonRecipients.length(); i++) {
                if (!jsonRecipients.getJSONObject(i).getString("email")
                        .equals(this.your_email)) {
                    recipients[i] = jsonRecipients.getJSONObject(i).getString(
                            "full_name");
                }
            }
        } else if (message.getString("type").equals("personal")) {
            this.setType(Message.PERSONAL_MESSAGE);
            recipients = new String[1];
            recipients[0] = message.getJSONObject("display_recipient")
                    .getString("short_name");
        }
        this.setContent(message.getString("content"));
        if (this.getType() == Message.STREAM_MESSAGE) {
            this.setSubject(message.getString("subject"));
        } else {
            this.setSubject(null);
        }

        this.setTimestamp(new Date(message.getInt("timestamp")));
        this.setID(message.getInt("id"));
    }

    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 getTimestamp() {
        return timestamp;
    }

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

    public int getID() {
        return id;
    }

    public void setID(int id) {
        this.id = id;
    }
}

@@ -1617,16 +1617,20 @@
 length()
+ - 1
 ; i++) %7B
@@ -2173,13 +2173,12 @@
 ng(%22
-short
+full
 _nam

Fix a crasher off-by-one and use full name instead of short for personals.