package androidx.recyclerview.widget;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.SparseIntArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import androidx.annotation.NonNull;
import java.util.Arrays;

/* JADX INFO: loaded from: classes.dex */
public class GridLayoutManager extends LinearLayoutManager {
    private static final boolean DEBUG = false;
    public static final int DEFAULT_SPAN_COUNT = -1;
    private static final String TAG = "GridLayoutManager";
    int[] mCachedBorders;
    final Rect mDecorInsets;
    boolean mPendingSpanCountChange;
    final SparseIntArray mPreLayoutSpanIndexCache;
    final SparseIntArray mPreLayoutSpanSizeCache;
    View[] mSet;
    int mSpanCount;
    c0 mSpanSizeLookup;
    private boolean mUsingSpansToEstimateScrollBarDimensions;

    public GridLayoutManager(Context context, AttributeSet attributeSet, int i10, int i11) {
        super(context, attributeSet, i10, i11);
        this.mPendingSpanCountChange = false;
        this.mSpanCount = -1;
        this.mPreLayoutSpanSizeCache = new SparseIntArray();
        this.mPreLayoutSpanIndexCache = new SparseIntArray();
        this.mSpanSizeLookup = new a0();
        this.mDecorInsets = new Rect();
        setSpanCount(q1.getProperties(context, attributeSet, i10, i11).f3852b);
    }

    public static int[] calculateItemBorders(int[] iArr, int i10, int i11) {
        int i12;
        if (iArr == null || iArr.length != i10 + 1 || iArr[iArr.length - 1] != i11) {
            iArr = new int[i10 + 1];
        }
        int i13 = 0;
        iArr[0] = 0;
        int i14 = i11 / i10;
        int i15 = i11 % i10;
        int i16 = 0;
        for (int i17 = 1; i17 <= i10; i17++) {
            i13 += i15;
            if (i13 <= 0 || i10 - i13 >= i15) {
                i12 = i14;
            } else {
                i12 = i14 + 1;
                i13 -= i10;
            }
            i16 += i12;
            iArr[i17] = i16;
        }
        return iArr;
    }

    public final int B(f2 f2Var) {
        if (getChildCount() != 0 && f2Var.b() != 0) {
            ensureLayoutState();
            boolean zIsSmoothScrollbarEnabled = isSmoothScrollbarEnabled();
            boolean z10 = !zIsSmoothScrollbarEnabled;
            View viewFindFirstVisibleChildClosestToStart = findFirstVisibleChildClosestToStart(z10, true);
            View viewFindFirstVisibleChildClosestToEnd = findFirstVisibleChildClosestToEnd(z10, true);
            if (viewFindFirstVisibleChildClosestToStart != null && viewFindFirstVisibleChildClosestToEnd != null) {
                c0 c0Var = this.mSpanSizeLookup;
                int position = getPosition(viewFindFirstVisibleChildClosestToStart);
                int i10 = this.mSpanCount;
                c0Var.getClass();
                int iA = c0.a(position, i10);
                c0 c0Var2 = this.mSpanSizeLookup;
                int position2 = getPosition(viewFindFirstVisibleChildClosestToEnd);
                int i11 = this.mSpanCount;
                c0Var2.getClass();
                int iA2 = c0.a(position2, i11);
                int iMin = Math.min(iA, iA2);
                int iMax = Math.max(iA, iA2);
                c0 c0Var3 = this.mSpanSizeLookup;
                int iB = f2Var.b() - 1;
                int i12 = this.mSpanCount;
                c0Var3.getClass();
                int iMax2 = this.mShouldReverseLayout ? Math.max(0, ((c0.a(iB, i12) + 1) - iMax) - 1) : Math.max(0, iMin);
                if (!zIsSmoothScrollbarEnabled) {
                    return iMax2;
                }
                int iAbs = Math.abs(this.mOrientationHelper.d(viewFindFirstVisibleChildClosestToEnd) - this.mOrientationHelper.g(viewFindFirstVisibleChildClosestToStart));
                c0 c0Var4 = this.mSpanSizeLookup;
                int position3 = getPosition(viewFindFirstVisibleChildClosestToStart);
                int i13 = this.mSpanCount;
                c0Var4.getClass();
                int iA3 = c0.a(position3, i13);
                c0 c0Var5 = this.mSpanSizeLookup;
                int position4 = getPosition(viewFindFirstVisibleChildClosestToEnd);
                int i14 = this.mSpanCount;
                c0Var5.getClass();
                return Math.round((iMax2 * (iAbs / ((c0.a(position4, i14) - iA3) + 1))) + (this.mOrientationHelper.k() - this.mOrientationHelper.g(viewFindFirstVisibleChildClosestToStart)));
            }
        }
        return 0;
    }

