/*
 * Copyright (C) 2012 Brian Muramatsu
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.btmura.android.reddit.accounts;

import android.accounts.AbstractAccountAuthenticator;
import android.accounts.Account;
import android.accounts.AccountAuthenticatorResponse;
import android.accounts.AccountManager;
import android.accounts.NetworkErrorException;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.btmura.android.reddit.BuildConfig;
import com.btmura.android.reddit.R;

public class AccountAuthenticator extends AbstractAccountAuthenticator {

    public static final String TAG = "AccountAuthenticator";

    public static final String AUTH_TOKEN_COOKIE = "cookie";
    public static final String AUTH_TOKEN_MODHASH = "modhash";

    private final Context context;

    public static String getAccountType(Context context) {
        return context.getString(R.string.account_type);
    }

    public AccountAuthenticator(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public Bundle addAccount(AccountAuthenticatorResponse response, String accountType,
            String authTokenType, String[] requiredFeatures, Bundle options)
            throws NetworkErrorException {
        if (BuildConfig.DEBUG) {
            Log.d(TAG, "addAccount");
        }

        Intent intent = new Intent(context, AccountAuthenticatorActivity.class);
        intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);

        Bundle result = new Bundle(1);
        result.putParcelable(AccountManager.KEY_INTENT, intent);
        return result;
    }

    @Override
    public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account,
            Bundle options) throws NetworkErrorException {
        if (BuildConfig.DEBUG) {
            Log.d(TAG, "confirmCredentials");
        }
        return null;
    }

    @Override
    public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) {
        if (BuildConfig.DEBUG) {
            Log.d(TAG, "editProperties");
        }
        return null;
    }

    @Override
    public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account,
            String authTokenType, Bundle options) throws NetworkErrorException {
        if (BuildConfig.DEBUG) {
            Log.d(TAG, "getAuthToken");
        }

        Intent intent = new Intent(context, AccountAuthenticatorActivity.class);
        intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
        intent.putExtra(com.btmura.android.reddit.accounts.AccountAuthenticatorActivity.EXTRA_LOGIN,
                account.name);

        Bundle result = new Bundle(1);
        result.putParcelable(AccountManager.KEY_INTENT, intent);
        return result;
    }

    @Override
    public String getAuthTokenLabel(String authTokenType) {
        if (BuildConfig.DEBUG) {
            Log.d(TAG, "getAuthTokenLabel");
        }
        return null;
    }

    @Override
    public Bundle hasFeatures(AccountAuthenticatorResponse response, Account account,
            String[] features) throws NetworkErrorException {
        if (BuildConfig.DEBUG) {
            Log.d(TAG, "hasFeatures");
        }
        return null;
    }

    @Override
    public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account,
            String authTokenType, Bundle options) throws NetworkErrorException {
        if (BuildConfig.DEBUG) {
            Log.d(TAG, "updateCredentials");
        }
        return null;
    }
}

@@ -960,16 +960,47 @@
 Bundle;%0A
+import android.text.TextUtils;%0A
 import a
@@ -1016,16 +1016,16 @@
 il.Log;%0A
-
 %0Aimport 
@@ -2502,36 +2502,74 @@
 %0A        return 
-null
+createIntentResult(response, account.name)
 ;%0A    %7D%0A%0A    @Ov
@@ -2760,37 +2760,67 @@
 %7D%0A        return
+ createIntentResult(response,
  null
+)
 ;%0A    %7D%0A%0A    @Ov
@@ -3073,33 +3073,32 @@
 ;%0A        %7D%0A
-%0A
         
 Intent inten
@@ -3089,425 +3089,57 @@
     
-Intent intent = new Intent(context, AccountAuthenticatorActivity.class);%0A        intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);%0A        intent.putExtra(com.btmura.android.reddit.accounts.AccountAuthenticatorActivity.EXTRA_LOGIN,%0A                account.name);%0A%0A        Bundle result = new Bundle(1);%0A        result.putParcelable(AccountManager.KEY_INTENT, intent);%0A        return result
+return createIntentResult(response, account.name)
 ;%0A  
@@ -3317,32 +3317,72 @@
     return null;
+ // OK to return null according to docs.
 %0A    %7D%0A%0A    @Ove
@@ -3613,32 +3613,33 @@
 ;%0A        %7D%0A
+%0A
         
 return null;
@@ -3622,35 +3622,144 @@
  %7D%0A%0A        
-return null
+Bundle result = new Bundle(1);%0A        result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, true);%0A        return result
 ;%0A    %7D%0A%0A   
@@ -4045,20 +4045,638 @@
  return 
-null
+createIntentResult(response, account.name);%0A    %7D%0A%0A    private Bundle createIntentResult(AccountAuthenticatorResponse response, String login) %7B%0A        Intent intent = new Intent(context, AccountAuthenticatorActivity.class);%0A        intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);%0A        if (!TextUtils.isEmpty(login)) %7B%0A            intent.putExtra(com.btmura.android.reddit.accounts.AccountAuthenticatorActivity.%0A                    EXTRA_LOGIN, login);%0A        %7D%0A%0A        Bundle result = new Bundle(1);%0A        result.putParcelable(AccountManager.KEY_INTENT, intent);%0A        return result
 ;%0A    %7D%0A

Implement other AccountAuthenticator methods