package com.davidstemmer.screenplay.sample.presenter;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.view.View;

import com.davidstemmer.screenplay.sample.R;
import com.davidstemmer.screenplay.sample.scene.ModalScene;
import com.davidstemmer.screenplay.sample.scene.PagedScene1;
import com.davidstemmer.screenplay.sample.scene.SimpleScene;
import com.davidstemmer.screenplay.sample.view.NavigationMenuView;
import com.davidstemmer.screenplay.scene.Scene;

import javax.inject.Inject;

import butterknife.ButterKnife;
import butterknife.OnClick;
import flow.Flow;
import mortar.ViewPresenter;

/**
 * Created by weefbellington on 10/25/14.
 */
public class NavigationMenuPresenter extends ViewPresenter<NavigationMenuView> implements DrawerLayout.DrawerListener {

    private final DrawerPresenter drawer;
    private final Flow flow;
    private final SimpleScene simpleScene;
    private final PagedScene1 pagedScene;
    private final ModalScene modalScene;

    private Scene nextScene;

    @Inject
    public NavigationMenuPresenter(DrawerPresenter drawerPresenter,
                                   Flow flow,
                                   SimpleScene simpleScene,
                                   PagedScene1 pagedScene,
                                   ModalScene modalScene) {

        this.drawer = drawerPresenter;
        this.flow = flow;
        this.simpleScene = simpleScene;
        this.pagedScene = pagedScene;
        this.modalScene = modalScene;
    }

    @OnClick(R.id.nav_item_simple_scene)
    void welcomeClicked() {
        nextScene = simpleScene;
        drawer.getLayout().closeDrawer(getView());
    }

    @OnClick(R.id.nav_item_paged_scenes)
    void pagedScenesClicked() {
        nextScene = pagedScene;
        drawer.getLayout().closeDrawer(getView());
    }

    @OnClick(R.id.nav_item_modal_scenes)
    void modalScenesClicked() {
        nextScene = modalScene;
        drawer.getLayout().closeDrawer(getView());
    }

    @Override
    protected void onLoad(Bundle savedInstanceState) {
        super.onLoad(savedInstanceState);

        ButterKnife.inject(this, (Activity) getView().getContext());
        drawer.getLayout().setDrawerListener(this);
    }

    @Override
    public void onDrawerSlide(View view, float v) {

    }

    @Override
    public void onDrawerOpened(View view) {

    }

    @Override
    public void onDrawerClosed(View view) {
        if (nextScene == null) {
            return;
        }
        if (flow.getBackstack().current().getScreen() != nextScene) {
            flow.resetTo(nextScene);
            nextScene = null;
        }
    }

    @Override
    public void onDrawerStateChanged(int i) {

    }

}

@@ -122,64 +122,18 @@
 oid.
-support.v4.widget.DrawerLayout;%0Aimport android.view.View
+os.Handler
 ;%0A%0Ai
@@ -743,47 +743,8 @@
 iew%3E
- implements DrawerLayout.DrawerListener
  %7B%0A%0A
@@ -1542,25 +1542,29 @@
         
-n
+showN
 extScene
  = simpl
@@ -1551,27 +1551,35 @@
 howNextScene
- = 
+AfterDelay(
 simpleScene;
@@ -1569,32 +1569,33 @@
 elay(simpleScene
+)
 ;%0A        drawer
@@ -1599,41 +1599,14 @@
 wer.
-getLayout().closeDrawer(getView()
+close(
 );%0A 
@@ -1692,25 +1692,29 @@
         
-n
+showN
 extScene
  = paged
@@ -1701,27 +1701,35 @@
 howNextScene
- = 
+AfterDelay(
 pagedScene;%0A
@@ -1718,32 +1718,33 @@
 Delay(pagedScene
+)
 ;%0A        drawer
@@ -1748,41 +1748,14 @@
 wer.
-getLayout().closeDrawer(getView()
+close(
 );%0A 
@@ -1841,25 +1841,29 @@
         
-n
+showN
 extScene
  = modal
@@ -1850,27 +1850,35 @@
 howNextScene
- = 
+AfterDelay(
 modalScene;%0A
@@ -1871,24 +1871,25 @@
 y(modalScene
+)
 ;%0A        dr
@@ -1897,41 +1897,14 @@
 wer.
-getLayout().closeDrawer(getView()
+close(
 );%0A 
@@ -2020,17 +2020,16 @@
 State);%0A
-%0A
         
@@ -2097,290 +2097,309 @@
     
+%7D%0A%0A
     
-drawer.getLayout().setDrawerListener(this);%0A    %7D%0A%0A    @Override%0A    public void onDrawerSlide(View view, float v
+private final Handler mDrawerHandler = new Handler();%0A    private void showNextSceneAfterDelay(final Scene nextScene
 ) %7B%0A
-%0A
     
-%7D%0A%0A
     
-@Override%0A    public void onDrawerOpened(View view) %7B%0A
+// Clears any previously posted runnables, for double clicks
 %0A    
-%7D%0A%0A
     
-@Override%0A    public void onDrawerClosed(View view) %7B%0A        if (nextScene == null
+mDrawerHandler.removeCallbacksAndMessages(null);%0A        mDrawerHandler.postDelayed(new Runnable(
 ) %7B%0A
@@ -2414,15 +2414,17 @@
     
-return;
+@Override
 %0A   
@@ -2432,80 +2432,36 @@
     
-%7D%0A
     
-    if (flow.getBackstack().current().getScreen() != nextScene) %7B%0A
+public void run() %7B%0A    
     
@@ -2479,11 +2479,13 @@
 w.re
-set
+place
 To(n
@@ -2511,109 +2511,144 @@
     
-nextScene = null;%0A        %7D%0A    %7D%0A%0A    @Override%0A    public void onDrawerStateChanged(int i) %7B%0A
+%7D%0A        %7D, 250);%0A        // The millisecond delay is arbitrary and was arrived at through trial and error%0A        drawer.close();
 %0A    %7D%0A
-%0A
 %7D%0A

Improve drawer navigation with delay