    public final int C(f2 f2Var) {
        if (getChildCount() != 0 && f2Var.b() != 0) {
            ensureLayoutState();
            View viewFindFirstVisibleChildClosestToStart = findFirstVisibleChildClosestToStart(!isSmoothScrollbarEnabled(), true);
            View viewFindFirstVisibleChildClosestToEnd = findFirstVisibleChildClosestToEnd(!isSmoothScrollbarEnabled(), true);
            if (viewFindFirstVisibleChildClosestToStart != null && viewFindFirstVisibleChildClosestToEnd != null) {
                if (!isSmoothScrollbarEnabled()) {
                    c0 c0Var = this.mSpanSizeLookup;
                    int iB = f2Var.b() - 1;
                    int i10 = this.mSpanCount;
                    c0Var.getClass();
                    return c0.a(iB, i10) + 1;
                }
                int iD = this.mOrientationHelper.d(viewFindFirstVisibleChildClosestToEnd) - this.mOrientationHelper.g(viewFindFirstVisibleChildClosestToStart);
                c0 c0Var2 = this.mSpanSizeLookup;
                int position = getPosition(viewFindFirstVisibleChildClosestToStart);
                int i11 = this.mSpanCount;
                c0Var2.getClass();
                int iA = c0.a(position, i11);
                c0 c0Var3 = this.mSpanSizeLookup;
                int position2 = getPosition(viewFindFirstVisibleChildClosestToEnd);
                int i12 = this.mSpanCount;
                c0Var3.getClass();
                int iA2 = c0.a(position2, i12);
                c0 c0Var4 = this.mSpanSizeLookup;
                int iB2 = f2Var.b() - 1;
                int i13 = this.mSpanCount;
                c0Var4.getClass();
                return (int) ((iD / ((iA2 - iA) + 1)) * (c0.a(iB2, i13) + 1));
            }
        }
        return 0;
    }

    public final void D() {
        View[] viewArr = this.mSet;
        if (viewArr == null || viewArr.length != this.mSpanCount) {
            this.mSet = new View[this.mSpanCount];
        }
    }

    public final int E(int i10, y1 y1Var, f2 f2Var) {
        if (!f2Var.f3706g) {
            c0 c0Var = this.mSpanSizeLookup;
            int i11 = this.mSpanCount;
            c0Var.getClass();
            return c0.a(i10, i11);
        }
        int iB = y1Var.b(i10);
        if (iB == -1) {
            return 0;
        }
        c0 c0Var2 = this.mSpanSizeLookup;
        int i12 = this.mSpanCount;
        c0Var2.getClass();
        return c0.a(iB, i12);
    }

    public final int F(int i10, y1 y1Var, f2 f2Var) {
        if (!f2Var.f3706g) {
            c0 c0Var = this.mSpanSizeLookup;
            int i11 = this.mSpanCount;
            c0Var.getClass();
            return i10 % i11;
        }
        int i12 = this.mPreLayoutSpanIndexCache.get(i10, -1);
        if (i12 != -1) {
            return i12;
        }
        int iB = y1Var.b(i10);
        if (iB == -1) {
            return 0;
        }
        c0 c0Var2 = this.mSpanSizeLookup;
        int i13 = this.mSpanCount;
        c0Var2.getClass();
        return iB % i13;
    }

    public final int G(int i10, y1 y1Var, f2 f2Var) {
        if (!f2Var.f3706g) {
            this.mSpanSizeLookup.getClass();
            return 1;
        }
        int i11 = this.mPreLayoutSpanSizeCache.get(i10, -1);
        if (i11 != -1) {
            return i11;
        }
        if (y1Var.b(i10) == -1) {
            return 1;
        }
        this.mSpanSizeLookup.getClass();
        return 1;
    }

    public final void H(View view, int i10, boolean z10) {
        int childMeasureSpec;
        int childMeasureSpec2;
        b0 b0Var = (b0) view.getLayoutParams();
        Rect rect = b0Var.mDecorInsets;
        int i11 = rect.top + rect.bottom + ((ViewGroup.MarginLayoutParams) b0Var).topMargin + ((ViewGroup.MarginLayoutParams) b0Var).bottomMargin;
        int i12 = rect.left + rect.right + ((ViewGroup.MarginLayoutParams) b0Var).leftMargin + ((ViewGroup.MarginLayoutParams) b0Var).rightMargin;
        int spaceForSpanRange = getSpaceForSpanRange(b0Var.f3668a, b0Var.f3669b);
        if (this.mOrientation == 1) {
            childMeasureSpec2 = q1.getChildMeasureSpec(spaceForSpanRange, i10, i12, ((ViewGroup.MarginLayoutParams) b0Var).width, false);
            childMeasureSpec = q1.getChildMeasureSpec(this.mOrientationHelper.l(), getHeightMode(), i11, ((ViewGroup.MarginLayoutParams) b0Var).height, true);
        } else {
            int childMeasureSpec3 = q1.getChildMeasureSpec(spaceForSpanRange, i10, i11, ((ViewGroup.MarginLayoutParams) b0Var).height, false);
            int childMeasureSpec4 = q1.getChildMeasureSpec(this.mOrientationHelper.l(), getWidthMode(), i12, ((ViewGroup.MarginLayoutParams) b0Var).width, true);
            childMeasureSpec = childMeasureSpec3;
            childMeasureSpec2 = childMeasureSpec4;
        }
        r1 r1Var = (r1) view.getLayoutParams();
        if (z10 ? shouldReMeasureChild(view, childMeasureSpec2, childMeasureSpec, r1Var) : shouldMeasureChild(view, childMeasureSpec2, childMeasureSpec, r1Var)) {
            view.measure(childMeasureSpec2, childMeasureSpec);
        }
    }

