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 java.util.Arrays;
import java.util.WeakHashMap;
import o.AbstractC0126Ew;
import o.AbstractC1157f10;
import o.C1253g10;
import o.C1729l10;
import o.C2035oE;
import o.C2204q10;
import o.C2516tL;
import o.C2611uL;
import o.C2642ui;
import o.C2706vL;
import o.G0;
import o.Gm0;
import o.H0;
import o.Oj0;

/* compiled from: r8-map-id-3b7cc9102578c509342fa2f0fabed2bfc72239cd258d579fc679bfcbc198b048 */
/* loaded from: classes.dex */
public class GridLayoutManager extends LinearLayoutManager {
    public boolean E;
    public int F;
    public int[] G;
    public View[] H;
    public final SparseIntArray I;
    public final SparseIntArray J;
    public final Gm0 K;
    public final Rect L;

    public GridLayoutManager(Context context, AttributeSet attributeSet, int i, int i2) {
        super(context, attributeSet, i, i2);
        this.E = false;
        this.F = -1;
        this.I = new SparseIntArray();
        this.J = new SparseIntArray();
        this.K = new Gm0(6);
        this.L = new Rect();
        l1(AbstractC1157f10.I(context, attributeSet, i, i2).b);
    }

    @Override // androidx.recyclerview.widget.LinearLayoutManager
    public final void B0(C2204q10 c2204q10, C2706vL c2706vL, C2642ui c2642ui) {
        int i;
        int i2 = this.F;
        for (int i3 = 0; i3 < this.F && (i = c2706vL.d) >= 0 && i < c2204q10.b() && i2 > 0; i3++) {
            c2642ui.a(c2706vL.d, Math.max(0, c2706vL.g));
            this.K.getClass();
            i2--;
            c2706vL.d += c2706vL.e;
        }
    }

    @Override // o.AbstractC1157f10
    public final int J(C1729l10 c1729l10, C2204q10 c2204q10) {
        if (this.p == 0) {
            return this.F;
        }
        if (c2204q10.b() < 1) {
            return 0;
        }
        return h1(c2204q10.b() - 1, c1729l10, c2204q10) + 1;
    }

    @Override // androidx.recyclerview.widget.LinearLayoutManager
    public final View N0(C1729l10 c1729l10, C2204q10 c2204q10, int i, int i2, int i3) {
        G0();
        int iK = this.r.k();
        int iG = this.r.g();
        int i4 = i2 > i ? 1 : -1;
        View view = null;
        View view2 = null;
        while (i != i2) {
            View viewU = u(i);
            int iH = AbstractC1157f10.H(viewU);
            if (iH >= 0 && iH < i3 && i1(iH, c1729l10, c2204q10) == 0) {
                if (((C1253g10) viewU.getLayoutParams()).a.h()) {
                    if (view2 == null) {
                        view2 = viewU;
                    }
                } else {
                    if (this.r.e(viewU) < iG && this.r.b(viewU) >= iK) {
                        return viewU;
                    }
                    if (view == null) {
                        view = viewU;
                    }
                }
            }
            i += i4;
        }
        return view != null ? view : view2;
    }

