package androidx.viewpager2.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.accessibility.AccessibilityNodeInfo;
import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.annotation.RestrictTo;
import androidx.core.view.s1;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.d1;
import androidx.recyclerview.widget.l1;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.List;
import tv.danmaku.ijk.media.player.IjkMediaMeta;
import x3.a;
import xyz.stream.utils.a0;
import xyz.stream.utils.t;
import y3.b;
import y3.c;
import y3.d;
import y3.e;
import y3.f;
import y3.g;
import y3.j;
import y3.k;
import y3.n;
import y3.o;
import y3.p;
import y3.q;
import y3.r;
import y3.s;

/* JADX INFO: loaded from: classes.dex */
public final class ViewPager2 extends ViewGroup {
    public t Q;
    public b R;
    public l1 S;
    public boolean T;
    public boolean U;
    public int V;
    public n W;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final Rect f4391a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final Rect f4392b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final f f4393c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public int f4394d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public boolean f4395e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final e f4396f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public j f4397g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public int f4398h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public Parcelable f4399i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public RecyclerView f4400j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public p f4401k;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public d f4402l;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public f f4403m;

    @IntRange(from = IjkMediaMeta.AV_CH_FRONT_LEFT)
    @Retention(RetentionPolicy.SOURCE)
    @RestrictTo({RestrictTo.Scope.f557c})
    public @interface OffscreenPageLimit {
    }

    @Retention(RetentionPolicy.SOURCE)
    @RestrictTo({RestrictTo.Scope.f557c})
    public @interface Orientation {
    }

    @Retention(RetentionPolicy.SOURCE)
    @RestrictTo({RestrictTo.Scope.f557c})
    public @interface ScrollState {
    }

    public ViewPager2(@NonNull Context context) {
        super(context);
        this.f4391a = new Rect();
        this.f4392b = new Rect();
        this.f4393c = new f();
        this.f4395e = false;
        this.f4396f = new e(this, 0);
        this.f4398h = -1;
        this.S = null;
        this.T = false;
        this.U = true;
        this.V = -1;
        a(context, null);
    }

    private void registerCurrentItemDataSetTracker(@Nullable d1 d1Var) {
        if (d1Var != null) {
            d1Var.registerAdapterDataObserver(this.f4396f);
        }
    }

    private void unregisterCurrentItemDataSetTracker(@Nullable d1 d1Var) {
        if (d1Var != null) {
            d1Var.unregisterAdapterDataObserver(this.f4396f);
        }
    }

    public final void a(Context context, AttributeSet attributeSet) {
        this.W = new n(this);
        q qVar = new q(this, context);
        this.f4400j = qVar;
        qVar.setId(View.generateViewId());
        this.f4400j.setDescendantFocusability(131072);
        j jVar = new j(this);
        this.f4397g = jVar;
        this.f4400j.setLayoutManager(jVar);
        int i10 = 1;
        this.f4400j.setScrollingTouchSlop(1);
        int[] iArr = a.ViewPager2;
        TypedArray typedArrayObtainStyledAttributes = context.obtainStyledAttributes(attributeSet, iArr);
        s1.saveAttributeDataForStyleable(this, context, iArr, attributeSet, typedArrayObtainStyledAttributes, 0, 0);
        try {
            int i11 = 0;
            setOrientation(typedArrayObtainStyledAttributes.getInt(a.ViewPager2_android_orientation, 0));
            typedArrayObtainStyledAttributes.recycle();
            this.f4400j.setLayoutParams(new ViewGroup.LayoutParams(-1, -1));
            this.f4400j.addOnChildAttachStateChangeListener(new g());
            d dVar = new d(this);
            this.f4402l = dVar;
            this.Q = new t(this, dVar, this.f4400j, 6);
            p pVar = new p(this);
            this.f4401k = pVar;
            pVar.attachToRecyclerView(this.f4400j);
            this.f4400j.addOnScrollListener(this.f4402l);
            f fVar = new f();
            this.f4403m = fVar;
            this.f4402l.f36770a = fVar;
            f fVar2 = new f(this, i11);
            f fVar3 = new f(this, i10);
            ((List) fVar.f36786b).add(fVar2);
            ((List) this.f4403m.f36786b).add(fVar3);
            this.W.a(this.f4400j);
            f fVar4 = this.f4403m;
            ((List) fVar4.f36786b).add(this.f4393c);
            b bVar = new b();
            this.R = bVar;
            ((List) this.f4403m.f36786b).add(bVar);
            RecyclerView recyclerView = this.f4400j;
            attachViewToParent(recyclerView, 0, recyclerView.getLayoutParams());
        } catch (Throwable th) {
            typedArrayObtainStyledAttributes.recycle();
            throw th;
        }
    }