    public final void I() {
        int height;
        int paddingTop;
        if (getOrientation() == 1) {
            height = getWidth() - getPaddingRight();
            paddingTop = getPaddingLeft();
        } else {
            height = getHeight() - getPaddingBottom();
            paddingTop = getPaddingTop();
        }
        this.mCachedBorders = calculateItemBorders(this.mCachedBorders, this.mSpanCount, height - paddingTop);
    }

    @Override // androidx.recyclerview.widget.q1
    public boolean checkLayoutParams(r1 r1Var) {
        return r1Var instanceof b0;
    }

    @Override // androidx.recyclerview.widget.LinearLayoutManager
    public void collectPrefetchPositionsForLayoutState(f2 f2Var, r0 r0Var, o1 o1Var) {
        int i10;
        int i11 = this.mSpanCount;
        for (int i12 = 0; i12 < this.mSpanCount && (i10 = r0Var.f3874d) >= 0 && i10 < f2Var.b() && i11 > 0; i12++) {
            ((x) o1Var).a(r0Var.f3874d, Math.max(0, r0Var.f3877g));
            this.mSpanSizeLookup.getClass();
            i11--;
            r0Var.f3874d += r0Var.f3875e;
        }
    }

    @Override // androidx.recyclerview.widget.LinearLayoutManager, androidx.recyclerview.widget.q1
    public int computeHorizontalScrollOffset(f2 f2Var) {
        return this.mUsingSpansToEstimateScrollBarDimensions ? B(f2Var) : super.computeHorizontalScrollOffset(f2Var);
    }

    @Override // androidx.recyclerview.widget.LinearLayoutManager, androidx.recyclerview.widget.q1
    public int computeHorizontalScrollRange(f2 f2Var) {
        return this.mUsingSpansToEstimateScrollBarDimensions ? C(f2Var) : super.computeHorizontalScrollRange(f2Var);
    }

    @Override // androidx.recyclerview.widget.LinearLayoutManager, androidx.recyclerview.widget.q1
    public int computeVerticalScrollOffset(f2 f2Var) {
        return this.mUsingSpansToEstimateScrollBarDimensions ? B(f2Var) : super.computeVerticalScrollOffset(f2Var);
    }

    @Override // androidx.recyclerview.widget.LinearLayoutManager, androidx.recyclerview.widget.q1
    public int computeVerticalScrollRange(f2 f2Var) {
        return this.mUsingSpansToEstimateScrollBarDimensions ? C(f2Var) : super.computeVerticalScrollRange(f2Var);
    }

    @Override // androidx.recyclerview.widget.LinearLayoutManager
    public View findReferenceChild(y1 y1Var, f2 f2Var, boolean z10, boolean z11) {
        int i10;
        int childCount;
        int childCount2 = getChildCount();
        int i11 = 1;
        if (z11) {
            childCount = getChildCount() - 1;
            i10 = -1;
            i11 = -1;
        } else {
            i10 = childCount2;
            childCount = 0;
        }
        int iB = f2Var.b();
        ensureLayoutState();
        int iK = this.mOrientationHelper.k();
        int i12 = this.mOrientationHelper.i();
        View view = null;
        View view2 = null;
        while (childCount != i10) {
            View childAt = getChildAt(childCount);
            int position = getPosition(childAt);
            if (position >= 0 && position < iB && F(position, y1Var, f2Var) == 0) {
                if (((r1) childAt.getLayoutParams()).isItemRemoved()) {
                    if (view2 == null) {
                        view2 = childAt;
                    }
                } else {
                    if (this.mOrientationHelper.g(childAt) < i12 && this.mOrientationHelper.d(childAt) >= iK) {
                        return childAt;
                    }
                    if (view == null) {
                        view = childAt;
                    }
                }
            }
            childCount += i11;
        }
        return view != null ? view : view2;
    }

    @Override // androidx.recyclerview.widget.LinearLayoutManager, androidx.recyclerview.widget.q1
    public r1 generateDefaultLayoutParams() {
        return this.mOrientation == 0 ? new b0(-2, -1) : new b0(-1, -2);
    }

    @Override // androidx.recyclerview.widget.q1
    public r1 generateLayoutParams(Context context, AttributeSet attributeSet) {
        b0 b0Var = new b0(context, attributeSet);
        b0Var.f3668a = -1;
        b0Var.f3669b = 0;
        return b0Var;
    }

