package com.fsck.k9.search;

import java.util.UUID;

import android.content.Context;

import com.fsck.k9.BaseAccount;
import com.fsck.k9.R;
import com.fsck.k9.search.SearchSpecification.Attribute;
import com.fsck.k9.search.SearchSpecification.Searchfield;

/**
 * This class is basically a wrapper around a LocalSearch. It allows to expose it as
 * an account. This is a meta-account containing all the e-mail that matches the search.
 */
public class SearchAccount implements BaseAccount {
    public static final String ALL_MESSAGES = "all_messages";
    public static final String UNIFIED_INBOX = "unified_inbox";


    // create the all messages search ( all accounts is default when none specified )
    public static SearchAccount createAllMessagesAccount(Context context) {
        String name = context.getString(R.string.search_all_messages_title);

        LocalSearch tmpSearch = new LocalSearch(name);
        tmpSearch.and(Searchfield.SEARCHABLE, "1", Attribute.EQUALS);

        return new SearchAccount(ALL_MESSAGES, tmpSearch, name,
                context.getString(R.string.search_all_messages_detail));
    }


    // create the unified inbox meta account ( all accounts is default when none specified )
    public static SearchAccount createUnifiedInboxAccount(Context context) {
        String name = context.getString(R.string.integrated_inbox_title);
        LocalSearch tmpSearch = new LocalSearch(name);
        tmpSearch.and(Searchfield.INTEGRATE, "1", Attribute.EQUALS);
        return new SearchAccount(UNIFIED_INBOX, tmpSearch, name,
                context.getString(R.string.integrated_inbox_detail));
    }

    private String mId;
    private String mEmail;
    private String mDescription;
    private LocalSearch mSearch;
    private String mFakeUuid;

    public SearchAccount(String id, LocalSearch search, String description, String email)
            throws IllegalArgumentException {

        if (search == null) {
            throw new IllegalArgumentException("Provided LocalSearch was null");
        }

        mId = id;
        mSearch = search;
        mDescription = description;
        mEmail = email;
    }

    public String getId() {
        return mId;
    }

    @Override
    public synchronized String getEmail() {
        return mEmail;
    }

    @Override
    public synchronized void setEmail(String email) {
        this.mEmail = email;
    }

    @Override
    public String getDescription() {
        return mDescription;
    }

    @Override
    public void setDescription(String description) {
        this.mDescription = description;
    }

    public LocalSearch getRelatedSearch() {
        return mSearch;
    }

    /*
     * This will only be used when accessed as an Account. If that
     * is the case we don't want to return the uuid of a real account since
     * this is posing as a fake meta-account. If this object is accesed as
     * a Search then methods from LocalSearch will be called which do handle
     * things nice.
     */
    @Override
    public String getUuid() {
        if (mFakeUuid == null) {
            mFakeUuid = UUID.randomUUID().toString();
        }
        return mFakeUuid;
    }
}

@@ -26,32 +26,8 @@
 h;%0A%0A
-import java.util.UUID;%0A%0A
 impo
@@ -1729,38 +1729,8 @@
 rch;
-%0A    private String mFakeUuid;
 %0A%0A  
@@ -2627,24 +2627,25 @@
 %0A%0A    /*
+*
 %0A     * 
 This wil
@@ -2640,464 +2640,496 @@
   * 
-This will only be used when accessed as an Account. If that%0A     * is the case we don't want to return the uuid of a real account since%0A     * this is posing as a fake meta-account. If this object
+Returns the ID of this %7B@code SearchAccount%7D instance.%0A     *%0A     * %3Cp%3E%0A     * This isn't really a UUID. But since we don't expose this value to other apps and we only%0A     * use the account UUID as opaque string (e.g. as key in a %7B@code Map%7D) we're fine.%3Cbr%3E%0A     * Using a constant string
  is 
-accesed as%0A     * a Search then methods from LocalSearch will be called which do handle%0A     * things nice.%0A     */%0A    @Override%0A    public String getUuid() %7B%0A        if (mFakeUuid == null) %7B%0A            mFakeUuid = UUID.randomUUID().toString();%0A        %7D
+necessary to identify the same search account even when the%0A     * corresponding %7B@link SearchAccount%7D object has been recreated.%0A     * %3C/p%3E%0A     */%0A    @Override%0A    public String getUuid() %7B
 %0A   
@@ -3145,15 +3145,9 @@
 rn m
-FakeUui
+I
 d;%0A 

Use constant "UUID" for search accounts