    public final void b() {
        d1 adapter;
        if (this.f4398h == -1 || (adapter = getAdapter()) == null) {
            return;
        }
        if (this.f4399i != null) {
            this.f4399i = null;
        }
        int iMax = Math.max(0, Math.min(this.f4398h, adapter.getItemCount() - 1));
        this.f4394d = iMax;
        this.f4398h = -1;
        this.f4400j.scrollToPosition(iMax);
        this.W.b();
    }

    public final void c(int i10, boolean z10) {
        k kVar;
        d1 adapter = getAdapter();
        if (adapter == null) {
            if (this.f4398h != -1) {
                this.f4398h = Math.max(i10, 0);
                return;
            }
            return;
        }
        if (adapter.getItemCount() <= 0) {
            return;
        }
        int iMin = Math.min(Math.max(i10, 0), adapter.getItemCount() - 1);
        int i11 = this.f4394d;
        if (iMin == i11 && this.f4402l.f36775f == 0) {
            return;
        }
        if (iMin == i11 && z10) {
            return;
        }
        double d10 = i11;
        this.f4394d = iMin;
        this.W.b();
        d dVar = this.f4402l;
        if (dVar.f36775f != 0) {
            dVar.c();
            c cVar = dVar.f36776g;
            d10 = ((double) cVar.f36768b) + ((double) cVar.f36767a);
        }
        d dVar2 = this.f4402l;
        dVar2.getClass();
        dVar2.f36774e = z10 ? 2 : 3;
        dVar2.f36782m = false;
        boolean z11 = dVar2.f36778i != iMin;
        dVar2.f36778i = iMin;
        dVar2.a(2);
        if (z11 && (kVar = dVar2.f36770a) != null) {
            kVar.c(iMin);
        }
        if (!z10) {
            this.f4400j.scrollToPosition(iMin);
            return;
        }
        double d11 = iMin;
        if (Math.abs(d11 - d10) <= 3.0d) {
            this.f4400j.smoothScrollToPosition(iMin);
            return;
        }
        this.f4400j.scrollToPosition(d11 > d10 ? iMin - 3 : iMin + 3);
        RecyclerView recyclerView = this.f4400j;
        recyclerView.post(new s(recyclerView, iMin));
    }

    @Override // android.view.View
    public final boolean canScrollHorizontally(int i10) {
        return this.f4400j.canScrollHorizontally(i10);
    }

    @Override // android.view.View
    public final boolean canScrollVertically(int i10) {
        return this.f4400j.canScrollVertically(i10);
    }

    public final void d() {
        p pVar = this.f4401k;
        if (pVar == null) {
            throw new IllegalStateException("Design assumption violated.");
        }
        View viewFindSnapView = pVar.findSnapView(this.f4397g);
        if (viewFindSnapView == null) {
            return;
        }
        int position = this.f4397g.getPosition(viewFindSnapView);
        if (position != this.f4394d && getScrollState() == 0) {
            this.f4403m.c(position);
        }
        this.f4395e = false;
    }

    @Override // android.view.ViewGroup, android.view.View
    public final void dispatchRestoreInstanceState(SparseArray sparseArray) {
        Parcelable parcelable = (Parcelable) sparseArray.get(getId());
        if (parcelable instanceof r) {
            int i10 = ((r) parcelable).f36796a;
            sparseArray.put(this.f4400j.getId(), (Parcelable) sparseArray.get(i10));
            sparseArray.remove(i10);
        }
        super.dispatchRestoreInstanceState(sparseArray);
        b();
    }

    @Override // android.view.ViewGroup, android.view.View
    @RequiresApi(23)
    public CharSequence getAccessibilityClassName() {
        this.W.getClass();
        this.W.getClass();
        return "androidx.viewpager.widget.ViewPager";
    }

    @Nullable
    public d1 getAdapter() {
        return this.f4400j.getAdapter();
    }

    public int getCurrentItem() {
        return this.f4394d;
    }

    public int getItemDecorationCount() {
        return this.f4400j.getItemDecorationCount();
    }

    public int getOffscreenPageLimit() {
        return this.V;
    }

