package com.reactnativenavigation.views;

import android.content.Context;
import android.graphics.Color;

import com.aurelhubert.ahbottomnavigation.AHBottomNavigation;
import com.aurelhubert.ahbottomnavigation.AHBottomNavigationItem;
import com.reactnativenavigation.animation.VisibilityAnimator;
import com.reactnativenavigation.params.AppStyle;
import com.reactnativenavigation.params.ScreenParams;
import com.reactnativenavigation.params.StyleParams;
import com.reactnativenavigation.utils.ViewUtils;

import java.util.List;

public class BottomTabs extends AHBottomNavigation {

    private VisibilityAnimator visibilityAnimator;

    public BottomTabs(Context context) {
        super(context);
        setForceTint(true);
        setId(ViewUtils.generateViewId());
        createVisibilityAnimator();
        setStyle();
    }

    public void addTabs(List<ScreenParams> params, OnTabSelectedListener onTabSelectedListener) {
        for (ScreenParams screenParams : params) {
            AHBottomNavigationItem item = new AHBottomNavigationItem(screenParams.title, screenParams.tabIcon,
                    Color.GRAY);
            addItem(item);
            setOnTabSelectedListener(onTabSelectedListener);
        }
    }

    public void setStyleFromScreen(StyleParams params) {
        setBackgroundColor(params.bottomTabsColor);

        if (params.bottomTabsButtonColor.hasColor()) {
            setInactiveColor(params.bottomTabsButtonColor.getColor());
        }

        if (params.selectedBottomTabsButtonColor.hasColor()) {
            setAccentColor(params.selectedBottomTabsButtonColor.getColor());
        }

        setForceTitlesDisplay(params.forceTitlesDisplay);

        setVisibility(params.bottomTabsHidden, true);
    }

    public void setVisibility(boolean hidden, boolean animated) {
        if (visibilityAnimator != null) {
            visibilityAnimator.setVisible(!hidden, animated);
        } else {
            setVisibility(hidden);
        }
    }

    private void setBackgroundColor(StyleParams.Color bottomTabsColor) {
        if (bottomTabsColor.hasColor()) {
            setDefaultBackgroundColor(bottomTabsColor.getColor());
        } else {
            setDefaultBackgroundColor(Color.WHITE);
        }
    }

    private void setVisibility(boolean bottomTabsHidden) {
        setVisibility(bottomTabsHidden ? GONE : VISIBLE);
    }

    private void createVisibilityAnimator() {
        ViewUtils.runOnPreDraw(this, new Runnable() {
            @Override
            public void run() {
                visibilityAnimator = new VisibilityAnimator(BottomTabs.this,
                        VisibilityAnimator.HideDirection.Down,
                        getHeight());
            }
        });
    }

    private void setStyle() {
        if (AppStyle.appStyle == null) {
            return;
        }
        if (AppStyle.appStyle.bottomTabBadgeBackgroundColor.hasColor()) {
            setNotificationBackgroundColor(AppStyle.appStyle.bottomTabBadgeBackgroundColor.getColor());
        }
        if (AppStyle.appStyle.bottomTabBadgeTextColor.hasColor()) {
            setNotificationTextColor(AppStyle.appStyle.bottomTabBadgeTextColor.getColor());
        }
    }
}

@@ -2785,106 +2785,11 @@
 if (
-AppStyle.appStyle == null) %7B%0A            return;%0A        %7D%0A        if (AppStyle.appStyle.bottomTab
+has
 Badg
@@ -2800,33 +2800,24 @@
 kgroundColor
-.hasColor
 ()) %7B%0A      
@@ -2940,53 +2940,20 @@
 if (
-AppStyle.appStyle.bottomTabBadgeTextColor.has
+hasBadgeText
 Colo
@@ -3043,32 +3043,32 @@
 or.getColor());%0A
-
         %7D%0A    %7D%0A
@@ -3066,11 +3066,371 @@
  %7D%0A    %7D
+%0A%0A    private boolean hasBadgeTextColor() %7B%0A        return AppStyle.appStyle.bottomTabBadgeTextColor != null && AppStyle.appStyle.bottomTabBadgeTextColor.hasColor();%0A    %7D%0A%0A    private boolean hasBadgeBackgroundColor() %7B%0A        return AppStyle.appStyle.bottomTabBadgeBackgroundColor != null && AppStyle.appStyle.bottomTabBadgeBackgroundColor.hasColor();%0A    %7D
 %0A%7D%0A

Fix npe in BottomTabswhen appStyle is undefined. Again.