    /* JADX WARN: Code restructure failed: missing block: B:62:0x00e2, code lost:
    
        if (r13 == (r2 > r15)) goto L57;
     */
    /* JADX WARN: Code restructure failed: missing block: B:84:0x0142, code lost:
    
        if (r16 == null) goto L86;
     */
    /* JADX WARN: Code restructure failed: missing block: B:85:0x0144, code lost:
    
        return r16;
     */
    /* JADX WARN: Code restructure failed: missing block: B:86:0x0145, code lost:
    
        return r17;
     */
    @Override // androidx.recyclerview.widget.LinearLayoutManager, o.AbstractC1157f10
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public final android.view.View S(android.view.View r23, int r24, o.C1729l10 r25, o.C2204q10 r26) {
        /*
            Method dump skipped, instructions count: 326
            To view this dump add '--comments-level debug' option
        */
        throw new UnsupportedOperationException("Method not decompiled: androidx.recyclerview.widget.GridLayoutManager.S(android.view.View, int, o.l10, o.q10):android.view.View");
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r12v23 */
    /* JADX WARN: Type inference failed for: r12v24, types: [boolean, int] */
    /* JADX WARN: Type inference failed for: r12v27 */
    /* JADX WARN: Type inference failed for: r12v28 */
    /* JADX WARN: Type inference failed for: r12v35 */
    @Override // androidx.recyclerview.widget.LinearLayoutManager
    public final void T0(C1729l10 c1729l10, C2204q10 c2204q10, C2706vL c2706vL, C2611uL c2611uL) {
        int i;
        int i2;
        int i3;
        int iD;
        int iD2;
        int iE;
        int i4;
        int iW;
        int iW2;
        ?? r12;
        int i5;
        View viewB;
        int iJ = this.r.j();
        boolean z = iJ != 1073741824;
        int i6 = v() > 0 ? this.G[this.F] : 0;
        if (z) {
            m1();
        }
        boolean z2 = c2706vL.e == 1;
        int iI1 = this.F;
        if (!z2) {
            iI1 = i1(c2706vL.d, c1729l10, c2204q10) + j1(c2706vL.d, c1729l10, c2204q10);
        }
        int i7 = 0;
        while (i7 < this.F && (i5 = c2706vL.d) >= 0 && i5 < c2204q10.b() && iI1 > 0) {
            int i8 = c2706vL.d;
            int iJ1 = j1(i8, c1729l10, c2204q10);
            if (iJ1 > this.F) {
                throw new IllegalArgumentException("Item at position " + i8 + " requires " + iJ1 + " spans but GridLayoutManager has only " + this.F + " spans.");
            }
            iI1 -= iJ1;
            if (iI1 < 0 || (viewB = c2706vL.b(c1729l10)) == null) {
                break;
            }
            this.H[i7] = viewB;
            i7++;
        }
        if (i7 == 0) {
            c2611uL.b = true;
            return;
        }
        if (z2) {
            i3 = 1;
            i2 = i7;
            i = 0;
        } else {
            i = i7 - 1;
            i2 = -1;
            i3 = -1;
        }
        int i9 = 0;
        while (i != i2) {
            View view = this.H[i];
            C2035oE c2035oE = (C2035oE) view.getLayoutParams();
            int iJ12 = j1(AbstractC1157f10.H(view), c1729l10, c2204q10);
            c2035oE.f = iJ12;
            c2035oE.e = i9;
            i9 += iJ12;
            i += i3;
        }
        float f = 0.0f;
        int i10 = 0;
        for (int i11 = 0; i11 < i7; i11++) {
            View view2 = this.H[i11];
            if (c2706vL.k != null) {
                r12 = 0;
                r12 = 0;
                if (z2) {
                    b(view2, -1, true);
                } else {
                    b(view2, 0, true);
                }
            } else if (z2) {
                r12 = 0;
                b(view2, -1, false);
            } else {
                r12 = 0;
                b(view2, 0, false);
            }
            RecyclerView recyclerView = this.b;
            Rect rect = this.L;
            if (recyclerView == null) {
                rect.set(r12, r12, r12, r12);
            } else {
                rect.set(recyclerView.J(view2));
            }
            k1(view2, iJ, r12);
            int iC = this.r.c(view2);
            if (iC > i10) {
                i10 = iC;
            }
            float fD = (this.r.d(view2) * 1.0f) / ((C2035oE) view2.getLayoutParams()).f;
            if (fD > f) {
                f = fD;
            }
        }
        if (z) {
            e1(Math.max(Math.round(f * this.F), i6));
            i10 = 0;
            for (int i12 = 0; i12 < i7; i12++) {
                View view3 = this.H[i12];
                k1(view3, 1073741824, true);
                int iC2 = this.r.c(view3);
                if (iC2 > i10) {
                    i10 = iC2;
                }
            }
        }
        for (int i13 = 0; i13 < i7; i13++) {
            View view4 = this.H[i13];
            if (this.r.c(view4) != i10) {
                C2035oE c2035oE2 = (C2035oE) view4.getLayoutParams();
                Rect rect2 = c2035oE2.b;
                int i14 = rect2.top + rect2.bottom + ((ViewGroup.MarginLayoutParams) c2035oE2).topMargin + ((ViewGroup.MarginLayoutParams) c2035oE2).bottomMargin;
                int i15 = rect2.left + rect2.right + ((ViewGroup.MarginLayoutParams) c2035oE2).leftMargin + ((ViewGroup.MarginLayoutParams) c2035oE2).rightMargin;
                int iG1 = g1(c2035oE2.e, c2035oE2.f);
                if (this.p == 1) {
                    iW2 = AbstractC1157f10.w(false, iG1, 1073741824, i15, ((ViewGroup.MarginLayoutParams) c2035oE2).width);
                    iW = View.MeasureSpec.makeMeasureSpec(i10 - i14, 1073741824);
                } else {
                    int iMakeMeasureSpec = View.MeasureSpec.makeMeasureSpec(i10 - i15, 1073741824);
                    iW = AbstractC1157f10.w(false, iG1, 1073741824, i14, ((ViewGroup.MarginLayoutParams) c2035oE2).height);
                    iW2 = iMakeMeasureSpec;
                }
                if (w0(view4, iW2, iW, (C1253g10) view4.getLayoutParams())) {
                    view4.measure(iW2, iW);
                }
            }
        }
        c2611uL.a = i10;
        if (this.p == 1) {
            if (c2706vL.f == -1) {
                int i16 = c2706vL.b;
                i4 = i16 - i10;
                iD2 = i16;
            } else {
                i4 = c2706vL.b;
                iD2 = i4 + i10;
            }
            iE = 0;
            iD = 0;
        } else {
            if (c2706vL.f == -1) {
                int i17 = c2706vL.b;
                iE = i17 - i10;
                iD2 = 0;
                iD = i17;
            } else {
                int i18 = c2706vL.b;
                iD = i18 + i10;
                iD2 = 0;
                iE = i18;
            }
            i4 = iD2;
        }
        for (int i19 = 0; i19 < i7; i19++) {
            View view5 = this.H[i19];
            C2035oE c2035oE3 = (C2035oE) view5.getLayoutParams();
            if (this.p != 1) {
                int iG = G() + this.G[c2035oE3.e];
                i4 = iG;
                iD2 = this.r.d(view5) + iG;
            } else if (S0()) {
                int iE2 = E() + this.G[this.F - c2035oE3.e];
                iD = iE2;
                iE = iE2 - this.r.d(view5);
            } else {
                iE = E() + this.G[c2035oE3.e];
                iD = this.r.d(view5) + iE;
            }
            AbstractC1157f10.N(view5, iE, i4, iD, iD2);
            if (c2035oE3.a.h() || c2035oE3.a.k()) {
                c2611uL.c = true;
            }
            c2611uL.d = view5.hasFocusable() | c2611uL.d;
        }
        Arrays.fill(this.H, (Object) null);
    }

    @Override // androidx.recyclerview.widget.LinearLayoutManager
    public final void U0(C1729l10 c1729l10, C2204q10 c2204q10, C2516tL c2516tL, int i) {
        m1();
        if (c2204q10.b() > 0 && !c2204q10.g) {
            boolean z = i == 1;
            int iI1 = i1(c2516tL.b, c1729l10, c2204q10);
            if (z) {
                while (iI1 > 0) {
                    int i2 = c2516tL.b;
                    if (i2 <= 0) {
                        break;
                    }
                    int i3 = i2 - 1;
                    c2516tL.b = i3;
                    iI1 = i1(i3, c1729l10, c2204q10);
                }
            } else {
                int iB = c2204q10.b() - 1;
                int i4 = c2516tL.b;
                while (i4 < iB) {
                    int i5 = i4 + 1;
                    int iI12 = i1(i5, c1729l10, c2204q10);
                    if (iI12 <= iI1) {
                        break;
                    }
                    i4 = i5;
                    iI1 = iI12;
                }
                c2516tL.b = i4;
            }
        }
        f1();
    }

    @Override // o.AbstractC1157f10
    public final void V(C1729l10 c1729l10, C2204q10 c2204q10, View view, H0 h0) {
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        if (!(layoutParams instanceof C2035oE)) {
            U(view, h0);
            return;
        }
        C2035oE c2035oE = (C2035oE) layoutParams;
        int iH1 = h1(c2035oE.a.b(), c1729l10, c2204q10);
        if (this.p == 0) {
            h0.k(G0.b(false, c2035oE.e, c2035oE.f, iH1, 1));
        } else {
            h0.k(G0.b(false, iH1, 1, c2035oE.e, c2035oE.f));
        }
    }

    @Override // o.AbstractC1157f10
    public final void W(int i, int i2) {
        Gm0 gm0 = this.K;
        gm0.k();
        ((SparseIntArray) gm0.n).clear();
    }

    @Override // o.AbstractC1157f10
    public final void X() {
        Gm0 gm0 = this.K;
        gm0.k();
        ((SparseIntArray) gm0.n).clear();
    }

    @Override // o.AbstractC1157f10
    public final void Y(int i, int i2) {
        Gm0 gm0 = this.K;
        gm0.k();
        ((SparseIntArray) gm0.n).clear();
    }

    @Override // o.AbstractC1157f10
    public final void Z(int i, int i2) {
        Gm0 gm0 = this.K;
        gm0.k();
        ((SparseIntArray) gm0.n).clear();
    }

    @Override // o.AbstractC1157f10
    public final void a0(int i, int i2) {
        Gm0 gm0 = this.K;
        gm0.k();
        ((SparseIntArray) gm0.n).clear();
    }

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

    @Override // androidx.recyclerview.widget.LinearLayoutManager, o.AbstractC1157f10
    public final void b0(C1729l10 c1729l10, C2204q10 c2204q10) {
        boolean z = c2204q10.g;
        SparseIntArray sparseIntArray = this.J;
        SparseIntArray sparseIntArray2 = this.I;
        if (z) {
            int iV = v();
            for (int i = 0; i < iV; i++) {
                C2035oE c2035oE = (C2035oE) u(i).getLayoutParams();
                int iB = c2035oE.a.b();
                sparseIntArray2.put(iB, c2035oE.f);
                sparseIntArray.put(iB, c2035oE.e);
            }
        }
        super.b0(c1729l10, c2204q10);
        sparseIntArray2.clear();
        sparseIntArray.clear();
    }

    @Override // androidx.recyclerview.widget.LinearLayoutManager, o.AbstractC1157f10
    public final void c0(C2204q10 c2204q10) {
        super.c0(c2204q10);
        this.E = false;
    }

    public final void e1(int i) {
        int i2;
        int[] iArr = this.G;
        int i3 = this.F;
        if (iArr == null || iArr.length != i3 + 1 || iArr[iArr.length - 1] != i) {
            iArr = new int[i3 + 1];
        }
        int i4 = 0;
        iArr[0] = 0;
        int i5 = i / i3;
        int i6 = i % i3;
        int i7 = 0;
        for (int i8 = 1; i8 <= i3; i8++) {
            i4 += i6;
            if (i4 <= 0 || i3 - i4 >= i6) {
                i2 = i5;
            } else {
                i2 = i5 + 1;
                i4 -= i3;
            }
            i7 += i2;
            iArr[i8] = i7;
        }
        this.G = iArr;
    }

    @Override // o.AbstractC1157f10
    public final boolean f(C1253g10 c1253g10) {
        return c1253g10 instanceof C2035oE;
    }

    public final void f1() {
        View[] viewArr = this.H;
        if (viewArr == null || viewArr.length != this.F) {
            this.H = new View[this.F];
        }
    }

    public final int g1(int i, int i2) {
        if (this.p != 1 || !S0()) {
            int[] iArr = this.G;
            return iArr[i2 + i] - iArr[i];
        }
        int[] iArr2 = this.G;
        int i3 = this.F;
        return iArr2[i3 - i] - iArr2[(i3 - i) - i2];
    }

    public final int h1(int i, C1729l10 c1729l10, C2204q10 c2204q10) {
        boolean z = c2204q10.g;
        Gm0 gm0 = this.K;
        if (!z) {
            int i2 = this.F;
            gm0.getClass();
            return Gm0.i(i, i2);
        }
        int iB = c1729l10.b(i);
        if (iB == -1) {
            return 0;
        }
        int i3 = this.F;
        gm0.getClass();
        return Gm0.i(iB, i3);
    }

    public final int i1(int i, C1729l10 c1729l10, C2204q10 c2204q10) {
        boolean z = c2204q10.g;
        Gm0 gm0 = this.K;
        if (!z) {
            int i2 = this.F;
            gm0.getClass();
            return i % i2;
        }
        int i3 = this.J.get(i, -1);
        if (i3 != -1) {
            return i3;
        }
        int iB = c1729l10.b(i);
        if (iB == -1) {
            return 0;
        }
        int i4 = this.F;
        gm0.getClass();
        return iB % i4;
    }

    public final int j1(int i, C1729l10 c1729l10, C2204q10 c2204q10) {
        boolean z = c2204q10.g;
        Gm0 gm0 = this.K;
        if (!z) {
            gm0.getClass();
            return 1;
        }
        int i2 = this.I.get(i, -1);
        if (i2 != -1) {
            return i2;
        }
        if (c1729l10.b(i) == -1) {
            return 1;
        }
        gm0.getClass();
        return 1;
    }

    @Override // androidx.recyclerview.widget.LinearLayoutManager, o.AbstractC1157f10
    public final int k(C2204q10 c2204q10) {
        return D0(c2204q10);
    }

    public final void k1(View view, int i, boolean z) {
        int iW;
        int iW2;
        C2035oE c2035oE = (C2035oE) view.getLayoutParams();
        Rect rect = c2035oE.b;
        int i2 = rect.top + rect.bottom + ((ViewGroup.MarginLayoutParams) c2035oE).topMargin + ((ViewGroup.MarginLayoutParams) c2035oE).bottomMargin;
        int i3 = rect.left + rect.right + ((ViewGroup.MarginLayoutParams) c2035oE).leftMargin + ((ViewGroup.MarginLayoutParams) c2035oE).rightMargin;
        int iG1 = g1(c2035oE.e, c2035oE.f);
        if (this.p == 1) {
            iW2 = AbstractC1157f10.w(false, iG1, i, i3, ((ViewGroup.MarginLayoutParams) c2035oE).width);
            iW = AbstractC1157f10.w(true, this.r.l(), this.m, i2, ((ViewGroup.MarginLayoutParams) c2035oE).height);
        } else {
            int iW3 = AbstractC1157f10.w(false, iG1, i, i2, ((ViewGroup.MarginLayoutParams) c2035oE).height);
            int iW4 = AbstractC1157f10.w(true, this.r.l(), this.l, i3, ((ViewGroup.MarginLayoutParams) c2035oE).width);
            iW = iW3;
            iW2 = iW4;
        }
        C1253g10 c1253g10 = (C1253g10) view.getLayoutParams();
        if (z ? w0(view, iW2, iW, c1253g10) : u0(view, iW2, iW, c1253g10)) {
            view.measure(iW2, iW);
        }
    }

    @Override // androidx.recyclerview.widget.LinearLayoutManager, o.AbstractC1157f10
    public final int l(C2204q10 c2204q10) {
        return E0(c2204q10);
    }

    public final void l1(int i) {
        if (i == this.F) {
            return;
        }
        this.E = true;
        if (i < 1) {
            throw new IllegalArgumentException(AbstractC0126Ew.j(i, "Span count should be at least 1. Provided "));
        }
        this.F = i;
        this.K.k();
        l0();
    }

    @Override // androidx.recyclerview.widget.LinearLayoutManager, o.AbstractC1157f10
    public final int m0(int i, C1729l10 c1729l10, C2204q10 c2204q10) {
        m1();
        f1();
        return super.m0(i, c1729l10, c2204q10);
    }

    public final void m1() {
        int iD;
        int iG;
        if (this.p == 1) {
            iD = this.n - F();
            iG = E();
        } else {
            iD = this.f519o - D();
            iG = G();
        }
        e1(iD - iG);
    }

    @Override // androidx.recyclerview.widget.LinearLayoutManager, o.AbstractC1157f10
    public final int n(C2204q10 c2204q10) {
        return D0(c2204q10);
    }

    @Override // androidx.recyclerview.widget.LinearLayoutManager, o.AbstractC1157f10
    public final int o(C2204q10 c2204q10) {
        return E0(c2204q10);
    }

    @Override // androidx.recyclerview.widget.LinearLayoutManager, o.AbstractC1157f10
    public final int o0(int i, C1729l10 c1729l10, C2204q10 c2204q10) {
        m1();
        f1();
        return super.o0(i, c1729l10, c2204q10);
    }

    @Override // androidx.recyclerview.widget.LinearLayoutManager, o.AbstractC1157f10
    public final C1253g10 r() {
        return this.p == 0 ? new C2035oE(-2, -1) : new C2035oE(-1, -2);
    }

    @Override // o.AbstractC1157f10
    public final void r0(Rect rect, int i, int i2) {
        int iG;
        int iG2;
        if (this.G == null) {
            super.r0(rect, i, i2);
        }
        int iF = F() + E();
        int iD = D() + G();
        if (this.p == 1) {
            int iHeight = rect.height() + iD;
            RecyclerView recyclerView = this.b;
            WeakHashMap weakHashMap = Oj0.a;
            iG2 = AbstractC1157f10.g(i2, iHeight, recyclerView.getMinimumHeight());
            int[] iArr = this.G;
            iG = AbstractC1157f10.g(i, iArr[iArr.length - 1] + iF, this.b.getMinimumWidth());
        } else {
            int iWidth = rect.width() + iF;
            RecyclerView recyclerView2 = this.b;
            WeakHashMap weakHashMap2 = Oj0.a;
            iG = AbstractC1157f10.g(i, iWidth, recyclerView2.getMinimumWidth());
            int[] iArr2 = this.G;
            iG2 = AbstractC1157f10.g(i2, iArr2[iArr2.length - 1] + iD, this.b.getMinimumHeight());
        }
        this.b.setMeasuredDimension(iG, iG2);
    }

    @Override // o.AbstractC1157f10
    public final C1253g10 s(Context context, AttributeSet attributeSet) {
        C2035oE c2035oE = new C2035oE(context, attributeSet);
        c2035oE.e = -1;
        c2035oE.f = 0;
        return c2035oE;
    }

    @Override // o.AbstractC1157f10
    public final C1253g10 t(ViewGroup.LayoutParams layoutParams) {
        if (layoutParams instanceof ViewGroup.MarginLayoutParams) {
            C2035oE c2035oE = new C2035oE((ViewGroup.MarginLayoutParams) layoutParams);
            c2035oE.e = -1;
            c2035oE.f = 0;
            return c2035oE;
        }
        C2035oE c2035oE2 = new C2035oE(layoutParams);
        c2035oE2.e = -1;
        c2035oE2.f = 0;
        return c2035oE2;
    }

    @Override // o.AbstractC1157f10
    public final int x(C1729l10 c1729l10, C2204q10 c2204q10) {
        if (this.p == 1) {
            return this.F;
        }
        if (c2204q10.b() < 1) {
            return 0;
        }
        return h1(c2204q10.b() - 1, c1729l10, c2204q10) + 1;
    }

    @Override // androidx.recyclerview.widget.LinearLayoutManager, o.AbstractC1157f10
    public final boolean z0() {
        return this.z == null && !this.E;
    }

    public GridLayoutManager(int i) {
        super(1);
        this.E = false;
        this.F = -1;
        this.I = new SparseIntArray();
        this.J = new SparseIntArray();
        this.K = new Gm0(6);
        this.L = new Rect();
        l1(i);
    }
}
