package com.reactnativenavigation.views.toptabs;

import android.content.res.ColorStateList;
import android.graphics.Typeface;
import android.view.ViewGroup;
import android.widget.TextView;

import com.reactnativenavigation.parse.params.Color;
import com.reactnativenavigation.parse.params.Number;
import com.reactnativenavigation.utils.Task;
import com.reactnativenavigation.utils.ViewUtils;
import com.reactnativenavigation.views.toptabs.TopTabs;

class TopTabsStyleHelper {
    private TopTabs topTabs;

    TopTabsStyleHelper(TopTabs topTabs) {
        this.topTabs = topTabs;
    }

    void applyTopTabsFontSize(Number fontSize) {
        if (!fontSize.hasValue()) return;
        for (int i = 0; i < topTabs.getTabCount(); i++) {
            applyOnTabTitle(i, (title) -> title.setTextSize(fontSize.get()));
        }
    }

    void applyTopTabsColors(Color selected, Color unselected) {
        ColorStateList originalColors = topTabs.getTabTextColors();
        int selectedTabColor = originalColors != null ? originalColors.getColorForState(topTabs.getSelectedTabColors(), -1) : -1;
        int tabTextColor = originalColors != null ? originalColors.getColorForState(topTabs.getDefaultTabColors(), -1) : -1;

        if (selected.hasValue()) {
            tabTextColor = selected.get();
        }

        if (unselected.hasValue()) {
            selectedTabColor = unselected.get();
        }

        topTabs.setTabTextColors(tabTextColor, selectedTabColor);
    }

    void setFontFamily(int tabIndex, Typeface fontFamily) {
        applyOnTabTitle(tabIndex, (title) -> title.setTypeface(fontFamily));
    }

    private void applyOnTabTitle(int tabIndex, Task<TextView> action) {
        TextView title = ViewUtils.findChildByClass(getTabView(tabIndex), TextView.class);
        if (title != null) {
            action.run(title);
        }
    }

    private ViewGroup getTabView(int tabIndex) {
        return (ViewGroup) ((ViewGroup) topTabs.getChildAt(0)).getChildAt(tabIndex);
    }
}

@@ -389,64 +389,8 @@
 ils;
-%0Aimport com.reactnativenavigation.views.toptabs.TopTabs;
 %0A%0Acl
@@ -1204,23 +1204,27 @@
         
-tabText
+selectedTab
 Color = 
@@ -1295,35 +1295,31 @@
             
-selectedTab
+tabText
 Color = unse

Fix TopTabs selected and unselected colors