package com.rthoni.camotion.business;

import android.accounts.AbstractAccountAuthenticator;
import android.accounts.Account;
import android.accounts.AccountAuthenticatorResponse;
import android.accounts.AccountManager;
import android.accounts.NetworkErrorException;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;

import com.rthoni.camotion.ui.AddAccountActivity;

/**
 *
 * Created by robin on 11/28/15.
 */
public class CamotionAccountBusiness extends Service {

    /**
     * The implementation of the class |AccountAuthenticatorImpl|.
     * It is implemented as a singleton
     */
    private static AccountAuthenticatorImpl sAccountAuthenticator = null;

    /**
     * The main constructor.
     */
    public CamotionAccountBusiness() {
        super();
    }

    /**
     * The bind method of the service.
     * @param intent The intent used to invoke the service
     * @return The binder of the class which has implemented |AbstractAccountAuthenticator|
     */
    @Override
    public IBinder onBind(Intent intent) {
        IBinder ret = null;
        if (intent.getAction().equals(android.accounts.AccountManager.ACTION_AUTHENTICATOR_INTENT)) {
            ret = getAuthenticator().getIBinder();
        }
        return ret;
    }

    /**
     * The method used to obtain the authenticator. It is implemented as a singleton
     * @return The implementation of the class |AbstractAccountAuthenticator|
     */
    private AccountAuthenticatorImpl getAuthenticator() {
        if (CamotionAccountBusiness.sAccountAuthenticator == null) {
            CamotionAccountBusiness.sAccountAuthenticator = new AccountAuthenticatorImpl(this);
        }

        return CamotionAccountBusiness.sAccountAuthenticator;
    }

    /**
     * The class which implements the class |AbstractAccountAuthenticator|.
     * It is the one which the Android system calls to perform any action related with the account
     */
    private static class AccountAuthenticatorImpl extends AbstractAccountAuthenticator {

        /**
         * The Context used.
         */
        private final Context mContext;

        /**
         * The main constructor of the class.
         * @param context The context used
         */
        public AccountAuthenticatorImpl(Context context) {
            super(context);
            mContext = context;
        }

        @Override
        public Bundle addAccount(AccountAuthenticatorResponse response,
                                 String accountType,
                                 String authTokenType,
                                 String[] requiredFeatures,
                                 Bundle options) throws NetworkErrorException {
            Bundle reply = new Bundle();

            Intent i = new Intent(mContext, AddAccountActivity.class);
            i.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
            i.putExtra("AuthTokenType", authTokenType);
            reply.putParcelable(AccountManager.KEY_INTENT, i);

            return reply;
        }

        @Override
        public Bundle confirmCredentials(AccountAuthenticatorResponse arg0, Account arg1,
                                         Bundle arg2) throws NetworkErrorException {
            return null;
        }

        @Override
        public Bundle editProperties(AccountAuthenticatorResponse arg0, String arg1) {
            return null;
        }

        @Override
        public Bundle getAuthToken(AccountAuthenticatorResponse arg0, Account arg1,
                                   String arg2, Bundle arg3) throws NetworkErrorException {
            return null;
        }

        @Override
        public String getAuthTokenLabel(String arg0) {
            return null;
        }

        @Override
        public Bundle hasFeatures(AccountAuthenticatorResponse arg0, Account arg1,
                                  String[] arg2) throws NetworkErrorException {
            return null;
        }

        @Override
        public Bundle updateCredentials(AccountAuthenticatorResponse arg0, Account arg1,
                                        String arg2, Bundle arg3) throws NetworkErrorException {
            return null;
        }
    }
}
