package com.reactnativenavigation.views.titlebar;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.reactnativenavigation.parse.Alignment;
import com.reactnativenavigation.parse.params.Button;
import com.reactnativenavigation.parse.params.Color;
import com.reactnativenavigation.viewcontrollers.ReactViewCreator;
import com.reactnativenavigation.viewcontrollers.TitleBarReactViewController;
import com.reactnativenavigation.viewcontrollers.TopBarButtonController;

import java.util.ArrayList;
import java.util.List;

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

@SuppressLint("ViewConstructor")
public class TitleBar extends Toolbar {
    private final ReactViewCreator buttonCreator;
    private TitleBarReactViewController reactViewController;
    private final TitleBarReactViewCreator reactViewCreator;
    private final TopBarButtonController.OnClickListener onClickListener;
    private final List<TopBarButtonController> rightButtonControllers = new ArrayList<>();
    private TopBarButtonController leftButtonController;

    public TitleBar(Context context, ReactViewCreator buttonCreator, TitleBarReactViewCreator reactViewCreator, TopBarButtonController.OnClickListener onClickListener) {
        super(context);
        this.buttonCreator = buttonCreator;
        this.reactViewCreator = reactViewCreator;
        reactViewController = new TitleBarReactViewController((Activity) context, reactViewCreator);
        this.onClickListener = onClickListener;
        getMenu();
        setContentDescription("titleBar");
    }

    @Override
    public void setTitle(CharSequence title) {
        clearComponent();
        super.setTitle(title);
    }

    public String getTitle() {
        return super.getTitle() == null ? "" : (String) super.getTitle();
    }

    public void setTitleTextColor(Color color) {
        if (color.hasValue()) setTitleTextColor(color.get());
    }

    public void setComponent(String componentName, Alignment alignment) {
        clearTitle();
        reactViewController.setComponent(componentName);
        addView(reactViewController.getView(), getComponentLayoutParams(alignment));
    }

    public void setBackgroundColor(Color color) {
        if (color.hasValue()) setBackgroundColor(color.get());
    }

    public void setTitleFontSize(float size) {
        TextView titleTextView = getTitleTextView();
        if (titleTextView != null) titleTextView.setTextSize(size);
    }

    public void setTitleTypeface(Typeface typeface) {
        TextView titleTextView = getTitleTextView();
        if (titleTextView != null) {
            titleTextView.setTypeface(typeface);
        }
    }

    public TextView getTitleTextView() {
        return findTextView(this);
    }

    public void clear() {
        clearTitle();
        clearSubtitle();
        clearRightButtons();
        clearLeftButton();
        clearComponent();
    }

    private void clearTitle() {
        setTitle(null);
    }

    private void clearSubtitle() {
        setSubtitle(null);
    }

    private void clearComponent() {
        reactViewController.destroy();
        reactViewController = new TitleBarReactViewController((Activity) getContext(), reactViewCreator);
    }

    private void clearLeftButton() {
        setNavigationIcon(null);
        if (leftButtonController != null) {
            leftButtonController.destroy();
            leftButtonController = null;
        }
    }

    private void clearRightButtons() {
        for (TopBarButtonController button : rightButtonControllers) {
            button.destroy();
        }
        rightButtonControllers.clear();
        getMenu().clear();
    }

    public void setLeftButtons(List<Button> leftButtons) {
        if (leftButtons == null) return;
        if (leftButtons.isEmpty()) {
            clearLeftButton();
            return;
        }
        if (leftButtons.size() > 1) {
            Log.w("RNN", "Use a custom TopBar to have more than one left button");
        }
        setLeftButton(leftButtons.get(0));
    }

    private void setLeftButton(final Button button) {
        TopBarButtonController controller = createButtonController(button);
        leftButtonController = controller;
        controller.applyNavigationIcon(this);
    }

    public void setRightButtons(List<Button> rightButtons) {
        if (rightButtons == null) return;
        clearRightButtons();
        for (int i = 0; i < rightButtons.size(); i++) {
            TopBarButtonController controller = createButtonController(rightButtons.get(i));
            rightButtonControllers.add(controller);
            controller.addToMenu(this, rightButtons.size() - i - 1);
        }
    }

    public TopBarButtonController createButtonController(Button button) {
        return new TopBarButtonController((Activity) getContext(), button, buttonCreator, onClickListener);
    }

    @Nullable
    private TextView findTextView(ViewGroup root) {
        for (int i = 0; i < root.getChildCount(); i++) {
            View view = root.getChildAt(i);
            if (view instanceof ViewGroup) {
                view = findTextView((ViewGroup) view);
            }
            if (view instanceof TextView) {
                return (TextView) view;
            }
        }
        return null;
    }

    public Toolbar.LayoutParams getComponentLayoutParams(Alignment alignment) {
        LayoutParams lp = new LayoutParams(MATCH_PARENT, getHeight());
        if (alignment == Alignment.Center) {
            lp.gravity = Gravity.CENTER;
        }
        return lp;
    }
}

@@ -183,52 +183,8 @@
 ce;%0A
-import android.support.annotation.Nullable;%0A
 impo
@@ -279,65 +279,8 @@
 ty;%0A
-import android.view.View;%0Aimport android.view.ViewGroup;%0A
 impo
@@ -461,24 +461,74 @@
 rams.Color;%0A
+import com.reactnativenavigation.utils.ViewUtils;%0A
 import com.r
@@ -2983,32 +2983,144 @@
     
-return findTextView(this
+List%3CTextView%3E children = ViewUtils.findChildrenByClass(this, TextView.class);%0A        return children.isEmpty() ? null : children.get(0
 );%0A 
@@ -5264,425 +5264,8 @@
  %7D%0A%0A
-    @Nullable%0A    private TextView findTextView(ViewGroup root) %7B%0A        for (int i = 0; i %3C root.getChildCount(); i++) %7B%0A            View view = root.getChildAt(i);%0A            if (view instanceof ViewGroup) %7B%0A                view = findTextView((ViewGroup) view);%0A            %7D%0A            if (view instanceof TextView) %7B%0A                return (TextView) view;%0A            %7D%0A        %7D%0A        return null;%0A    %7D%0A%0A
     

Resolve Title TextView using findChildrenByClass