package ng.kingsley.android.app;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.TypedArray;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.DisplayMetrics;
import android.view.MenuItem;
import android.view.View;

import ng.kingsley.android.appcommons.R;
import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper;

/**
 * @author ADIO Kingsley O.
 * @since 05 Jun, 2015
 */
public class BaseActivity extends AppCompatActivity {

    protected Toolbar mToolbar;
    protected DrawerLayout mDrawer;
    private ActionBarDrawerToggle mToggle;

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);

        trySetupToolbar();
        trySetupDrawer();
    }

    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
    }

    protected void trySetupToolbar() {
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        if (mToolbar != null) {
            setSupportActionBar(mToolbar);
        }
    }

    protected boolean trySetupDrawer() {
        mDrawer = (DrawerLayout) findViewById(R.id.drawer);
        if (mDrawer != null) {
            View side = mDrawer.findViewById(R.id.navigation_view);
            int possibleMinDrawerWidth = getScreenSize(this)[0] - attrToDimenPx(this, R.attr.actionBarSize);
            int maxDrawerWidth = getResources().getDimensionPixelSize(R.dimen.navigation_drawer_max_width);
            side.getLayoutParams().width = Math.min(possibleMinDrawerWidth, maxDrawerWidth);

            mToggle = new ActionBarDrawerToggle(this,
              mDrawer, R.string.app_name, R.string.app_name);
            mToggle.setDrawerIndicatorEnabled(true);
            mDrawer.addDrawerListener(mToggle);
            mDrawer.post(new Runnable() {
                @Override
                public void run() {
                    mToggle.syncState();
                    if (getSupportActionBar() != null) {
                        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                    }
                }
            });
            return true;
        }
        return false;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (mToggle != null && mToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        // Force checking the native fragment manager for a backstack rather than
        // the support lib fragment manager.
        if (mDrawer != null && mDrawer.isDrawerOpen(GravityCompat.START)) {
            mDrawer.closeDrawer(GravityCompat.START);
        } else if (!getFragmentManager().popBackStackImmediate()) {
            super.onBackPressed();
        }
    }

    protected void presentFragment(Fragment fragment, boolean addToBackstack, boolean detachExisting) {
        FragmentTransaction transaction = getFragmentManager()
          .beginTransaction()
          .add(R.id.container, fragment);
        if (detachExisting) {
            transaction.detach(getFragmentManager().findFragmentById(R.id.container));
        }
        if (addToBackstack) {
            transaction.addToBackStack(null);
        }
        transaction.commit();
    }

    protected void presentFragment(android.support.v4.app.Fragment fragment,
      boolean addToBackstack, boolean detachExisting) {
        android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager()
          .beginTransaction()
          .add(R.id.container, fragment);
        if (detachExisting) {
            transaction.detach(getSupportFragmentManager().findFragmentById(R.id.container));
        }
        if (addToBackstack) {
            transaction.addToBackStack(null);
        }
        transaction.commit();
    }

    protected boolean hasPermission(String permission) {
        return ContextCompat.checkSelfPermission(this, permission)
          == PackageManager.PERMISSION_GRANTED;
    }

    private static int attrToDimenPx(Context context, int attr) {
        TypedArray a = null;
        try {
            a = context.getTheme().obtainStyledAttributes(new int[]{attr});
            return a.getDimensionPixelSize(0, 0);
        } finally {
            if (a != null) {
                a.recycle();
            }
        }
    }

    private static int[] getScreenSize(Context context) {
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        return new int[]{metrics.widthPixels, metrics.heightPixels};
    }

    /**
     * Get the Main Application component for dependency injection.
     */
    protected <C> C getAppComponent(Class<C> componentType) {
        return componentType.cast(((BaseApplication) getApplication()).getComponent());
    }
}

@@ -209,16 +209,61 @@
 dArray;%0A
+import android.support.annotation.StringRes;%0A
 import a
@@ -295,32 +295,32 @@
 .ContextCompat;%0A
-
 import android.s
@@ -2035,16 +2035,22 @@
 e(this,%0A
+      
         
@@ -2099,24 +2099,24 @@
 .app_name);%0A
-
             
@@ -3256,32 +3256,521 @@
        %7D%0A    %7D%0A%0A
+    public CharSequence getSupportTitle() %7B%0A        if (getSupportActionBar() == null) return null;%0A        return getSupportActionBar().getTitle();%0A    %7D%0A%0A    public void setSupportTitle(CharSequence title) %7B%0A        if (getSupportActionBar() != null) %7B%0A            getSupportActionBar().setTitle(title);%0A        %7D%0A    %7D%0A%0A    public void setSupportTitle(@StringRes int titleRes) %7B%0A        String title = (titleRes %3E 0) ? getString(titleRes) : null;%0A        setSupportTitle(title);%0A    %7D%0A%0A
     protected vo
@@ -3922,32 +3922,38 @@
 ger()%0A          
+      
 .beginTransactio
@@ -3948,32 +3948,38 @@
 inTransaction()%0A
+      
           .add(R
@@ -4331,16 +4331,22 @@
 agment,%0A
+      
       bo
@@ -4385,32 +4385,32 @@
 tachExisting) %7B%0A
-
         android.
@@ -4488,32 +4488,38 @@
 ger()%0A          
+      
 .beginTransactio
@@ -4514,32 +4514,38 @@
 inTransaction()%0A
+      
           .add(R
@@ -4951,16 +4951,22 @@
 ission)%0A
+      
         

Add methods to directly interact with the support action bar title