package apz.pirichat.shared;
import java.util.Vector;
import java.io.Serializable;

public class ChatMessage implements Serializable
{
    private String mSource;
    private String mDestination;
    private String mMessage;
    private Vector<MessageAttribute> mMessageAttribute;

    public ChatMessage()
    {
        // Default constructor for re-serialisation
    }

    public ChatMessage(String pSource, String pDestination, String pMessage) throws PiriException
    {
        // Constructor
        // Parameter validation...
        if (pSource == null)
            throw new PiriNullPointerException("pSource");
        if (pDestination == null)
            throw new PiriNullPointerException("pDestination");
        if (pMessage == null)
            throw new PiriNullPointerException("pMessage");
        if (pSource.length() == 0)
            throw new PiriZeroLengthException("pSource");
        if (pDestination.length() == 0)
            throw new PiriZeroLengthException("pDestination");
        if (pMessage.length() == 0)
            throw new PiriZeroLengthException("pMessage");
        mSource = pSource;
        mDestination = pDestination;
        mMessage = pMessage;
        mMessageAttribute = new Vector<MessageAttribute>();
    }

    public String getSource()
    {
        return mSource;
    }

    public String getDestination()
    {
        return mDestination;
    }

    public String getMessage()
    {
        return mMessage;
    }

    public boolean doesAttributeExist(String pName) throws PiriException
    {
        return (lookupAttributeIndex(pName) != (-1));
    }

    public MessageAttribute[] getAttributes()
    {
        MessageAttribute[] tempArray = new MessageAttribute[mMessageAttribute.size()];
        for(int i=0; i<mMessageAttribute.size(); i++)
            tempArray[i] = (MessageAttribute)mMessageAttribute.elementAt(i);
        return tempArray;
    }

    public String getAttribute(String pName) throws PiriException
    {
        if (pName == null)
            throw new PiriNullPointerException("pName");
        int index = lookupAttributeIndex(pName);
        if (index == (-1))
            throw new PiriAttributeNotFoundException(pName);
        return ((MessageAttribute)mMessageAttribute.elementAt(index)).getValue();
    }

    public void setAttribute(String pName, String pValue) throws PiriException
    {
        if (pName == null)
            throw new PiriNullPointerException("pName");
        if (pValue == null)
            throw new PiriNullPointerException("pValue");
        if (pName.length() == 0)
            throw new PiriZeroLengthException("pName");
        // If attribute with that name already exists, overwrite
        // Otherwise, create a new attribute
        int index = lookupAttributeIndex(pName);
        if (index == (-1))
        {
            mMessageAttribute.add(new MessageAttribute(pName, pValue));
        }
        else
        {
            ((MessageAttribute)mMessageAttribute.elementAt(index)).setValue(pValue);
        }
    }

    private int lookupAttributeIndex(String pName)
    {
        for(int i=0; i<mMessageAttribute.size(); i++)
            if (((MessageAttribute)mMessageAttribute.elementAt(i)).getName().equals(pName))
                return i;
        return (-1);
    }

    public class MessageAttribute implements Serializable
    {
        private String mName;
        private String mValue;

        public MessageAttribute()
        {
            // Our default constructer
        }

        public MessageAttribute(String pName, String pValue) throws PiriException
        {
            if (pName == null)
                throw new PiriNullPointerException("pName");
            if (pValue == null)
                throw new PiriNullPointerException("pValue");
            if (pName.length() == 0)
                throw new PiriZeroLengthException("pName");
            mName = pName;
            mValue = pValue;
        }

        public String getName()
        {
            return mName;
        }

        public String getValue()
        {
            return mValue;
        }

        public void setValue(String pValue) throws PiriException
        {
            if (pValue == null)
                throw new PiriNullPointerException("pValue");
            mValue = pValue;
        }
    }
}
