package androidx.viewpager2.widget;

import android.view.View;
import android.view.ViewGroup;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;
import defpackage.n41;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Locale;

/* JADX INFO: loaded from: classes.dex */
final class h extends RecyclerView.t {
    public ViewPager2.g a;
    public final ViewPager2 b;
    public final RecyclerView c;
    public final LinearLayoutManager d;
    public int e;
    public int f;
    public final a g;
    public int h;
    public int i;
    public boolean j;
    public boolean k;
    public boolean l;

    public static final class a {
        public int a;
        public float b;
        public int c;
    }

    public h(ViewPager2 viewPager2) {
        this.b = viewPager2;
        RecyclerView recyclerView = viewPager2.l;
        this.c = recyclerView;
        this.d = (LinearLayoutManager) recyclerView.getLayoutManager();
        this.g = new a();
        b();
    }

    public final void a(int i) {
        if ((this.e == 3 && this.f == 0) || this.f == i) {
            return;
        }
        this.f = i;
        ViewPager2.g gVar = this.a;
        if (gVar != null) {
            gVar.onPageScrollStateChanged(i);
        }
    }

    public final void b() {
        this.e = 0;
        this.f = 0;
        a aVar = this.g;
        aVar.a = -1;
        aVar.b = 0.0f;
        aVar.c = 0;
        this.h = -1;
        this.i = -1;
        this.j = false;
        this.k = false;
        this.l = false;
    }

