package com.reactnativenavigation.views;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.widget.LinearLayout;

import com.aurelhubert.ahbottomnavigation.AHBottomNavigation;
import com.aurelhubert.ahbottomnavigation.AHBottomNavigationItem;
import com.reactnativenavigation.R;
import com.reactnativenavigation.parse.LayoutDirection;

import androidx.annotation.IntRange;

import static com.reactnativenavigation.utils.ViewUtils.findChildByClass;

@SuppressLint("ViewConstructor")
public class BottomTabs extends AHBottomNavigation {
    private boolean itemsCreationEnabled = true;
    private boolean shouldCreateItems = true;

    public BottomTabs(Context context) {
        super(context);
        setId(R.id.bottomTabs);
    }

    public void disableItemsCreation() {
        itemsCreationEnabled = false;
    }

    public void enableItemsCreation() {
        itemsCreationEnabled = true;
        if (shouldCreateItems) createItems();
    }

    @Override
    public void onSizeChanged(int w, int h, int oldw, int oldh) {
        if (hasItemsAndIsMeasured(w, h, oldw, oldh)) createItems();
    }

    @Override
    protected void createItems() {
        if (itemsCreationEnabled) {
            superCreateItems();
        } else {
            shouldCreateItems = true;
        }
    }

    public void superCreateItems() {
        super.createItems();
    }

    @Override
    public void setCurrentItem(@IntRange(from = 0) int position) {
        super.setCurrentItem(position);
    }

    @Override
    public void setTitleState(TitleState titleState) {
        if (getTitleState() != titleState) super.setTitleState(titleState);
    }

    @Override
    public void setBackgroundColor(int color) {
        super.setBackgroundColor(color);
        if (getDefaultBackgroundColor() != color) setDefaultBackgroundColor(color);
    }

    public void setText(int index, String text) {
        AHBottomNavigationItem item = getItem(index);
        if (!item.getTitle(getContext()).equals(text)) {
            item.setTitle(text);
            refresh();
        }
    }

    public void setIcon(int index, Drawable icon) {
        AHBottomNavigationItem item = getItem(index);
        if (!item.getDrawable(getContext()).equals(icon)) {
            item.setIcon(icon);
            refresh();
        }
    }

    public void setSelectedIcon(int index, Drawable icon) {
        AHBottomNavigationItem item = getItem(index);
        if (!item.getDrawable(getContext()).equals(icon)) {
            item.setSelectedIcon(icon);
            refresh();
        }
    }

    public void setLayoutDirection(LayoutDirection direction) {
         LinearLayout tabsContainer = findChildByClass(this, LinearLayout.class);
        if (tabsContainer != null) tabsContainer.setLayoutDirection(direction.get());
    }

    private boolean hasItemsAndIsMeasured(int w, int h, int oldw, int oldh) {
        return w != 0 && h != 0 && (w != oldw || h != oldh) && getItemsCount() > 0;
    }
}

@@ -419,38 +419,155 @@
 ort 
-androidx.annotation.IntRange;%0A
+java.util.ArrayList;%0Aimport java.util.List;%0A%0Aimport androidx.annotation.IntRange;%0A%0Aimport static com.reactnativenavigation.utils.CollectionUtils.*;
 %0Aimp
@@ -818,16 +818,86 @@
  = true;
+%0A    private List%3CRunnable%3E onItemCreationEnabled = new ArrayList%3C%3E();
 %0A%0A    pu
@@ -1188,30 +1188,156 @@
 eItems) 
-createItems();
+%7B%0A            createItems();%0A            forEach(onItemCreationEnabled, Runnable::run);%0A            onItemCreationEnabled.clear();%0A        %7D
 %0A    %7D%0A%0A
@@ -1847,38 +1847,357 @@
    s
-uper.setCurrentItem(position);
+etCurrentItem(position, true);%0A    %7D%0A%0A    @Override%0A    public void setCurrentItem(@IntRange(from = 0) int position, boolean useCallback) %7B%0A        if (itemsCreationEnabled) %7B%0A            super.setCurrentItem(position, useCallback);%0A        %7D else %7B%0A            onItemCreationEnabled.add(() -%3E super.setCurrentItem(position, useCallback));%0A        %7D
 %0A   

Fix crash when currentTabIndex is defined (#6334)