package com.navigation.reactnative;

import android.view.MenuItem;
import android.view.View;

import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.common.MapBuilder;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.ViewGroupManager;
import com.facebook.react.uimanager.annotations.ReactProp;

import java.util.Map;

import javax.annotation.Nonnull;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;

public class NavigationBarManager extends ViewGroupManager<NavigationBarView> {
    @Nonnull
    @Override
    public String getName() {
        return "NVNavigationBar";
    }

    @Nonnull
    @Override
    protected NavigationBarView createViewInstance(@Nonnull ThemedReactContext reactContext) {
        return new NavigationBarView(reactContext);
    }

    @Override
    public int getChildCount(NavigationBarView parent) {
        return parent.toolbar.getChildCount();
    }

    @Override
    public View getChildAt(NavigationBarView parent, int index) {
        return parent.toolbar.getChildAt(index);
    }

    @Override
    public void addView(NavigationBarView parent, View child, int index) {
        Toolbar.LayoutParams layoutParams = new Toolbar.LayoutParams(
                Toolbar.LayoutParams.MATCH_PARENT,
                Toolbar.LayoutParams.MATCH_PARENT
        );
        parent.toolbar.addView(child, index, layoutParams);
    }

    @Override
    public void removeViewAt(NavigationBarView parent, int index) {
        parent.toolbar.removeViewAt(index);
    }

    @ReactProp(name = "title")
    public void setTitle(NavigationBarView view, @Nullable String title) {
        view.setTitle(title);
    }

    @ReactProp(name = "logo")
    public void setLogo(NavigationBarView view, @Nullable ReadableMap logo) {
        view.setLogoSource(logo);
    }

    @ReactProp(name = "navIcon")
    public void setNavIcon(NavigationBarView view, @Nullable ReadableMap navIcon) {
        view.setNavIconSource(navIcon);
    }

    @ReactProp(name = "overflowIcon")
    public void setOverflowIcon(NavigationBarView view, @Nullable ReadableMap overflowIcon) {
        view.setOverflowIconSource(overflowIcon);
    }

    @ReactProp(name = "menuItems")
    public void setMenuItems(NavigationBarView view, @Nullable ReadableArray menuItems) {
        view.setMenuItems(menuItems);
    }

    @Override
    public Map<String, Object> getExportedCustomDirectEventTypeConstants() {
        return MapBuilder.<String, Object>builder()
                .put("onIconClicked", MapBuilder.of("registrationName", "onIconClicked"))
                .put("onActionSelected", MapBuilder.of("registrationName", "onActionSelected"))
                .build();
    }

    @Nullable
    @Override
    public Map<String, Object> getExportedViewConstants() {
        return MapBuilder.<String, Object>of(
                "ShowAsAction",
                MapBuilder.of(
                        "never", MenuItem.SHOW_AS_ACTION_NEVER,
                        "always", MenuItem.SHOW_AS_ACTION_ALWAYS,
                        "ifRoom", MenuItem.SHOW_AS_ACTION_IF_ROOM));
    }
}

@@ -30,16 +30,125 @@
 ative;%0A%0A
+import android.content.Context;%0Aimport android.content.res.Resources;%0Aimport android.content.res.TypedArray;%0A
 import a
@@ -169,16 +169,16 @@
 nuItem;%0A
-
 import a
@@ -1589,24 +1589,79 @@
 outParams);%0A
+        parent.toolbar.setContentInsetsRelative(0, 0);%0A
     %7D%0A%0A    @
@@ -1773,32 +1773,172 @@
 eViewAt(index);%0A
+        int%5B%5D insets = getDefaultContentInsets(parent.getContext());%0A        parent.toolbar.setContentInsetsRelative(insets%5B0%5D, insets%5B1%5D);%0A
     %7D%0A%0A    @Reac
@@ -3432,16 +3432,16 @@
 ALWAYS,%0A
-
         
@@ -3506,11 +3506,1677 @@
 );%0A    %7D
+%0A%0A    private int%5B%5D getDefaultContentInsets(Context context) %7B%0A        Resources.Theme theme = context.getTheme();%0A        TypedArray toolbarStyle = null;%0A        TypedArray contentInsets = null;%0A%0A        try %7B%0A            toolbarStyle =%0A                    theme.obtainStyledAttributes(new int%5B%5D %7BgetIdentifier(context, %22toolbarStyle%22)%7D);%0A%0A            int toolbarStyleResId = toolbarStyle.getResourceId(0, 0);%0A%0A            contentInsets =%0A                    theme.obtainStyledAttributes(%0A                            toolbarStyleResId,%0A                            new int%5B%5D %7B%0A                                    getIdentifier(context, %22contentInsetStart%22),%0A                                    getIdentifier(context, %22contentInsetEnd%22),%0A                            %7D);%0A%0A            int contentInsetStart = contentInsets.getDimensionPixelSize(0, 0);%0A            int contentInsetEnd = contentInsets.getDimensionPixelSize(1, 0);%0A%0A            return new int%5B%5D %7BcontentInsetStart, contentInsetEnd%7D;%0A        %7D finally %7B%0A            recycleQuietly(toolbarStyle);%0A            recycleQuietly(contentInsets);%0A        %7D%0A%0A    %7D%0A%0A    private static void recycleQuietly(@Nullable TypedArray style) %7B%0A        if (style != null) %7B%0A            style.recycle();%0A        %7D%0A    %7D%0A%0A    /**%0A     * The appcompat-v7 BUCK dep is listed as a provided_dep, which complains that%0A     * com.facebook.react.R doesn't exist. Since the attributes provided from a parent, we can access%0A     * those attributes dynamically.%0A     */%0A    private static int getIdentifier(Context context, String name) %7B%0A        return context.getResources().getIdentifier(name, %22attr%22, context.getPackageName());%0A    %7D
 %0A%7D%0A

Remove left padding on Toolbar to align TitleBarView correctly.