    @Override // androidx.recyclerview.widget.q1
    public int getColumnCountForAccessibility(y1 y1Var, f2 f2Var) {
        if (this.mOrientation == 1) {
            return this.mSpanCount;
        }
        if (f2Var.b() < 1) {
            return 0;
        }
        return E(f2Var.b() - 1, y1Var, f2Var) + 1;
    }

    @Override // androidx.recyclerview.widget.q1
    public int getRowCountForAccessibility(y1 y1Var, f2 f2Var) {
        if (this.mOrientation == 0) {
            return this.mSpanCount;
        }
        if (f2Var.b() < 1) {
            return 0;
        }
        return E(f2Var.b() - 1, y1Var, f2Var) + 1;
    }

    public int getSpaceForSpanRange(int i10, int i11) {
        if (this.mOrientation != 1 || !isLayoutRTL()) {
            int[] iArr = this.mCachedBorders;
            return iArr[i11 + i10] - iArr[i10];
        }
        int[] iArr2 = this.mCachedBorders;
        int i12 = this.mSpanCount;
        return iArr2[i12 - i10] - iArr2[(i12 - i10) - i11];
    }

    public int getSpanCount() {
        return this.mSpanCount;
    }

    public c0 getSpanSizeLookup() {
        return this.mSpanSizeLookup;
    }

    public boolean isUsingSpansToEstimateScrollbarDimensions() {
        return this.mUsingSpansToEstimateScrollBarDimensions;
    }

