package com.reactnativenavigation.views;

import android.content.Context;
import android.os.Bundle;
import android.view.View;

import com.facebook.react.ReactRootView;
import com.reactnativenavigation.NavigationApplication;
import com.reactnativenavigation.params.NavigationParams;
import com.reactnativenavigation.screens.SingleScreen;
import com.reactnativenavigation.utils.ViewUtils;
import com.reactnativenavigation.views.utils.ViewMeasurer;

public class ContentView extends ReactRootView {
    private final String screenId;
    private final NavigationParams navigationParams;
    private Bundle initialProps;

    boolean isContentVisible = false;
    private SingleScreen.OnDisplayListener onDisplayListener;
    protected ViewMeasurer viewMeasurer;

    public void setOnDisplayListener(SingleScreen.OnDisplayListener onDisplayListener) {
        this.onDisplayListener = onDisplayListener;
    }

    public ContentView(Context context, String screenId, NavigationParams navigationParams) {
        this(context, screenId, navigationParams, Bundle.EMPTY);
    }

    public ContentView(Context context, String screenId, NavigationParams navigationParams, Bundle initialProps) {
        super(context);
        this.screenId = screenId;
        this.navigationParams = navigationParams;
        this.initialProps = initialProps;
        attachToJS();
        viewMeasurer = new ViewMeasurer();
    }

    public void setViewMeasurer(ViewMeasurer viewMeasurer) {
        this.viewMeasurer = viewMeasurer;
    }

    private void attachToJS() {
        navigationParams.toBundle().putAll(initialProps);
        startReactApplication(NavigationApplication.instance.getReactGateway().getReactInstanceManager(),
                screenId,
                createInitialParams()
        );
    }

    private Bundle createInitialParams() {
        final Bundle params = new Bundle();
        params.putAll(navigationParams.toBundle());
        params.putAll(initialProps);
        return params;
    }

    public String getNavigatorEventId() {
        return navigationParams.navigatorEventId;
    }

    public void unmountReactView() {
        unmountReactApplication();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSpec = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.AT_MOST);
        int heightSpec = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(heightMeasureSpec), MeasureSpec.AT_MOST);

        super.onMeasure(widthSpec, heightSpec);

        int measuredHeight = viewMeasurer.getMeasuredHeight(heightSpec);
        setMeasuredDimension(viewMeasurer.getMeasuredWidth(widthSpec), measuredHeight);
    }

    @Override
    public void onViewAdded(final View child) {
        super.onViewAdded(child);
        detectContentViewVisible(child);
    }

    private void detectContentViewVisible(final View child) {
        if (onDisplayListener != null) {
            post(new Runnable() {
                @Override
                public void run() {
                    ViewUtils.runOnPreDraw(child, new Runnable() {
                        @Override
                        public void run() {
                            if (!isContentVisible) {
                                isContentVisible = true;
                                onDisplayListener.onDisplay();
                                onDisplayListener = null;
                            }
                        }
                    });
                }
            });
        }
    }
}

@@ -2898,22 +2898,16 @@
 Visible(
-final 
 View chi
@@ -2904,32 +2904,32 @@
 e(View child) %7B%0A
+
         if (onDi
@@ -2969,13 +2969,38 @@
     
-post(
+ViewUtils.runOnPreDraw(child, 
 new 
@@ -3078,161 +3078,8 @@
 ) %7B%0A
-                    ViewUtils.runOnPreDraw(child, new Runnable() %7B%0A                        @Override%0A                        public void run() %7B%0A        
     
@@ -3115,32 +3115,24 @@
 tVisible) %7B%0A
-        
             
@@ -3188,32 +3188,24 @@
             
-  
-      
 onDisplayLis
@@ -3223,24 +3223,16 @@
 play();%0A
-        
         
@@ -3297,67 +3297,9 @@
     
-        %7D%0A                        %7D%0A                    %7D);
+%7D
 %0A   

Revert "Delay onDisplay until react finishes layout cycle"