/**
 * Copyright (c) 2017-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.litho.widget;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.facebook.litho.ComponentInfo;
import com.facebook.litho.SizeSpec;

/**
 * An implementation for {@link LayoutInfo} to implement linear lists with a
 * {@link LinearLayoutManager}.
 */
public class LinearLayoutInfo implements LayoutInfo {
  private static final int MAX_SANE_RANGE = 10;
  private static final int MIN_SANE_RANGE = 2;

  private final LinearLayoutManager mLinearLayoutManager;

  public LinearLayoutInfo(LinearLayoutManager linearLayoutManager) {
    mLinearLayoutManager = linearLayoutManager;
  }

  public LinearLayoutInfo(Context context, int orientation, boolean reverseLayout) {
      mLinearLayoutManager = new LinearLayoutManager(context, orientation, reverseLayout);
  }

  @Override
  public int getScrollDirection() {
    return mLinearLayoutManager.getOrientation();
  }

  @Override
  public int findFirstVisiblePosition() {
    return mLinearLayoutManager.findFirstVisibleItemPosition();
  }

  @Override
  public int findLastVisiblePosition() {
    return mLinearLayoutManager.findLastVisibleItemPosition();
  }

  @Override
  public RecyclerView.LayoutManager getLayoutManager() {
    return mLinearLayoutManager;
  }

  @Override
  public void setComponentInfoCollection(ComponentInfoCollection componentInfoCollection) {
    // Do nothing
  }

  @Override
  public int approximateRangeSize(
      int firstMeasuredItemWidth,
      int firstMeasuredItemHeight,
      int recyclerMeasuredWidth,
      int recyclerMeasuredHeight) {

    int approximateRange;

    switch (mLinearLayoutManager.getOrientation()) {
      case LinearLayoutManager.HORIZONTAL:
        approximateRange = (int)
            Math.ceil((float) recyclerMeasuredWidth / (float) firstMeasuredItemWidth);
        break;
      default:
        approximateRange = (int)
            Math.ceil((float) recyclerMeasuredHeight / (float) firstMeasuredItemHeight);
        break;
    }

    if (approximateRange < MIN_SANE_RANGE) {
      approximateRange = MIN_SANE_RANGE;
    } else if (approximateRange > MAX_SANE_RANGE) {
      approximateRange = MAX_SANE_RANGE;
    }

    return approximateRange;
  }

  @Override
  public int getChildHeightSpec(int heightSpec, ComponentInfo componentInfo) {
    switch (mLinearLayoutManager.getOrientation()) {
      case LinearLayoutManager.HORIZONTAL:
        return heightSpec;
      default:
        return SizeSpec.makeSizeSpec(0, SizeSpec.UNSPECIFIED);
    }
  }

  @Override
  public int getChildWidthSpec(int widthSpec, ComponentInfo componentInfo) {
    switch (mLinearLayoutManager.getOrientation()) {
      case LinearLayoutManager.HORIZONTAL:
        return SizeSpec.makeSizeSpec(0, SizeSpec.UNSPECIFIED);
      default:
        return widthSpec;
    }
  }
}

@@ -425,16 +425,68 @@
 anager;%0A
+import android.support.v7.widget.OrientationHelper;%0A
 import a
@@ -524,16 +524,16 @@
 erView;%0A
-
 %0Aimport 
@@ -1132,24 +1132,675 @@
 seLayout) %7B%0A
+    if (orientation == OrientationHelper.HORIZONTAL) %7B%0A      // Predictive animation in RecyclerView has some bugs that surface occasionally when one%0A      // RecyclerView is used as a child of another RecyclerView and it is very hard to repro%0A      // (https://issuetracker.google.com/issues/37007605). This one disables that optional%0A      // feature for Horizontal one which is used as inner RecyclerView most of the cases.%0A      // Disabling predictive animations lets RecyclerView use the simple animations instead.%0A      mLinearLayoutManager =%0A          new LinearLayoutManagerNoPredictiveAnim(context, orientation, reverseLayout);%0A    %7D else %7B%0A
       mLinea
@@ -1878,16 +1878,22 @@
 ayout);%0A
+    %7D%0A
   %7D%0A%0A  @
@@ -3894,11 +3894,396 @@
    %7D%0A  %7D
+%0A%0A  private static class LinearLayoutManagerNoPredictiveAnim%0A      extends LinearLayoutManager %7B%0A%0A    public LinearLayoutManagerNoPredictiveAnim(%0A        Context context,%0A        int orientation,%0A        boolean reverseLayout) %7B%0A      super(context, orientation, reverseLayout);%0A    %7D%0A%0A    @Override%0A    public boolean supportsPredictiveItemAnimations() %7B%0A      return false;%0A    %7D%0A  %7D
 %0A%7D%0A

Disable predictive animations for child Horizontal RecyclerViews when using binder with LinearLayoutInfo