    /* JADX WARN: Removed duplicated region for block: B:43:0x009a  */
    /* JADX WARN: Removed duplicated region for block: B:45:0x009d  */
    @Override // androidx.recyclerview.widget.LinearLayoutManager
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public void layoutChunk(y1 y1Var, f2 f2Var, r0 r0Var, q0 q0Var) {
        int heightMode;
        int i10;
        int i11;
        int i12;
        int i13;
        int i14;
        int i15;
        int i16;
        int iF;
        int iF2;
        int i17;
        int iF3;
        int childMeasureSpec;
        int childMeasureSpec2;
        boolean z10;
        int i18;
        View viewB;
        w0 w0Var = (w0) this.mOrientationHelper;
        int i19 = w0Var.f3942d;
        q1 q1Var = w0Var.f3953a;
        switch (i19) {
            case 0:
                heightMode = q1Var.getHeightMode();
                break;
            default:
                heightMode = q1Var.getWidthMode();
                break;
        }
        boolean z11 = heightMode != 1073741824;
        int i20 = getChildCount() > 0 ? this.mCachedBorders[this.mSpanCount] : 0;
        if (z11) {
            I();
        }
        boolean z12 = r0Var.f3875e == 1;
        int iF4 = this.mSpanCount;
        if (!z12) {
            iF4 = F(r0Var.f3874d, y1Var, f2Var) + G(r0Var.f3874d, y1Var, f2Var);
        }
        for (int i21 = 0; i21 < this.mSpanCount && (i18 = r0Var.f3874d) >= 0 && i18 < f2Var.b() && iF4 > 0; i21++) {
            int i22 = r0Var.f3874d;
            int iG = G(i22, y1Var, f2Var);
            if (iG > this.mSpanCount) {
                throw new IllegalArgumentException(u.a.b(defpackage.e.r("Item at position ", i22, " requires ", iG, " spans but GridLayoutManager has only "), this.mSpanCount, " spans."));
            }
            iF4 -= iG;
            if (iF4 < 0 || (viewB = r0Var.b(y1Var)) == null) {
                if (i21 != 0) {
                    q0Var.f3863b = true;
                    return;
                }
                if (z12) {
                    i12 = 1;
                    i11 = i21;
                    i10 = 0;
                } else {
                    i10 = i21 - 1;
                    i11 = -1;
                    i12 = -1;
                }
                int i23 = 0;
                while (i10 != i11) {
                    View view = this.mSet[i10];
                    b0 b0Var = (b0) view.getLayoutParams();
                    int iG2 = G(getPosition(view), y1Var, f2Var);
                    b0Var.f3669b = iG2;
                    b0Var.f3668a = i23;
                    i23 += iG2;
                    i10 += i12;
                }
                float f10 = 0.0f;
                int i24 = 0;
                for (int i25 = 0; i25 < i21; i25++) {
                    View view2 = this.mSet[i25];
                    if (r0Var.f3881k != null) {
                        z10 = false;
                        if (z12) {
                            addDisappearingView(view2);
                        } else {
                            addDisappearingView(view2, 0);
                        }
                    } else if (z12) {
                        addView(view2);
                        z10 = false;
                    } else {
                        z10 = false;
                        addView(view2, 0);
                    }
                    calculateItemDecorationsForChild(view2, this.mDecorInsets);
                    H(view2, heightMode, z10);
                    int iE = this.mOrientationHelper.e(view2);
                    if (iE > i24) {
                        i24 = iE;
                    }
                    float f11 = (this.mOrientationHelper.f(view2) * 1.0f) / ((b0) view2.getLayoutParams()).f3669b;
                    if (f11 > f10) {
                        f10 = f11;
                    }
                }
                if (z11) {
                    this.mCachedBorders = calculateItemBorders(this.mCachedBorders, this.mSpanCount, Math.max(Math.round(f10 * this.mSpanCount), i20));
                    i24 = 0;
                    for (int i26 = 0; i26 < i21; i26++) {
                        View view3 = this.mSet[i26];
                        H(view3, 1073741824, true);
                        int iE2 = this.mOrientationHelper.e(view3);
                        if (iE2 > i24) {
                            i24 = iE2;
                        }
                    }
                }
                for (int i27 = 0; i27 < i21; i27++) {
                    View view4 = this.mSet[i27];
                    if (this.mOrientationHelper.e(view4) != i24) {
                        b0 b0Var2 = (b0) view4.getLayoutParams();
                        Rect rect = b0Var2.mDecorInsets;
                        int i28 = rect.top + rect.bottom + ((ViewGroup.MarginLayoutParams) b0Var2).topMargin + ((ViewGroup.MarginLayoutParams) b0Var2).bottomMargin;
                        int i29 = rect.left + rect.right + ((ViewGroup.MarginLayoutParams) b0Var2).leftMargin + ((ViewGroup.MarginLayoutParams) b0Var2).rightMargin;
                        int spaceForSpanRange = getSpaceForSpanRange(b0Var2.f3668a, b0Var2.f3669b);
                        if (this.mOrientation == 1) {
                            childMeasureSpec2 = q1.getChildMeasureSpec(spaceForSpanRange, 1073741824, i29, ((ViewGroup.MarginLayoutParams) b0Var2).width, false);
                            childMeasureSpec = View.MeasureSpec.makeMeasureSpec(i24 - i28, 1073741824);
                        } else {
                            int iMakeMeasureSpec = View.MeasureSpec.makeMeasureSpec(i24 - i29, 1073741824);
                            childMeasureSpec = q1.getChildMeasureSpec(spaceForSpanRange, 1073741824, i28, ((ViewGroup.MarginLayoutParams) b0Var2).height, false);
                            childMeasureSpec2 = iMakeMeasureSpec;
                        }
                        if (shouldReMeasureChild(view4, childMeasureSpec2, childMeasureSpec, (r1) view4.getLayoutParams())) {
                            view4.measure(childMeasureSpec2, childMeasureSpec);
                        }
                    }
                }
                int i30 = 0;
                q0Var.f3862a = i24;
                if (this.mOrientation != 1) {
                    if (r0Var.f3876f == -1) {
                        int i31 = r0Var.f3872b;
                        i14 = i31 - i24;
                        i13 = i31;
                    } else {
                        int i32 = r0Var.f3872b;
                        i13 = i32 + i24;
                        i14 = i32;
                    }
                    i15 = 0;
                    i16 = 0;
                } else if (r0Var.f3876f == -1) {
                    i16 = r0Var.f3872b;
                    i15 = i16 - i24;
                    i14 = 0;
                    i13 = 0;
                } else {
                    int i33 = r0Var.f3872b;
                    i15 = i33;
                    i13 = 0;
                    i16 = i33 + i24;
                    i14 = 0;
                }
                while (i30 < i21) {
                    View view5 = this.mSet[i30];
                    b0 b0Var3 = (b0) view5.getLayoutParams();
                    if (this.mOrientation != 1) {
                        int paddingTop = getPaddingTop() + this.mCachedBorders[b0Var3.f3668a];
                        iF = i14;
                        iF2 = i13;
                        i17 = paddingTop;
                        iF3 = this.mOrientationHelper.f(view5) + paddingTop;
                    } else if (isLayoutRTL()) {
                        int paddingLeft = getPaddingLeft() + this.mCachedBorders[this.mSpanCount - b0Var3.f3668a];
                        iF2 = paddingLeft;
                        i17 = i15;
                        iF3 = i16;
                        iF = paddingLeft - this.mOrientationHelper.f(view5);
                    } else {
                        int paddingLeft2 = getPaddingLeft() + this.mCachedBorders[b0Var3.f3668a];
                        iF2 = this.mOrientationHelper.f(view5) + paddingLeft2;
                        i17 = i15;
                        iF3 = i16;
                        iF = paddingLeft2;
                    }
                    layoutDecoratedWithMargins(view5, iF, i17, iF2, iF3);
                    if (b0Var3.isItemRemoved() || b0Var3.isItemChanged()) {
                        q0Var.f3864c = true;
                    }
                    q0Var.f3865d |= view5.hasFocusable();
                    i30++;
                    i14 = iF;
                    i13 = iF2;
                    i15 = i17;
                    i16 = iF3;
                }
                Arrays.fill(this.mSet, (Object) null);
                return;
            }
            this.mSet[i21] = viewB;
        }
        if (i21 != 0) {
        }
    }

