package org.phphub.app.common.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v4.app.NavUtils;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.widget.TextView;

import com.github.pwittchen.prefser.library.Prefser;
import com.levelmoney.velodrome.Velodrome;

import org.phphub.app.R;
import org.phphub.app.common.App;
import org.phphub.app.common.Navigator;
import org.phphub.app.common.internal.di.component.ApiComponent;
import org.phphub.app.common.internal.di.component.AppComponent;
import org.phphub.app.common.internal.di.module.ActivityModule;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import icepick.Icepick;
import nucleus.presenter.Presenter;
import nucleus.view.NucleusAppCompatActivity;

import static org.phphub.app.common.Constant.GUEST_TOKEN_KEY;

public abstract class BaseActivity<PresenterType extends Presenter> extends NucleusAppCompatActivity<PresenterType> {
    @Nullable
    @Bind(R.id.toolbar)
    Toolbar toolbarView;

    @Nullable
    @Bind(R.id.toolbar_title)
    TextView toolbarTitleView;

    Navigator navigator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        injectorPresenter();
        super.onCreate(savedInstanceState);
        Icepick.restoreInstanceState(this, savedInstanceState);
        setContentView(getLayoutResId());
        initializeToolbar();
        navigator = getAppComponent().navigator();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Icepick.saveInstanceState(this, outState);
    }

    @Override
    protected void onPostCreate(@Nullable Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        if (!isChild()) {
            onTitleChanged(getTitleName(), getTitleColor());
        }
    }


    @Override
    protected void onTitleChanged(CharSequence title, int color) {
        super.onTitleChanged(title, color);
        if (toolbarTitleView == null) {
            return;
        }
        toolbarTitleView.setText(title);
    }

    @Override
    public void onContentChanged() {
        super.onContentChanged();
        ButterKnife.bind(this);
    }

    protected void initializeToolbar() {
        if (toolbarView == null) {
            return;
        }
        setSupportActionBar(toolbarView);
        if (toolbarTitleView != null) {
            getSupportActionBar().setDisplayShowTitleEnabled(false);
        }
        if (!TextUtils.isEmpty(NavUtils.getParentActivityName(this))) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setHomeAsUpIndicator(R.mipmap.ic_back);
        }
    }

    protected Map<String, String> getHttpHeaderAuth() {
        final Prefser prefser = new Prefser(this);
        String clientToken = prefser.get(GUEST_TOKEN_KEY, String.class, "");
        Map<String, String> map = new HashMap<String, String>();

        map.put("Authorization", "Bearer " + clientToken);

        return map;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Velodrome.handleResult(this, requestCode, resultCode, data);
    }

    protected CharSequence getTitleName() {
        return getTitle();
    }

    protected AppComponent getAppComponent() {
        return ((App) getApplication()).getAppComponent();
    }

    protected ApiComponent getApiComponent() {
        return ((App) getApplication()).getApiComponent();
    }

    protected ActivityModule getActivityModule() {
        return new ActivityModule(this);
    }

    protected void injectorPresenter() {}

    abstract protected @LayoutRes int getLayoutResId();
}
@@ -26,24 +26,97 @@
 mmon.base;%0A%0A
+import android.accounts.Account;%0Aimport android.accounts.AccountManager;%0A
 import andro
@@ -892,16 +892,64 @@
 rKnife;%0A
+import eu.unicate.retroauth.AuthAccountManager;%0A
 import i
@@ -1045,16 +1045,122 @@
 ctivity;
+%0Aimport rx.Observable;%0Aimport rx.Subscriber;%0Aimport rx.functions.Action1;%0Aimport rx.schedulers.Schedulers;
 %0A%0Aimport
@@ -3182,192 +3182,1361 @@
     
-final Prefser prefser = new Prefser(this);%0A        String clientToken = prefser.get(GUEST_TOKEN_KEY, String.class, %22%22);%0A        Map%3CString, String%3E map = new HashMap%3CString, String%3E();
+Map%3CString, String%3E map = new HashMap%3C%3E();%0A        final String%5B%5D token = %7B%22%22%7D;%0A%0A        AccountManager accountManager = AccountManager.get(this);%0A        Account%5B%5D accounts = accountManager.getAccountsByType(getString(R.string.auth_account_type));%0A        final AuthAccountManager authAccountManager = new AuthAccountManager(this);%0A        if (accounts.length %3E 0) %7B%0A            final Account account = accounts%5B0%5D;%0A            final String accountType = getString(R.string.auth_account_type),%0A                    tokenType = getString(R.string.auth_token_type);%0A%0A            Observable.create(new Observable.OnSubscribe%3CString%3E() %7B%0A                @Override%0A                public void call(Subscriber%3C? super String%3E subscriber) %7B%0A                    subscriber.onNext(authAccountManager.getAuthToken(account, accountType, tokenType));%0A                    subscriber.onCompleted();%0A                %7D%0A            %7D)%0A            .subscribeOn(Schedulers.io())%0A            .toBlocking()%0A            .forEach(new Action1%3CString%3E() %7B%0A                @Override%0A                public void call(String s) %7B%0A                    token%5B0%5D = s;%0A                %7D%0A            %7D);%0A        %7D%0A%0A        if (TextUtils.isEmpty(token%5B0%5D)) %7B%0A            Prefser prefser = new Prefser(this);%0A            token%5B0%5D = prefser.get(GUEST_TOKEN_KEY, String.class, %22%22);%0A        %7D
 %0A%0A  
@@ -4582,19 +4582,16 @@
 %22 + 
-clientT
+t
 oken
+%5B0%5D
 );%0A%0A

change BaseActiviyt:getHttpHeaderAuth()