package app.android.gambit.ui;


import java.util.ArrayList;
import java.util.List;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import app.android.gambit.R;


public class AsyncAccountSelector
{
	public static enum Result {
		SUCCESS, NO_ACCOUNTS_REGISTERED
	}

	public static interface AccountWaiter
	{
		void onAccountObtained(Result result, Account account);
	}

	private static final String ACCOUNT_NAME = "google_account";
	private static final String ACCOUNT_TYPE = "com.google";

	private final Activity activity;
	private final AccountWaiter accountWaiter;

	public AsyncAccountSelector(Activity selectDialogParent, AccountWaiter accountWaiter) {
		this.activity = selectDialogParent;
		this.accountWaiter = accountWaiter;
	}

	public void selectAccount() {
		if (haveAccountNameInPreferences()) {
			notifyAccountWaiter(loadAccountNameFromPreferences());
		}
		else {
			selectAccountFromDialog();
		}
	}

	private boolean haveAccountNameInPreferences() {
		String accountName = loadAccountNameFromPreferences();
		return !accountName.isEmpty();
	}

	private String loadAccountNameFromPreferences() {
		String accountName = getPreference(ACCOUNT_NAME);

		if (isAccountRegistered(accountName)) {
			return accountName;
		}
		else {
			return new String();
		}
	}

	private String getPreference(String key) {
		SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity
			.getApplicationContext());

		return preferences.getString(key, new String());
	}

	private boolean isAccountRegistered(String accountName) {
		return getRegisteredAccountsNames().contains(accountName);
	}

	private List<String> getRegisteredAccountsNames() {
		List<String> registeredAccountsNames = new ArrayList<String>();

		AccountManager accountManager = AccountManager.get(activity.getApplicationContext());

		for (Account account : accountManager.getAccountsByType(ACCOUNT_TYPE)) {
			registeredAccountsNames.add(account.name);
		}

		return registeredAccountsNames;
	}

	private void notifyAccountWaiter(String accountName) {
		Account account = buildAccountFromName(accountName);

		Result result;
		if (account == null) {
			result = Result.NO_ACCOUNTS_REGISTERED;
		}
		else {
			result = Result.SUCCESS;
		}

		accountWaiter.onAccountObtained(result, account);
	}

	private Account buildAccountFromName(String accountName) {
		AccountManager accountManager = AccountManager.get(activity.getApplicationContext());

		for (Account account : accountManager.getAccountsByType(ACCOUNT_TYPE)) {
			if (account.name.equals(accountName)) {
				return account;
			}
		}

		return null;
	}

	private void selectAccountFromDialog() {
		if (haveSomeAccountRegistered()) {
			constructAccountsListDialog().show();
		}
		else {
			notifyAccountWaiter(new String());
		}
	}

	private boolean haveSomeAccountRegistered() {
		return !getRegisteredAccountsNames().isEmpty();
	}

	private Dialog constructAccountsListDialog() {
		AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(activity);
		dialogBuilder.setTitle(activity.getString(R.string.title_choose_google_account));

		final List<String> accountsNamesList = getRegisteredAccountsNames();
		String[] accountsNamesArray = new String[accountsNamesList.size()];
		accountsNamesList.toArray(accountsNamesArray);

		dialogBuilder.setItems(accountsNamesArray, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int selectedAccountPosition) {
				String selectedAccountName = accountsNamesList.get(selectedAccountPosition);

				setPreference(ACCOUNT_NAME, selectedAccountName);

				notifyAccountWaiter(selectedAccountName);
			}
		});

		return dialogBuilder.create();
	}

	private void setPreference(String key, String value) {
		SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity
			.getApplicationContext());
		SharedPreferences.Editor preferencesEditor = preferences.edit();

		preferencesEditor.putString(key, value);

		preferencesEditor.commit();
	}
}

@@ -459,17 +459,18 @@
 m Result
- 
+%0A%09
 %7B%0A%09%09SUCC
@@ -1103,24 +1103,124 @@
 ces());%0A%09%09%7D%0A
+%09%09else if (isSingleAccountRegistered()) %7B%0A%09%09%09notifyAccountWaiter(getSingleRegisteredAccount());%0A%09%09%7D%0A
 %09%09else %7B%0A%09%09%09
@@ -1732,32 +1732,36 @@
 Preferences(
+%0A%09%09%09
 activity
 %0A%09%09%09.getAppl
@@ -1740,36 +1740,32 @@
 ces(%0A%09%09%09activity
-%0A%09%09%09
 .getApplicationC
@@ -2939,24 +2939,391 @@
 n null;%0A%09%7D%0A%0A
+%09private boolean isSingleAccountRegistered() %7B%0A%09%09final int ACCOUNTS_COUNT_FOR_SINGLE_ACCOUNT = 1;%0A%0A%09%09return getRegisteredAccountsNames().size() == ACCOUNTS_COUNT_FOR_SINGLE_ACCOUNT;%0A%09%7D%0A%0A%09private String getSingleRegisteredAccount() %7B%0A%09%09final int ACCOUNTS_INDEX_FOR_SINGLE_ACCOUNT = 0;%0A%0A%09%09return getRegisteredAccountsNames().get(ACCOUNTS_INDEX_FOR_SINGLE_ACCOUNT);%0A%09%7D%0A%0A
 %09private voi
@@ -4069,17 +4069,19 @@
 stener()
- 
+%0A%09%09
 %7B%0A%09%09%09@Ov
@@ -4533,24 +4533,28 @@
 erences(
+%0A%09%09%09
 activity
 %0A%09%09%09.get
@@ -4545,20 +4545,16 @@
 activity
-%0A%09%09%09
 .getAppl

Fix issue with single registered account.