    @Override // androidx.recyclerview.widget.LinearLayoutManager
    public void onAnchorReady(y1 y1Var, f2 f2Var, p0 p0Var, int i10) {
        super.onAnchorReady(y1Var, f2Var, p0Var, i10);
        I();
        if (f2Var.b() > 0 && !f2Var.f3706g) {
            boolean z10 = i10 == 1;
            int iF = F(p0Var.f3847b, y1Var, f2Var);
            if (z10) {
                while (iF > 0) {
                    int i11 = p0Var.f3847b;
                    if (i11 <= 0) {
                        break;
                    }
                    int i12 = i11 - 1;
                    p0Var.f3847b = i12;
                    iF = F(i12, y1Var, f2Var);
                }
            } else {
                int iB = f2Var.b() - 1;
                int i13 = p0Var.f3847b;
                while (i13 < iB) {
                    int i14 = i13 + 1;
                    int iF2 = F(i14, y1Var, f2Var);
                    if (iF2 <= iF) {
                        break;
                    }
                    i13 = i14;
                    iF = iF2;
                }
                p0Var.f3847b = i13;
            }
        }
        D();
    }

    /* JADX WARN: Code restructure failed: missing block: B:56:0x00d1, code lost:
    
        if (r13 == (r2 > r15)) goto L47;
     */
    /* JADX WARN: Removed duplicated region for block: B:72:0x00f9  */
    /* JADX WARN: Removed duplicated region for block: B:73:0x010f  */
    @Override // androidx.recyclerview.widget.LinearLayoutManager, androidx.recyclerview.widget.q1
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public View onFocusSearchFailed(View view, int i10, y1 y1Var, f2 f2Var) {
        int childCount;
        int i11;
        int childCount2;
        View view2;
        View view3;
        int i12;
        int i13;
        int i14;
        int i15;
        y1 y1Var2 = y1Var;
        f2 f2Var2 = f2Var;
        View viewFindContainingItemView = findContainingItemView(view);
        View view4 = null;
        if (viewFindContainingItemView == null) {
            return null;
        }
        b0 b0Var = (b0) viewFindContainingItemView.getLayoutParams();
        int i16 = b0Var.f3668a;
        int i17 = b0Var.f3669b + i16;
        if (super.onFocusSearchFailed(view, i10, y1Var, f2Var) == null) {
            return null;
        }
        if ((convertFocusDirectionToLayoutDirection(i10) == 1) != this.mShouldReverseLayout) {
            childCount2 = getChildCount() - 1;
            childCount = -1;
            i11 = -1;
        } else {
            childCount = getChildCount();
            i11 = 1;
            childCount2 = 0;
        }
        boolean z10 = this.mOrientation == 1 && isLayoutRTL();
        int iE = E(childCount2, y1Var2, f2Var2);
        int i18 = -1;
        int i19 = -1;
        int iMin = 0;
        int iMin2 = 0;
        int i20 = childCount2;
        View view5 = null;
        while (i20 != childCount) {
            int iE2 = E(i20, y1Var2, f2Var2);
            View childAt = getChildAt(i20);
            if (childAt == viewFindContainingItemView) {
                break;
            }
            if (!childAt.hasFocusable() || iE2 == iE) {
                b0 b0Var2 = (b0) childAt.getLayoutParams();
                int i21 = b0Var2.f3668a;
                view2 = viewFindContainingItemView;
                int i22 = b0Var2.f3669b + i21;
                if (childAt.hasFocusable() && i21 == i16 && i22 == i17) {
                    return childAt;
                }
                if (!(childAt.hasFocusable() && view4 == null) && (childAt.hasFocusable() || view5 != null)) {
                    view3 = view5;
                    int iMin3 = Math.min(i22, i17) - Math.max(i21, i16);
                    if (!childAt.hasFocusable()) {
                        if (view4 == null) {
                            i12 = iMin;
                            i13 = childCount;
                            if (isViewPartiallyVisible(childAt, false, true)) {
                                i14 = iMin2;
                                if (iMin3 > i14) {
                                    i15 = i19;
                                } else if (iMin3 == i14) {
                                    i15 = i19;
                                    if (z10 == (i21 > i15)) {
                                    }
                                    i20 += i11;
                                    y1Var2 = y1Var;
                                    f2Var2 = f2Var;
                                    viewFindContainingItemView = view2;
                                    childCount = i13;
                                } else {
                                    i15 = i19;
                                }
                                if (childAt.hasFocusable()) {
                                    int i23 = b0Var2.f3668a;
                                    iMin2 = Math.min(i22, i17) - Math.max(i21, i16);
                                    i19 = i23;
                                    iMin = i12;
                                    view5 = childAt;
                                } else {
                                    i18 = b0Var2.f3668a;
                                    i19 = i15;
                                    iMin2 = i14;
                                    view5 = view3;
                                    view4 = childAt;
                                    iMin = Math.min(i22, i17) - Math.max(i21, i16);
                                }
                                i20 += i11;
                                y1Var2 = y1Var;
                                f2Var2 = f2Var;
                                viewFindContainingItemView = view2;
                                childCount = i13;
                            }
                            i19 = i15;
                            iMin2 = i14;
                            iMin = i12;
                            view5 = view3;
                            i20 += i11;
                            y1Var2 = y1Var;
                            f2Var2 = f2Var;
                            viewFindContainingItemView = view2;
                            childCount = i13;
                        }
                        i15 = i19;
                        i14 = iMin2;
                        i19 = i15;
                        iMin2 = i14;
                        iMin = i12;
                        view5 = view3;
                        i20 += i11;
                        y1Var2 = y1Var;
                        f2Var2 = f2Var;
                        viewFindContainingItemView = view2;
                        childCount = i13;
                    } else if (iMin3 <= iMin) {
                        if (iMin3 == iMin) {
                        }
                    }
                } else {
                    view3 = view5;
                }
                i12 = iMin;
                i13 = childCount;
                i15 = i19;
                i14 = iMin2;
                if (childAt.hasFocusable()) {
                }
                i20 += i11;
                y1Var2 = y1Var;
                f2Var2 = f2Var;
                viewFindContainingItemView = view2;
                childCount = i13;
            } else {
                if (view4 != null) {
                    break;
                }
                view2 = viewFindContainingItemView;
                view3 = view5;
            }
            i12 = iMin;
            i13 = childCount;
            i15 = i19;
            i14 = iMin2;
            i19 = i15;
            iMin2 = i14;
            iMin = i12;
            view5 = view3;
            i20 += i11;
            y1Var2 = y1Var;
            f2Var2 = f2Var;
            viewFindContainingItemView = view2;
            childCount = i13;
        }
        return view4 != null ? view4 : view5;
    }

