package com.fsck.k9.activity;

import android.util.Log;
import com.fsck.k9.K9;
import com.fsck.k9.helper.Utility;
import com.fsck.k9.mail.Flag;
import com.fsck.k9.mail.MessagingException;

import java.io.Serializable;
import java.util.StringTokenizer;

public class MessageReference implements Serializable
{
    private static final long serialVersionUID = -1625198750239083389L;
    public String accountUuid;
    public String folderName;
    public String uid;
    public Flag flag = null;

    /**
     * Initialize an empty MessageReference.
     */
    public MessageReference()
    {
    }

    // Version identifier for use when serializing. This will allow us to introduce future versions
    // if we have to rev MessageReference.
    private static final String IDENTITY_VERSION_1 = "!";
    private static final String IDENTITY_SEPARATOR = ":";

    /**
     * Initialize a MessageReference from a seraialized identity.
     * @param identity Serialized identity.
     * @throws MessagingException On missing or corrupted identity.
     */
    public MessageReference(final String identity) throws MessagingException
    {
        // Can't be null and must be at least length one so we can check the version.
        if (identity == null || identity.length() < 1)
        {
            throw new MessagingException("Null or truncated MessageReference identity.");
        }

        // Version check.
        if (identity.charAt(0) == IDENTITY_VERSION_1.charAt(0))
        {
            // Split the identity, stripping away the first two characters representing the version and delimiter.
            StringTokenizer tokens = new StringTokenizer(identity.substring(2), IDENTITY_SEPARATOR, false);
            if (tokens.countTokens() == 4)
            {
                accountUuid = Utility.base64Decode(tokens.nextToken());
                folderName = Utility.base64Decode(tokens.nextToken());
                uid = Utility.base64Decode(tokens.nextToken());
                flag = Flag.valueOf(tokens.nextToken());

                if (K9.DEBUG)
                    Log.d(K9.LOG_TAG, "Thawed " + toString());
            }
            else
            {
                throw new MessagingException("Invalid token in " + IDENTITY_SEPARATOR + " identity.");
            }
        }
    }

    /**
     * Serialize this MessageReference for storing in a K9 identity.  This is a colon-delimited base64 string.
     *
     * @return Serialized string.
     */
    public String toIdentityString()
    {
        StringBuilder refString = new StringBuilder();

        refString.append(IDENTITY_VERSION_1);
        refString.append(IDENTITY_SEPARATOR);
        refString.append(Utility.base64Encode(accountUuid));
        refString.append(IDENTITY_SEPARATOR);
        refString.append(Utility.base64Encode(folderName));
        refString.append(IDENTITY_SEPARATOR);
        refString.append(Utility.base64Encode(uid));
        refString.append(IDENTITY_SEPARATOR);
        refString.append(flag.name());

        return refString.toString();
    }

    @Override
    public boolean equals(Object o)
    {
        if (o instanceof MessageReference == false)
        {
            return false;
        }
        MessageReference other = (MessageReference)o;
        if ((accountUuid == other.accountUuid || (accountUuid != null && accountUuid.equals(other.accountUuid)))
                && (folderName == other.folderName || (folderName != null && folderName.equals(other.folderName)))
                && (uid == other.uid || (uid != null && uid.equals(other.uid))))
        {
            return true;
        }
        return false;
    }

    @Override
    public int hashCode()
    {
        final int MULTIPLIER = 31;

        int result = 1;
        result = MULTIPLIER * result + ((accountUuid == null) ? 0 : accountUuid.hashCode());
        result = MULTIPLIER * result + ((folderName == null) ? 0 : folderName.hashCode());
        result = MULTIPLIER * result + ((uid == null) ? 0 : uid.hashCode());
        return result;
    }

    @Override
    public String toString()
    {
        return "MessageReference{" +
                "accountUuid='" + accountUuid + '\'' +
                ", folderName='" + folderName + '\'' +
                ", uid='" + uid + '\'' +
                ", flag=" + flag +
                '}';
    }
}

@@ -1745,12 +1745,12 @@
 s() 
-== 4
+%3E= 3
 )%0A  
@@ -1972,64 +1972,469 @@
 ));%0A
-                flag = Flag.valueOf(tokens.nextToken());
+%0A                if (tokens.hasMoreTokens())%0A                %7B%0A                    final String flagString = tokens.nextToken();%0A                    try%0A                    %7B%0A                        flag = Flag.valueOf(flagString);%0A                    %7D catch (IllegalArgumentException ie)%0A                    %7B%0A                        throw new MessagingException(%22Could not thaw message flag '%22 + flagString + %22'%22, ie);%0A                    %7D%0A                %7D
 %0A%0A  
@@ -2627,39 +2627,40 @@
 lid 
-token in %22 + IDENTITY_SEPARATOR
+MessageReference in %22 + identity
  + %22
@@ -3325,24 +3325,64 @@
 code(uid));%0A
+        if (flag != null)%0A        %7B%0A    
         refS
@@ -3415,32 +3415,36 @@
 RATOR);%0A        
+    
 refString.append
@@ -3453,24 +3453,34 @@
 lag.name());
+%0A        %7D
 %0A%0A        re

Fix for NPE introduced in r3258.