/**
 * 
 */
package com.beem.project.beem.service;

import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

/**
 * This class represents a instant message.
 * @author darisk
 */
public class Message implements Parcelable {

    /**
     * Normal message type.
     * Theese messages are like an email, with subject.
     */
    public static  final int MSG_TYPE_NORMAL = 100;

    /**
     * Chat message type.
     */
    public static  final int MSG_TYPE_CHAT = 200;

    /**
     * Group chat message type.
     */
    public static  final int MSG_TYPE_GROUP_CHAT = 300;

    private int mType;
    private String mBody;
    private String mSubject;
    private String mTo;
    private String mThread;

    /**
     * Parcelable.Creator needs by Android.
     */
    public static final Parcelable.Creator<Message> CREATOR = new Parcelable.Creator<Message>() {

	@Override
	public Message createFromParcel(Parcel source) {
	    return new Message(source);
	}

	@Override
	public Message[] newArray(int size) {
	    return new Message[size];
	}
    };

    /**
     * Constructor.
     * @param to	the destinataire of the message
     * @param type	the message type
     */
    public Message(final String to, final int type) {
	mTo = to;
	mType = type;
	mBody = "";
	mSubject = "";
	mThread = "";
    }

    /**
     * Constructor a message of type chat.
     * @param to	the destinataire of the message
     */
    public Message(final String to) {
	this(to, MSG_TYPE_CHAT);
    }

    public Message(org.jivesoftware.smack.packet.Message smackMsg) {
	this(smackMsg.getTo());
	switch (smackMsg.getType()) {
	    case chat:
		mType = MSG_TYPE_CHAT;
		break;
	    case groupchat:
		mType = MSG_TYPE_GROUP_CHAT;
		break;
	    case normal:
		mType = MSG_TYPE_NORMAL;
		break;
	    default:
		Log.w("BEEM_MESSAGE", "type de message non gerer" + smackMsg.getType());
		break;
	}
	mBody = smackMsg.getBody();
	mSubject = smackMsg.getSubject();
	mThread = smackMsg.getThread();
    }
    
    /**
     * Construct a message from a parcel.
     * @param in parcel to use for construction
     */
    private Message(final Parcel in) {
	mType = in.readInt();
	mTo = in.readString();
	mBody = in.readString();
	mSubject = in.readString();
	mThread = in.readString();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void writeToParcel(Parcel dest, int flags) {
	// TODO Auto-generated method stub
	dest.writeInt(mType);
	dest.writeString(mTo);
	dest.writeString(mBody);
	dest.writeString(mSubject);
	dest.writeString(mThread);
    }

    /**
     * Get the type of the message.
     * @return the type of the message.
     */
    public int getType() {
        return mType;
    }

    /**
     * Set the type of the message.
     * @param type the type to set
     */
    public void setType(int type) {
        mType = type;
    }


    /**
     * Get the body of the message.
     * @return the Body of the message
     */
    public String getBody() {
        return mBody;
    }


    /**
     * Set the body of the message.
     * @param body the body to set
     */
    public void setBody(String body) {
        mBody = body;
    }


    /**
     * Get the subject of the message.
     * @return the subject
     */
    public String getSubject() {
        return mSubject;
    }


    /**
     * Set the subject of the message.
     * @param subject the subject to set
     */
    public void setSubject(String subject) {
        mSubject = subject;
    }


    /**
     * Get the destinataire of the message.
     * @return the destinataire of the message
     */
    public String getTo() {
        return mTo;
    }


    /**
     * Set the destinataire of the message.
     * @param to the destinataire to set
     */
    public void setTo(String to) {
        mTo = to;
    }


    /**
     * Get the thread of the message.
     * @return the thread
     */
    public String getThread() {
        return mThread;
    }


    /**
     * Set the thread of the message.
     * @param thread the thread to set
     */
    public void setThread(String thread) {
        mThread = thread;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int describeContents() {
	// TODO Auto-generated method stub
	return 0;
    }

}
