/**
 * Copyright (c) 2015-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */

package com.facebook.react.views.view;

import javax.annotation.concurrent.NotThreadSafe;

import android.graphics.Rect;
import android.view.View;
import android.view.ViewParent;

import com.facebook.react.uimanager.ReactStylesDiffMap;

/**
 * Provides implementation of common tasks for view and it's view manager supporting property
 * {@code removeClippedSubviews}.
 */
@NotThreadSafe
public class ReactClippingViewGroupHelper {

  public static final String PROP_REMOVE_CLIPPED_SUBVIEWS = "removeClippedSubviews";

  private static final Rect sHelperRect = new Rect();

  /**
   * Can be used by view that support {@code removeClippedSubviews} property to calculate area that
   * given {@param view} should be clipped to based on the clipping rectangle of it's parent in
   * case when parent is also set to clip it's children.
   *
   * @param view view that we want to calculate clipping rect for
   * @param outputRect where the calculated rectangle will be written
   */
  public static void calculateClippingRect(View view, Rect outputRect) {
    ViewParent parent = view.getParent();
    if (parent == null) {
      outputRect.setEmpty();
      return;
    } else if (parent instanceof ReactClippingViewGroup) {
      ReactClippingViewGroup clippingViewGroup = (ReactClippingViewGroup) parent;
      if (clippingViewGroup.getRemoveClippedSubviews()) {
        clippingViewGroup.getClippingRect(sHelperRect);
        sHelperRect.offset(-view.getLeft(), -view.getTop());
        view.getDrawingRect(outputRect);
        if (!outputRect.intersect(sHelperRect)) {
          // rectangles does not intersect -> we should write empty rect to output
          outputRect.setEmpty();
        }
        return;
      }
    }
    view.getDrawingRect(outputRect);
  }
}

@@ -1730,16 +1730,159 @@
         
+// Intersect the view with the parent's rectangle%0A        // This will result in the overlap with coordinates in the parent space%0A        if (!
 sHelperR
@@ -1889,16 +1889,18 @@
 ect.
-off
+inter
 se
+c
 t(
--
 view
@@ -1911,17 +1911,16 @@
 Left(), 
--
 view.get
@@ -1928,51 +1928,47 @@
 op()
-);%0A        view.getDrawingRect(outputRect);
+, view.getRight(), view.getBottom())) %7B
 %0A   
@@ -1972,21 +1972,18 @@
         
-if (!
+  
 outputRe
@@ -1989,119 +1989,245 @@
 ect.
-intersect(sHelperRect)) %7B%0A
+setEmpty();%0A          return;%0A 
      
+  %7D%0A
      
-// rectangles does not 
+   // Now we move the coordinates to the View's coord
 in
+a
 te
-rsect -%3E we should write empty rect to output%0A  
+ space%0A        sHelperRect.offset(-view.getLeft(), -view.getTop());%0A        sHelperRect.offset(view.getScrollX(), view.getScrollY());%0A
     
@@ -2248,26 +2248,22 @@
 .set
-Empty();%0A        %7D
+(sHelperRect);
 %0A   

Fix the clip rect computation for removeClippedSubviews