package doit.study.droid.activities;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.NavigationView;
import android.support.v4.app.TaskStackBuilder;
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.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import doit.study.droid.R;
import timber.log.Timber;

public class DrawerBaseActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {
    private static final boolean DEBUG = true;
    private DrawerLayout mDrawer;
    protected FrameLayout mContainerContent;
    private static String sVersion;
    private ActionBarDrawerToggle mActionBarDrawerToggle;
    private NavigationView mNavigationView;
    protected int mSelectionId = R.id.nav_set_topic;

    private String getVersion() {
        if (sVersion != null)
            return sVersion;

        try {
            sVersion = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            sVersion = "buggy";
        }
        return sVersion;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        if (DEBUG) Timber.d("onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base_drawer);

        mContainerContent = (FrameLayout) findViewById(R.id.container_content);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        mActionBarDrawerToggle = new ActionBarDrawerToggle(this, mDrawer, toolbar, R.string.drawer_open, R.string.drawer_close);
        mDrawer.addDrawerListener(mActionBarDrawerToggle);
        mActionBarDrawerToggle.syncState();

        mNavigationView = (NavigationView) findViewById(R.id.navigation_view);
        setNavTitle();
    }

    private void setNavTitle(){
        View header = mNavigationView.getHeaderView(0);
        mNavigationView.setNavigationItemSelectedListener(this);
        TextView tv = (TextView) header.findViewById(R.id.version_num_header);
        tv.setText(getVersion());
    }


    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        if (DEBUG) Timber.d("onPostCreate");
        super.onPostCreate(savedInstanceState);
       // Sync the toggle state after onRestoreInstanceState has occurred.
        mActionBarDrawerToggle.syncState();
    }

    @Override
    protected void onResume() {
        if (DEBUG) Timber.d("onResume %d", mSelectionId);
        super.onResume();
        mNavigationView.setCheckedItem(mSelectionId);
    }

    @Override
    public void onBackPressed() {
        if (DEBUG) Timber.d("onBackPressed");
        if (isNavDrawerOpen()) {
            closeNavDrawer();
        } else {
            super.onBackPressed();
        }
    }

    protected boolean isNavDrawerOpen() {
        return mDrawer != null && mDrawer.isDrawerOpen(GravityCompat.START);
    }

    protected void closeNavDrawer() {
        if (mDrawer != null) {
            mDrawer.closeDrawer(GravityCompat.START);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (DEBUG) Timber.d("onOptionsItemSelected");
        // The action bar home/up action should open or close the drawer.
        if (mActionBarDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        if (DEBUG) Timber.d("onConfigurationChanged");
        super.onConfigurationChanged(newConfig);
        // Pass any configuration change to the drawer toggles
        mActionBarDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onNavigationItemSelected(MenuItem menuItem) {
        Timber.d("selected: %d", menuItem.getItemId());
        switch(menuItem.getItemId()) {
            case R.id.nav_get_motivation:
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(getString(R.string.url_motivational_video)));
                startActivity(intent);
                break;
            case R.id.nav_set_topic:
                createBackStack(new Intent(this, TopicsChooserActivity.class));
                break;
            case R.id.nav_do_it:
                createBackStack(new Intent(this, InterrogatorActivity.class));
                break;
            default:
                break;
        }
        mDrawer.closeDrawer(GravityCompat.START);

        return true;
    }


    /**
     * Enables back navigation for activities that are launched from the NavBar. See
     * {@code AndroidManifest.xml} to find out the parent activity names for each activity.
     * @param intent
     */
    private void createBackStack(Intent intent) {
        TaskStackBuilder builder = TaskStackBuilder.create(this);
            builder.addNextIntentWithParentStack(intent);
            builder.startActivities();
    }
}

@@ -31,16 +31,77 @@
 ities;%0A%0A
+import android.app.Activity;%0Aimport android.content.Context;%0A
 import a
@@ -123,16 +123,16 @@
 Intent;%0A
-
 import a
@@ -691,24 +691,76 @@
 .view.View;%0A
+import android.view.inputmethod.InputMethodManager;%0A
 import andro
@@ -2221,26 +2221,74 @@
 his,
- mDrawer, toolbar,
+%0A                mDrawer,%0A                toolbar,%0A               
  R.s
@@ -2305,16 +2305,32 @@
 er_open,
+%0A               
  R.strin
@@ -2344,17 +2344,451 @@
 r_close)
-;
+ %7B%0A            // hide keyboard%0A            @Override%0A            public void onDrawerOpened(View drawerView) %7B%0A                super.onDrawerOpened(drawerView);%0A                InputMethodManager inputMethodManager = (InputMethodManager)%0A                        getSystemService(Context.INPUT_METHOD_SERVICE);%0A                inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);%0A            %7D%0A        %7D;%0A
 %0A       
@@ -3023,16 +3023,17 @@
 vTitle()
+ 
 %7B%0A      
@@ -3432,24 +3432,25 @@
 anceState);%0A
+ 
        // Sy
@@ -3512,16 +3512,16 @@
 curred.%0A
-
         
@@ -5073,16 +5073,17 @@
   switch
+ 
 (menuIte
@@ -5948,16 +5948,23 @@
 tivity.%0A
+     *%0A
      * @
@@ -6100,36 +6100,32 @@
 (this);%0A        
-    
 builder.addNextI
@@ -6158,28 +6158,24 @@
 t);%0A        
-    
 builder.star
@@ -6195,10 +6195,12 @@
 ;%0A    %7D%0A
+%0A%0A
 %7D%0A

fix #78: Navigation drawer and keyboard overlap each other