package androidx.leanback.widget;

import androidx.leanback.widget.GridLayoutManager;
import androidx.leanback.widget.am;
import androidx.recyclerview.widget.RecyclerView;
import defpackage.g01;

/* JADX INFO: loaded from: classes.dex */
class h0 extends am {
    public final am.a j = new am.a(0);

    public h0() {
        n(1);
    }

    @Override // androidx.leanback.widget.am
    public final boolean b(int i, boolean z) {
        int iMin;
        int iD;
        if (this.b.c() == 0 || (!z && c(i))) {
            return false;
        }
        int i2 = this.g;
        if (i2 >= 0) {
            iMin = i2 + 1;
        } else {
            int i3 = this.i;
            iMin = i3 != -1 ? Math.min(i3, this.b.c() - 1) : 0;
        }
        int i4 = iMin;
        boolean z2 = false;
        while (i4 < this.b.c()) {
            GridLayoutManager.b bVar = this.b;
            Object[] objArr = this.a;
            int iB = bVar.b(i4, true, objArr, false);
            if (this.f < 0 || this.g < 0) {
                iD = this.c ? Integer.MAX_VALUE : Integer.MIN_VALUE;
                this.f = i4;
                this.g = i4;
            } else {
                if (this.c) {
                    int i5 = i4 - 1;
                    iD = (this.b.d(i5) - this.b.e(i5)) - this.d;
                } else {
                    int i6 = i4 - 1;
                    iD = this.b.d(i6) + this.b.e(i6) + this.d;
                }
                this.g = i4;
            }
            this.b.a(objArr[0], i4, iB, 0, iD);
            if (z || c(i)) {
                return true;
            }
            i4++;
            z2 = true;
        }
        return z2;
    }

    @Override // androidx.leanback.widget.am
    public final void e(int i, int i2, RecyclerView.o.c cVar) {
        int iO;
        int iD;
        if (!this.c ? i2 < 0 : i2 > 0) {
            if (this.g == this.b.c() - 1) {
                return;
            }
            int i3 = this.g;
            if (i3 >= 0) {
                iO = i3 + 1;
            } else {
                int i4 = this.i;
                iO = i4 != -1 ? Math.min(i4, this.b.c() - 1) : 0;
            }
            int iE = this.b.e(this.g) + this.d;
            int iD2 = this.b.d(this.g);
            if (this.c) {
                iE = -iE;
            }
            iD = iE + iD2;
        } else {
            if (this.f == 0) {
                return;
            }
            iO = o();
            iD = this.b.d(this.f) + (this.c ? this.d : -this.d);
        }
        cVar.a(iO, Math.abs(iD - i));
    }

    @Override // androidx.leanback.widget.am
    public final int f(int[] iArr, int i, boolean z) {
        if (iArr != null) {
            iArr[0] = 0;
            iArr[1] = i;
        }
        return this.c ? this.b.d(i) : this.b.d(i) + this.b.e(i);
    }

    @Override // androidx.leanback.widget.am
    public final int h(int[] iArr, int i, boolean z) {
        if (iArr != null) {
            iArr[0] = 0;
            iArr[1] = i;
        }
        return this.c ? this.b.d(i) - this.b.e(i) : this.b.d(i);
    }

    @Override // androidx.leanback.widget.am
    public final g01[] j(int i, int i2) {
        g01 g01Var = this.h[0];
        g01Var.c = g01Var.b;
        g01Var.a(i);
        this.h[0].a(i2);
        return this.h;
    }

    @Override // androidx.leanback.widget.am
    public final am.a k(int i) {
        return this.j;
    }

    @Override // androidx.leanback.widget.am
    public final boolean m(int i, boolean z) {
        int iD;
        if (this.b.c() == 0 || (!z && d(i))) {
            return false;
        }
        int i2 = GridLayoutManager.this.j;
        boolean z2 = false;
        for (int iO = o(); iO >= i2; iO--) {
            GridLayoutManager.b bVar = this.b;
            Object[] objArr = this.a;
            int iB = bVar.b(iO, false, objArr, false);
            if (this.f < 0 || this.g < 0) {
                iD = this.c ? Integer.MIN_VALUE : Integer.MAX_VALUE;
                this.f = iO;
                this.g = iO;
            } else {
                iD = this.c ? this.b.d(iO + 1) + this.d + iB : (this.b.d(iO + 1) - this.d) - iB;
                this.f = iO;
            }
            this.b.a(objArr[0], iO, iB, 0, iD);
            z2 = true;
            if (z || d(i)) {
                break;
            }
        }
        return z2;
    }

    public final int o() {
        int i = this.f;
        if (i >= 0) {
            return i - 1;
        }
        int i2 = this.i;
        return i2 != -1 ? Math.min(i2, this.b.c() - 1) : this.b.c() - 1;
    }
}
