package androidx.leanback.widget;

import androidx.leanback.widget.GridLayoutManager;
import androidx.leanback.widget.am;
import defpackage.cr4;
import defpackage.e01;
import defpackage.g01;

/* JADX INFO: loaded from: classes.dex */
abstract class i0 extends am {
    public e01 j;
    public int k;
    public Object l;
    public int m;

    public static class a extends am.a {
        public int b;
        public int c;

        public a(int i, int i2) {
            super(i);
            this.b = i2;
            this.c = 0;
        }
    }

    @Override // androidx.leanback.widget.am
    public final boolean b(int i, boolean z) {
        Object[] objArr = this.a;
        if (this.b.c() == 0 || (!z && c(i))) {
            return false;
        }
        try {
            if (!o(i, z)) {
                return q(i, z);
            }
            objArr[0] = null;
            this.l = null;
            return true;
        } finally {
            objArr[0] = null;
            this.l = null;
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:20:0x0051  */
    @Override // androidx.leanback.widget.am
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final g01[] j(int i, int i2) {
        for (int i3 = 0; i3 < this.e; i3++) {
            g01 g01Var = this.h[i3];
            g01Var.c = g01Var.b;
        }
        if (i >= 0) {
            while (i <= i2) {
                g01 g01Var2 = this.h[k(i).a];
                if (g01Var2.c() <= 0) {
                    g01Var2.a(i);
                    g01Var2.a(i);
                } else {
                    int i4 = g01Var2.b;
                    int i5 = g01Var2.c;
                    if (i4 == i5) {
                        throw new ArrayIndexOutOfBoundsException();
                    }
                    int[] iArr = g01Var2.a;
                    int i6 = g01Var2.d;
                    if (iArr[(i5 - 1) & i6] == i - 1) {
                        if (i4 == i5) {
                            throw new ArrayIndexOutOfBoundsException();
                        }
                        int i7 = (i5 - 1) & i6;
                        int i8 = iArr[i7];
                        g01Var2.c = i7;
                        g01Var2.a(i);
                    }
                }
                i++;
            }
        }
        return this.h;
    }

    @Override // androidx.leanback.widget.am
    public final void l(int i) {
        super.l(i);
        e01 e01Var = this.j;
        e01Var.b((r() - i) + 1);
        if (e01Var.d() == 0) {
            this.k = -1;
        }
    }

    @Override // androidx.leanback.widget.am
    public final boolean m(int i, boolean z) {
        Object[] objArr = this.a;
        if (this.b.c() == 0 || (!z && d(i))) {
            return false;
        }
        try {
            if (!t(i, z)) {
                return v(i, z);
            }
            objArr[0] = null;
            this.l = null;
            return true;
        } finally {
            objArr[0] = null;
            this.l = null;
        }
    }

    public final boolean o(int i, boolean z) {
        int i2;
        int iD;
        int i3;
        e01 e01Var = this.j;
        if (e01Var.d() != 0) {
            int iC = this.b.c();
            int i4 = this.g;
            if (i4 >= 0) {
                i2 = i4 + 1;
                iD = this.b.d(i4);
            } else {
                int i5 = this.i;
                i2 = i5 != -1 ? i5 : 0;
                if (i2 > r() + 1 || i2 < this.k) {
                    e01Var.c(e01Var.d());
                    return false;
                }
                if (i2 <= r()) {
                    iD = Integer.MAX_VALUE;
                }
            }
            int iR = r();
            int i6 = i2;
            while (i6 < iC && i6 <= iR) {
                a aVarK = k(i6);
                if (iD != Integer.MAX_VALUE) {
                    iD += aVarK.b;
                }
                int i7 = iD;
                int i8 = aVarK.a;
                GridLayoutManager.b bVar = this.b;
                Object[] objArr = this.a;
                int iB = bVar.b(i6, true, objArr, false);
                if (iB != aVarK.c) {
                    aVarK.c = iB;
                    e01Var.b(iR - i6);
                    i3 = i6;
                } else {
                    i3 = iR;
                }
                this.g = i6;
                if (this.f < 0) {
                    this.f = i6;
                }
                this.b.a(objArr[0], i6, iB, i8, i7);
                if (z || !c(i)) {
                    iD = i7 == Integer.MAX_VALUE ? this.b.d(i6) : i7;
                    if (i8 != this.e - 1 || !z) {
                        i6++;
                        iR = i3;
                    }
                }
                return true;
            }
        }
        return false;
    }

    public final int p(int i, int i2, int i3) {
        int iD;
        e01 e01Var = this.j;
        int i4 = this.g;
        if (i4 >= 0 && (i4 != r() || this.g != i - 1)) {
            throw new IllegalStateException();
        }
        int i5 = this.g;
        if (i5 >= 0) {
            iD = i3 - this.b.d(i5);
        } else if (e01Var.d() <= 0 || i != r() + 1) {
            iD = 0;
        } else {
            int iR = r();
            while (true) {
                if (iR < this.k) {
                    iR = r();
                    break;
                }
                if (k(iR).a == i2) {
                    break;
                }
                iR--;
            }
            iD = this.c ? (-k(iR).c) - this.d : k(iR).c + this.d;
            for (int i6 = iR + 1; i6 <= r(); i6++) {
                iD -= k(i6).b;
            }
        }
        a aVar = new a(i2, iD);
        Object[] objArr = e01Var.a;
        int i7 = e01Var.c;
        objArr[i7] = aVar;
        int i8 = e01Var.d & (i7 + 1);
        e01Var.c = i8;
        if (i8 == e01Var.b) {
            e01Var.a();
        }
        Object obj = this.l;
        if (obj != null) {
            aVar.c = this.m;
            this.l = null;
        } else {
            GridLayoutManager.b bVar = this.b;
            Object[] objArr2 = this.a;
            aVar.c = bVar.b(i, true, objArr2, false);
            obj = objArr2[0];
        }
        Object obj2 = obj;
        if (e01Var.d() == 1) {
            this.g = i;
            this.f = i;
            this.k = i;
        } else {
            int i9 = this.g;
            if (i9 < 0) {
                this.g = i;
                this.f = i;
            } else {
                this.g = i9 + 1;
            }
        }
        this.b.a(obj2, i, aVar.c, i2, i3);
        return aVar.c;
    }

    public abstract boolean q(int i, boolean z);

    public final int r() {
        return (this.j.d() + this.k) - 1;
    }

    @Override // androidx.leanback.widget.am
    /* JADX INFO: renamed from: s, reason: merged with bridge method [inline-methods] */
    public final a k(int i) {
        e01 e01Var = this.j;
        int i2 = i - this.k;
        if (i2 < 0 || i2 >= e01Var.d()) {
            return null;
        }
        if (i2 < 0) {
            e01Var.getClass();
        } else if (i2 < e01Var.d()) {
            Object obj = e01Var.a[e01Var.d & (e01Var.b + i2)];
            cr4.d(obj);
            return (a) obj;
        }
        throw new ArrayIndexOutOfBoundsException();
    }

    public final boolean t(int i, boolean z) {
        int i2;
        int iD;
        int i3;
        e01 e01Var = this.j;
        if (e01Var.d() != 0) {
            int i4 = this.f;
            if (i4 < 0) {
                int i5 = this.i;
                i2 = i5 != -1 ? i5 : 0;
                if (i2 <= r()) {
                    int i6 = this.k;
                    if (i2 >= i6 - 1) {
                        if (i2 >= i6) {
                            iD = Integer.MAX_VALUE;
                            i3 = 0;
                        }
                    }
                }
                e01Var.c(e01Var.d());
                return false;
            }
            iD = this.b.d(i4);
            i3 = k(this.f).b;
            i2 = this.f - 1;
            int iMax = Math.max(GridLayoutManager.this.j, this.k);
            for (int i7 = i2; i7 >= iMax; i7--) {
                a aVarK = k(i7);
                int i8 = aVarK.a;
                GridLayoutManager.b bVar = this.b;
                Object[] objArr = this.a;
                int iB = bVar.b(i7, false, objArr, false);
                if (iB != aVarK.c) {
                    e01Var.c((i7 + 1) - this.k);
                    this.k = this.f;
                    this.l = objArr[0];
                    this.m = iB;
                    return false;
                }
                this.f = i7;
                if (this.g < 0) {
                    this.g = i7;
                }
                this.b.a(objArr[0], i7, iB, i8, iD - i3);
                if (z || !d(i)) {
                    iD = this.b.d(i7);
                    i3 = aVarK.b;
                    if (i8 != 0 || !z) {
                    }
                }
                return true;
            }
        }
        return false;
    }

    public final int u(int i, int i2, int i3) {
        int i4 = this.f;
        if (i4 >= 0 && (i4 != this.k || i4 != i + 1)) {
            throw new IllegalStateException();
        }
        int i5 = this.k;
        a aVarK = i5 >= 0 ? k(i5) : null;
        int iD = this.b.d(this.k);
        a aVar = new a(i2, 0);
        e01 e01Var = this.j;
        int i6 = (e01Var.b - 1) & e01Var.d;
        e01Var.b = i6;
        e01Var.a[i6] = aVar;
        if (i6 == e01Var.c) {
            e01Var.a();
        }
        Object obj = this.l;
        if (obj != null) {
            aVar.c = this.m;
            this.l = null;
        } else {
            GridLayoutManager.b bVar = this.b;
            Object[] objArr = this.a;
            aVar.c = bVar.b(i, false, objArr, false);
            obj = objArr[0];
        }
        Object obj2 = obj;
        this.f = i;
        this.k = i;
        if (this.g < 0) {
            this.g = i;
        }
        int i7 = !this.c ? i3 - aVar.c : i3 + aVar.c;
        if (aVarK != null) {
            aVarK.b = iD - i7;
        }
        this.b.a(obj2, i, aVar.c, i2, i7);
        return aVar.c;
    }

    public abstract boolean v(int i, boolean z);
}