    public int getOrientation() {
        return this.f4397g.getOrientation() == 1 ? 1 : 0;
    }

    public int getPageSize() {
        int height;
        int paddingBottom;
        RecyclerView recyclerView = this.f4400j;
        if (getOrientation() == 0) {
            height = recyclerView.getWidth() - recyclerView.getPaddingLeft();
            paddingBottom = recyclerView.getPaddingRight();
        } else {
            height = recyclerView.getHeight() - recyclerView.getPaddingTop();
            paddingBottom = recyclerView.getPaddingBottom();
        }
        return height - paddingBottom;
    }

    public int getScrollState() {
        return this.f4402l.f36775f;
    }

    @Override // android.view.View
    public final void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo accessibilityNodeInfo) {
        int itemCount;
        int itemCount2;
        int itemCount3;
        super.onInitializeAccessibilityNodeInfo(accessibilityNodeInfo);
        n nVar = this.W;
        nVar.getClass();
        q0.f fVar = new q0.f(accessibilityNodeInfo);
        ViewPager2 viewPager2 = nVar.f36793d;
        if (viewPager2.getAdapter() == null) {
            itemCount = 0;
            itemCount2 = 0;
        } else if (viewPager2.getOrientation() == 1) {
            itemCount = viewPager2.getAdapter().getItemCount();
            itemCount2 = 1;
        } else {
            itemCount2 = viewPager2.getAdapter().getItemCount();
            itemCount = 1;
        }
        fVar.k(a0.y(itemCount, itemCount2, 0, false));
        d1 adapter = viewPager2.getAdapter();
        if (adapter == null || (itemCount3 = adapter.getItemCount()) == 0 || !viewPager2.U) {
            return;
        }
        if (viewPager2.f4394d > 0) {
            fVar.a(8192);
        }
        if (viewPager2.f4394d < itemCount3 - 1) {
            fVar.a(4096);
        }
        fVar.n(true);
    }

    @Override // android.view.ViewGroup, android.view.View
    public final void onLayout(boolean z10, int i10, int i11, int i12, int i13) {
        int measuredWidth = this.f4400j.getMeasuredWidth();
        int measuredHeight = this.f4400j.getMeasuredHeight();
        int paddingLeft = getPaddingLeft();
        Rect rect = this.f4391a;
        rect.left = paddingLeft;
        rect.right = (i12 - i10) - getPaddingRight();
        rect.top = getPaddingTop();
        rect.bottom = (i13 - i11) - getPaddingBottom();
        Rect rect2 = this.f4392b;
        Gravity.apply(8388659, measuredWidth, measuredHeight, rect, rect2);
        this.f4400j.layout(rect2.left, rect2.top, rect2.right, rect2.bottom);
        if (this.f4395e) {
            d();
        }
    }

    @Override // android.view.View
    public final void onMeasure(int i10, int i11) {
        measureChild(this.f4400j, i10, i11);
        int measuredWidth = this.f4400j.getMeasuredWidth();
        int measuredHeight = this.f4400j.getMeasuredHeight();
        int measuredState = this.f4400j.getMeasuredState();
        int paddingRight = getPaddingRight() + getPaddingLeft() + measuredWidth;
        int paddingBottom = getPaddingBottom() + getPaddingTop() + measuredHeight;
        setMeasuredDimension(View.resolveSizeAndState(Math.max(paddingRight, getSuggestedMinimumWidth()), i10, measuredState), View.resolveSizeAndState(Math.max(paddingBottom, getSuggestedMinimumHeight()), i11, measuredState << 16));
    }

    @Override // android.view.View
    public final void onRestoreInstanceState(Parcelable parcelable) {
        if (!(parcelable instanceof r)) {
            super.onRestoreInstanceState(parcelable);
            return;
        }
        r rVar = (r) parcelable;
        super.onRestoreInstanceState(rVar.getSuperState());
        this.f4398h = rVar.f36797b;
        this.f4399i = rVar.f36798c;
    }

    @Override // android.view.View
    @Nullable
    public Parcelable onSaveInstanceState() {
        r rVar = new r(super.onSaveInstanceState());
        rVar.f36796a = this.f4400j.getId();
        int i10 = this.f4398h;
        if (i10 == -1) {
            i10 = this.f4394d;
        }
        rVar.f36797b = i10;
        Parcelable parcelable = this.f4399i;
        if (parcelable != null) {
            rVar.f36798c = parcelable;
        } else {
            this.f4400j.getAdapter();
        }
        return rVar;
    }

    @Override // android.view.ViewGroup
    public final void onViewAdded(View view) {
        throw new IllegalStateException("ViewPager2 does not support direct child views");
    }

    @Override // android.view.View
    public boolean performAccessibilityAction(int i10, @Nullable Bundle bundle) {
        this.W.getClass();
        if (i10 != 8192 && i10 != 4096) {
            return super.performAccessibilityAction(i10, bundle);
        }
        n nVar = this.W;
        nVar.getClass();
        if (i10 != 8192 && i10 != 4096) {
            throw new IllegalStateException();
        }
        ViewPager2 viewPager2 = nVar.f36793d;
        int currentItem = i10 == 8192 ? viewPager2.getCurrentItem() - 1 : viewPager2.getCurrentItem() + 1;
        if (viewPager2.U) {
            viewPager2.c(currentItem, true);
        }
        return true;
    }

    public void setAdapter(@Nullable d1 d1Var) {
        d1 adapter = this.f4400j.getAdapter();
        this.W.onDetachAdapter(adapter);
        unregisterCurrentItemDataSetTracker(adapter);
        this.f4400j.setAdapter(d1Var);
        this.f4394d = 0;
        b();
        this.W.onAttachAdapter(d1Var);
        registerCurrentItemDataSetTracker(d1Var);
    }

    public void setCurrentItem(int i10, boolean z10) {
        if (((d) this.Q.f36173c).f36782m) {
            throw new IllegalStateException("Cannot change current item when ViewPager2 is fake dragging");
        }
        c(i10, z10);
    }

    @Override // android.view.View
    public void setLayoutDirection(int i10) {
        super.setLayoutDirection(i10);
        this.W.b();
    }

    public void setOffscreenPageLimit(int i10) {
        if (i10 < 1 && i10 != -1) {
            throw new IllegalArgumentException("Offscreen page limit must be OFFSCREEN_PAGE_LIMIT_DEFAULT or a number > 0");
        }
        this.V = i10;
        this.f4400j.requestLayout();
    }

    public void setOrientation(int i10) {
        this.f4397g.setOrientation(i10);
        this.W.b();
    }

    public void setPageTransformer(@Nullable o oVar) {
        if (oVar != null) {
            if (!this.T) {
                this.S = this.f4400j.getItemAnimator();
                this.T = true;
            }
            this.f4400j.setItemAnimator(null);
        } else if (this.T) {
            this.f4400j.setItemAnimator(this.S);
            this.S = null;
            this.T = false;
        }
        this.R.getClass();
        if (oVar == null) {
            return;
        }
        this.R.setPageTransformer(oVar);
        this.R.getClass();
    }

    public void setUserInputEnabled(boolean z10) {
        this.U = z10;
        this.W.b();
    }

    public void setCurrentItem(int i10) {
        setCurrentItem(i10, true);
    }

    public ViewPager2(@NonNull Context context, @Nullable AttributeSet attributeSet) {
        super(context, attributeSet);
        this.f4391a = new Rect();
        this.f4392b = new Rect();
        this.f4393c = new f();
        this.f4395e = false;
        this.f4396f = new e(this, 0);
        this.f4398h = -1;
        this.S = null;
        this.T = false;
        this.U = true;
        this.V = -1;
        a(context, attributeSet);
    }

    public ViewPager2(@NonNull Context context, @Nullable AttributeSet attributeSet, int i10) {
        super(context, attributeSet, i10);
        this.f4391a = new Rect();
        this.f4392b = new Rect();
        this.f4393c = new f();
        this.f4395e = false;
        this.f4396f = new e(this, 0);
        this.f4398h = -1;
        this.S = null;
        this.T = false;
        this.U = true;
        this.V = -1;
        a(context, attributeSet);
    }

    @RequiresApi(21)
    @SuppressLint({"ClassVerificationFailure"})
    public ViewPager2(@NonNull Context context, @Nullable AttributeSet attributeSet, int i10, int i11) {
        super(context, attributeSet, i10, i11);
        this.f4391a = new Rect();
        this.f4392b = new Rect();
        this.f4393c = new f();
        this.f4395e = false;
        this.f4396f = new e(this, 0);
        this.f4398h = -1;
        this.S = null;
        this.T = false;
        this.U = true;
        this.V = -1;
        a(context, attributeSet);
    }
}
