package com.reactnativenavigation.viewcontrollers.sidemenu;

import android.app.Activity;
import android.content.res.Resources;
import android.support.annotation.NonNull;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.DrawerLayout.LayoutParams;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;

import com.reactnativenavigation.parse.Options;
import com.reactnativenavigation.parse.SideMenuOptions;
import com.reactnativenavigation.presentation.OptionsPresenter;
import com.reactnativenavigation.presentation.SideMenuOptionsPresenter;
import com.reactnativenavigation.utils.CommandListener;
import com.reactnativenavigation.viewcontrollers.ChildControllersRegistry;
import com.reactnativenavigation.viewcontrollers.ParentController;
import com.reactnativenavigation.viewcontrollers.ViewController;
import com.reactnativenavigation.views.Component;

import java.util.ArrayList;
import java.util.Collection;

import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;

public class SideMenuController extends ParentController<DrawerLayout> {

	private ViewController centerController;
	private ViewController leftController;
	private ViewController rightController;
    private SideMenuOptionsPresenter presenter;

    public SideMenuController(Activity activity, ChildControllersRegistry childRegistry, String id, Options initialOptions, SideMenuOptionsPresenter sideMenuOptionsPresenter, OptionsPresenter presenter) {
		super(activity, childRegistry, id, presenter, initialOptions);
        this.presenter = sideMenuOptionsPresenter;
    }

    @Override
    protected ViewController getCurrentChild() {
	    if (getView().isDrawerOpen(Gravity.LEFT)) {
            return leftController;
        } else if (getView().isDrawerOpen(Gravity.RIGHT)) {
            return rightController;
        }
        return centerController;
    }

    @NonNull
	@Override
	protected DrawerLayout createView() {
        DrawerLayout sideMenu = new DrawerLayout(getActivity());
        presenter.bindView(sideMenu);
        return sideMenu;
	}

    @Override
    public void sendOnNavigationButtonPressed(String buttonId) {
        centerController.sendOnNavigationButtonPressed(buttonId);
    }

    @NonNull
	@Override
	public Collection<ViewController> getChildControllers() {
		ArrayList<ViewController> children = new ArrayList<>();
		if (centerController != null) children.add(centerController);
		if (leftController != null) children.add(leftController);
		if (rightController != null) children.add(rightController);
		return children;
	}

    @Override
    public void applyChildOptions(Options options, Component child) {
        super.applyChildOptions(options, child);
        performOnParentController(parentController ->
                ((ParentController) parentController).applyChildOptions(this.options, child)
        );
    }

    @Override
    public void mergeChildOptions(Options options, ViewController childController, Component child) {
        super.mergeChildOptions(options, childController, child);
        presenter.present(options.sideMenuRootOptions);
        performOnParentController(parentController ->
                ((ParentController) parentController).mergeChildOptions(options.copy().clearSideMenuOptions(), childController, child)
        );
    }

    @Override
    public void mergeOptions(Options options) {
        super.mergeOptions(options);
        presenter.present(this.options.sideMenuRootOptions);
    }

    @Override
    public boolean handleBack(CommandListener listener) {
        return presenter.handleBack() || super.handleBack(listener);
    }

    public void setCenterController(ViewController centerController) {
		this.centerController = centerController;
		View childView = centerController.getView();
		getView().addView(childView);
	}

    public void setLeftController(ViewController controller) {
        this.leftController = controller;
        int height = this.getHeight(options.sideMenuRootOptions.left);
        int width = this.getWidth(options.sideMenuRootOptions.left);
        getView().addView(controller.getView(), new LayoutParams(width, height, Gravity.LEFT));
    }

    public void setRightController(ViewController controller) {
        this.rightController = controller;
        int height = this.getHeight(options.sideMenuRootOptions.right);
        int width = this.getWidth(options.sideMenuRootOptions.right);
        getView().addView(controller.getView(), new LayoutParams(width, height, Gravity.RIGHT));
    }

    private int getWidth(SideMenuOptions sideMenuOptions) {
        int width = MATCH_PARENT;
        if (sideMenuOptions.width.hasValue()) {
            width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, sideMenuOptions.width.get(), Resources.getSystem().getDisplayMetrics());
        }
        return width;
    }

    private int getHeight(SideMenuOptions sideMenuOptions) {
        int height = MATCH_PARENT;
        if (sideMenuOptions.height.hasValue()) {
            height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, sideMenuOptions.height.get(), Resources.getSystem().getDisplayMetrics());
        }
        return height;
    }
}

@@ -1140,26 +1140,16 @@
 r center
-Controller
 ;%0A%09priva
@@ -1170,26 +1170,16 @@
 ler left
-Controller
 ;%0A%09priva
@@ -1197,34 +1197,24 @@
 roller right
-Controller
 ;%0A    privat
@@ -1715,26 +1715,16 @@
 urn left
-Controller
 ;%0A      
@@ -1801,26 +1801,16 @@
 rn right
-Controller
 ;%0A      
@@ -1834,26 +1834,16 @@
 n center
-Controller
 ;%0A    %7D%0A
@@ -2131,26 +2131,16 @@
   center
-Controller
 .sendOnN
@@ -2333,26 +2333,16 @@
  (center
-Controller
  != null
@@ -2362,26 +2362,16 @@
 d(center
-Controller
 );%0A%09%09if 
@@ -2375,26 +2375,16 @@
 if (left
-Controller
  != null
@@ -2402,26 +2402,16 @@
 add(left
-Controller
 );%0A%09%09if 
@@ -2416,26 +2416,16 @@
 f (right
-Controller
  != null
@@ -2444,26 +2444,16 @@
 dd(right
-Controller
 );%0A%09%09ret
@@ -3492,16 +3492,47 @@
 ack() %7C%7C
+ center.handleBack(listener) %7C%7C
  super.h
@@ -3643,26 +3643,16 @@
 s.center
-Controller
  = cente
@@ -3827,26 +3827,16 @@
 his.left
-Controller
  = contr
@@ -4167,26 +4167,16 @@
 is.right
-Controller
  = contr

Rename centre left right controllers