    /* JADX WARN: Removed duplicated region for block: B:65:0x0129  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void c() {
        int top;
        int childCount;
        int top2;
        int i;
        int bottom;
        int i2;
        LinearLayoutManager linearLayoutManager = this.d;
        int iFindFirstVisibleItemPosition = linearLayoutManager.findFirstVisibleItemPosition();
        a aVar = this.g;
        aVar.a = iFindFirstVisibleItemPosition;
        if (iFindFirstVisibleItemPosition == -1) {
            aVar.a = -1;
            aVar.b = 0.0f;
            aVar.c = 0;
            return;
        }
        View viewFindViewByPosition = linearLayoutManager.findViewByPosition(iFindFirstVisibleItemPosition);
        if (viewFindViewByPosition == null) {
            aVar.a = -1;
            aVar.b = 0.0f;
            aVar.c = 0;
            return;
        }
        int leftDecorationWidth = linearLayoutManager.getLeftDecorationWidth(viewFindViewByPosition);
        int rightDecorationWidth = linearLayoutManager.getRightDecorationWidth(viewFindViewByPosition);
        int topDecorationHeight = linearLayoutManager.getTopDecorationHeight(viewFindViewByPosition);
        int bottomDecorationHeight = linearLayoutManager.getBottomDecorationHeight(viewFindViewByPosition);
        ViewGroup.LayoutParams layoutParams = viewFindViewByPosition.getLayoutParams();
        if (layoutParams instanceof ViewGroup.MarginLayoutParams) {
            ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) layoutParams;
            leftDecorationWidth += marginLayoutParams.leftMargin;
            rightDecorationWidth += marginLayoutParams.rightMargin;
            topDecorationHeight += marginLayoutParams.topMargin;
            bottomDecorationHeight += marginLayoutParams.bottomMargin;
        }
        int height = viewFindViewByPosition.getHeight() + topDecorationHeight + bottomDecorationHeight;
        int width = viewFindViewByPosition.getWidth() + leftDecorationWidth + rightDecorationWidth;
        int orientation = linearLayoutManager.getOrientation();
        RecyclerView recyclerView = this.c;
        if (orientation == 0) {
            top = (viewFindViewByPosition.getLeft() - leftDecorationWidth) - recyclerView.getPaddingLeft();
            if (this.b.i.getLayoutDirection() == 1) {
                top = -top;
            }
            height = width;
        } else {
            top = (viewFindViewByPosition.getTop() - topDecorationHeight) - recyclerView.getPaddingTop();
        }
        int i3 = -top;
        aVar.c = i3;
        if (i3 >= 0) {
            aVar.b = height != 0 ? i3 / height : 0.0f;
            return;
        }
        int childCount2 = linearLayoutManager.getChildCount();
        if (childCount2 != 0) {
            boolean z = linearLayoutManager.getOrientation() == 0;
            int[][] iArr = (int[][]) Array.newInstance((Class<?>) Integer.TYPE, childCount2, 2);
            for (int i4 = 0; i4 < childCount2; i4++) {
                View childAt = linearLayoutManager.getChildAt(i4);
                if (childAt == null) {
                    throw new IllegalStateException("null view contained in the view hierarchy");
                }
                ViewGroup.LayoutParams layoutParams2 = childAt.getLayoutParams();
                ViewGroup.MarginLayoutParams marginLayoutParams2 = layoutParams2 instanceof ViewGroup.MarginLayoutParams ? (ViewGroup.MarginLayoutParams) layoutParams2 : b.a;
                int[] iArr2 = iArr[i4];
                if (z) {
                    top2 = childAt.getLeft();
                    i = marginLayoutParams2.leftMargin;
                } else {
                    top2 = childAt.getTop();
                    i = marginLayoutParams2.topMargin;
                }
                iArr2[0] = top2 - i;
                int[] iArr3 = iArr[i4];
                if (z) {
                    bottom = childAt.getRight();
                    i2 = marginLayoutParams2.rightMargin;
                } else {
                    bottom = childAt.getBottom();
                    i2 = marginLayoutParams2.bottomMargin;
                }
                iArr3[1] = bottom + i2;
            }
            Arrays.sort(iArr, new androidx.viewpager2.widget.a());
            int i5 = 1;
            while (true) {
                if (i5 >= childCount2) {
                    int[] iArr4 = iArr[0];
                    int i6 = iArr4[1];
                    int i7 = iArr4[0];
                    int i8 = i6 - i7;
                    if (i7 > 0 || iArr[childCount2 - 1][1] < i8) {
                        break;
                    }
                } else if (iArr[i5 - 1][1] != iArr[i5][0]) {
                    break;
                } else {
                    i5++;
                }
            }
            childCount = linearLayoutManager.getChildCount();
            for (int i9 = 0; i9 < childCount; i9++) {
                if (b.a(linearLayoutManager.getChildAt(i9))) {
                    throw new IllegalStateException("Page(s) contain a ViewGroup with a LayoutTransition (or animateLayoutChanges=\"true\"), which interferes with the scrolling animation. Make sure to call getLayoutTransition().setAnimateParentHierarchy(false) on all ViewGroups with a LayoutTransition before an animation is started.");
                }
            }
        } else if (linearLayoutManager.getChildCount() <= 1) {
            childCount = linearLayoutManager.getChildCount();
            while (i9 < childCount) {
            }
        }
        Locale locale = Locale.US;
        throw new IllegalStateException(n41.k("Page can only be offset by a positive amount, not by ", aVar.c));
    }

    @Override // androidx.recyclerview.widget.RecyclerView.t
    public final void onScrollStateChanged(RecyclerView recyclerView, int i) {
        ViewPager2.g gVar;
        ViewPager2.g gVar2;
        int i2 = this.e;
        if (!(i2 == 1 && this.f == 1) && i == 1) {
            this.e = 1;
            int i3 = this.i;
            if (i3 != -1) {
                this.h = i3;
                this.i = -1;
            } else if (this.h == -1) {
                this.h = this.d.findFirstVisibleItemPosition();
            }
            a(1);
            return;
        }
        if ((i2 == 1 || i2 == 4) && i == 2) {
            if (this.k) {
                a(2);
                this.j = true;
                return;
            }
            return;
        }
        a aVar = this.g;
        if ((i2 == 1 || i2 == 4) && i == 0) {
            c();
            if (!this.k) {
                int i4 = aVar.a;
                if (i4 != -1 && (gVar2 = this.a) != null) {
                    gVar2.onPageScrolled(i4, 0.0f, 0);
                }
            } else if (aVar.c == 0) {
                int i5 = this.h;
                int i6 = aVar.a;
                if (i5 != i6 && (gVar = this.a) != null) {
                    gVar.onPageSelected(i6);
                }
            }
            a(0);
            b();
        }
        if (this.e == 2 && i == 0 && this.l) {
            c();
            if (aVar.c == 0) {
                int i7 = this.i;
                int i8 = aVar.a;
                if (i7 != i8) {
                    if (i8 == -1) {
                        i8 = 0;
                    }
                    ViewPager2.g gVar3 = this.a;
                    if (gVar3 != null) {
                        gVar3.onPageSelected(i8);
                    }
                }
                a(0);
                b();
            }
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:15:0x0028  */
    @Override // androidx.recyclerview.widget.RecyclerView.t
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void onScrolled(RecyclerView recyclerView, int i, int i2) {
        int i3;
        ViewPager2.g gVar;
        this.k = true;
        c();
        boolean z = this.j;
        a aVar = this.g;
        if (z) {
            this.j = false;
            if (i2 > 0) {
                i3 = aVar.c != 0 ? aVar.a + 1 : aVar.a;
                this.i = i3;
                if (this.h != i3 && (gVar = this.a) != null) {
                    gVar.onPageSelected(i3);
                }
            } else {
                if (i2 == 0) {
                    if ((i < 0) == (this.b.i.getLayoutDirection() == 1)) {
                    }
                }
                this.i = i3;
                if (this.h != i3) {
                    gVar.onPageSelected(i3);
                }
            }
        } else if (this.e == 0) {
            int i4 = aVar.a;
            if (i4 == -1) {
                i4 = 0;
            }
            ViewPager2.g gVar2 = this.a;
            if (gVar2 != null) {
                gVar2.onPageSelected(i4);
            }
        }
        int i5 = aVar.a;
        if (i5 == -1) {
            i5 = 0;
        }
        float f = aVar.b;
        int i6 = aVar.c;
        ViewPager2.g gVar3 = this.a;
        if (gVar3 != null) {
            gVar3.onPageScrolled(i5, f, i6);
        }
        int i7 = aVar.a;
        int i8 = this.i;
        if ((i7 == i8 || i8 == -1) && aVar.c == 0 && this.f != 1) {
            a(0);
            b();
        }
    }
}