    @Override // androidx.recyclerview.widget.q1
    public void onInitializeAccessibilityNodeInfo(@NonNull y1 y1Var, @NonNull f2 f2Var, @NonNull q0.f fVar) {
        super.onInitializeAccessibilityNodeInfo(y1Var, f2Var, fVar);
        fVar.j(GridView.class.getName());
    }

    @Override // androidx.recyclerview.widget.q1
    public void onInitializeAccessibilityNodeInfoForItem(y1 y1Var, f2 f2Var, View view, q0.f fVar) {
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        if (!(layoutParams instanceof b0)) {
            super.onInitializeAccessibilityNodeInfoForItem(view, fVar);
            return;
        }
        b0 b0Var = (b0) layoutParams;
        int iE = E(b0Var.getViewLayoutPosition(), y1Var, f2Var);
        if (this.mOrientation == 0) {
            fVar.l(q0.d.a(b0Var.f3668a, b0Var.f3669b, iE, 1, false, false));
        } else {
            fVar.l(q0.d.a(iE, 1, b0Var.f3668a, b0Var.f3669b, false, false));
        }
    }

    @Override // androidx.recyclerview.widget.q1
    public void onItemsAdded(RecyclerView recyclerView, int i10, int i11) {
        this.mSpanSizeLookup.b();
        this.mSpanSizeLookup.f3679b.clear();
    }

    @Override // androidx.recyclerview.widget.q1
    public void onItemsChanged(RecyclerView recyclerView) {
        this.mSpanSizeLookup.b();
        this.mSpanSizeLookup.f3679b.clear();
    }

    @Override // androidx.recyclerview.widget.q1
    public void onItemsMoved(RecyclerView recyclerView, int i10, int i11, int i12) {
        this.mSpanSizeLookup.b();
        this.mSpanSizeLookup.f3679b.clear();
    }

    @Override // androidx.recyclerview.widget.q1
    public void onItemsRemoved(RecyclerView recyclerView, int i10, int i11) {
        this.mSpanSizeLookup.b();
        this.mSpanSizeLookup.f3679b.clear();
    }

    @Override // androidx.recyclerview.widget.q1
    public void onItemsUpdated(RecyclerView recyclerView, int i10, int i11, Object obj) {
        this.mSpanSizeLookup.b();
        this.mSpanSizeLookup.f3679b.clear();
    }

    @Override // androidx.recyclerview.widget.LinearLayoutManager, androidx.recyclerview.widget.q1
    public void onLayoutChildren(y1 y1Var, f2 f2Var) {
        if (f2Var.f3706g) {
            int childCount = getChildCount();
            for (int i10 = 0; i10 < childCount; i10++) {
                b0 b0Var = (b0) getChildAt(i10).getLayoutParams();
                int viewLayoutPosition = b0Var.getViewLayoutPosition();
                this.mPreLayoutSpanSizeCache.put(viewLayoutPosition, b0Var.f3669b);
                this.mPreLayoutSpanIndexCache.put(viewLayoutPosition, b0Var.f3668a);
            }
        }
        super.onLayoutChildren(y1Var, f2Var);
        this.mPreLayoutSpanSizeCache.clear();
        this.mPreLayoutSpanIndexCache.clear();
    }

    @Override // androidx.recyclerview.widget.LinearLayoutManager, androidx.recyclerview.widget.q1
    public void onLayoutCompleted(f2 f2Var) {
        super.onLayoutCompleted(f2Var);
        this.mPendingSpanCountChange = false;
    }

    @Override // androidx.recyclerview.widget.LinearLayoutManager, androidx.recyclerview.widget.q1
    public int scrollHorizontallyBy(int i10, y1 y1Var, f2 f2Var) {
        I();
        D();
        return super.scrollHorizontallyBy(i10, y1Var, f2Var);
    }

    @Override // androidx.recyclerview.widget.LinearLayoutManager, androidx.recyclerview.widget.q1
    public int scrollVerticallyBy(int i10, y1 y1Var, f2 f2Var) {
        I();
        D();
        return super.scrollVerticallyBy(i10, y1Var, f2Var);
    }

    @Override // androidx.recyclerview.widget.q1
    public void setMeasuredDimension(Rect rect, int i10, int i11) {
        int iChooseSize;
        int iChooseSize2;
        if (this.mCachedBorders == null) {
            super.setMeasuredDimension(rect, i10, i11);
        }
        int paddingRight = getPaddingRight() + getPaddingLeft();
        int paddingBottom = getPaddingBottom() + getPaddingTop();
        if (this.mOrientation == 1) {
            iChooseSize2 = q1.chooseSize(i11, rect.height() + paddingBottom, getMinimumHeight());
            int[] iArr = this.mCachedBorders;
            iChooseSize = q1.chooseSize(i10, iArr[iArr.length - 1] + paddingRight, getMinimumWidth());
        } else {
            iChooseSize = q1.chooseSize(i10, rect.width() + paddingRight, getMinimumWidth());
            int[] iArr2 = this.mCachedBorders;
            iChooseSize2 = q1.chooseSize(i11, iArr2[iArr2.length - 1] + paddingBottom, getMinimumHeight());
        }
        setMeasuredDimension(iChooseSize, iChooseSize2);
    }

    public void setSpanCount(int i10) {
        if (i10 == this.mSpanCount) {
            return;
        }
        this.mPendingSpanCountChange = true;
        if (i10 < 1) {
            throw new IllegalArgumentException(com.google.android.gms.measurement.internal.u1.g("Span count should be at least 1. Provided ", i10));
        }
        this.mSpanCount = i10;
        this.mSpanSizeLookup.b();
        requestLayout();
    }

    public void setSpanSizeLookup(c0 c0Var) {
        this.mSpanSizeLookup = c0Var;
    }

    @Override // androidx.recyclerview.widget.LinearLayoutManager
    public void setStackFromEnd(boolean z10) {
        if (z10) {
            throw new UnsupportedOperationException("GridLayoutManager does not support stack from end. Consider using reverse layout");
        }
        super.setStackFromEnd(false);
    }

    public void setUsingSpansToEstimateScrollbarDimensions(boolean z10) {
        this.mUsingSpansToEstimateScrollBarDimensions = z10;
    }

    @Override // androidx.recyclerview.widget.LinearLayoutManager, androidx.recyclerview.widget.q1
    public boolean supportsPredictiveItemAnimations() {
        return this.mPendingSavedState == null && !this.mPendingSpanCountChange;
    }

    @Override // androidx.recyclerview.widget.q1
    public r1 generateLayoutParams(ViewGroup.LayoutParams layoutParams) {
        if (layoutParams instanceof ViewGroup.MarginLayoutParams) {
            b0 b0Var = new b0((ViewGroup.MarginLayoutParams) layoutParams);
            b0Var.f3668a = -1;
            b0Var.f3669b = 0;
            return b0Var;
        }
        b0 b0Var2 = new b0(layoutParams);
        b0Var2.f3668a = -1;
        b0Var2.f3669b = 0;
        return b0Var2;
    }

    public GridLayoutManager(int i10) {
        this.mPendingSpanCountChange = false;
        this.mSpanCount = -1;
        this.mPreLayoutSpanSizeCache = new SparseIntArray();
        this.mPreLayoutSpanIndexCache = new SparseIntArray();
        this.mSpanSizeLookup = new a0();
        this.mDecorInsets = new Rect();
        setSpanCount(i10);
    }

    public GridLayoutManager(int i10, int i11, boolean z10) {
        super(i11, z10);
        this.mPendingSpanCountChange = false;
        this.mSpanCount = -1;
        this.mPreLayoutSpanSizeCache = new SparseIntArray();
        this.mPreLayoutSpanIndexCache = new SparseIntArray();
        this.mSpanSizeLookup = new a0();
        this.mDecorInsets = new Rect();
        setSpanCount(i10);
    }
}
