package y5;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Parcelable;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.FrameLayout;
import androidx.collection.s;
import androidx.core.view.s0;
import androidx.fragment.app.f;
import androidx.fragment.app.k0;
import androidx.fragment.app.z;
import androidx.lifecycle.n;
import androidx.lifecycle.r;
import androidx.lifecycle.u;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;
import java.util.Iterator;
import s3.i;

/* loaded from: classes.dex */
public abstract class a extends RecyclerView.h implements y5.c {

    /* renamed from: d, reason: collision with root package name */
    final n f51990d;

    /* renamed from: e, reason: collision with root package name */
    final z f51991e;

    /* renamed from: i, reason: collision with root package name */
    private g f51995i;

    /* renamed from: f, reason: collision with root package name */
    final s f51992f = new s();

    /* renamed from: g, reason: collision with root package name */
    private final s f51993g = new s();

    /* renamed from: h, reason: collision with root package name */
    private final s f51994h = new s();

    /* renamed from: j, reason: collision with root package name */
    boolean f51996j = false;

    /* renamed from: k, reason: collision with root package name */
    private boolean f51997k = false;

    /* JADX INFO: Access modifiers changed from: package-private */
    /* renamed from: y5.a$a, reason: collision with other inner class name */
    /* loaded from: classes.dex */
    public class ViewOnLayoutChangeListenerC1037a implements View.OnLayoutChangeListener {

        /* renamed from: a, reason: collision with root package name */
        final /* synthetic */ FrameLayout f51998a;

        /* renamed from: b, reason: collision with root package name */
        final /* synthetic */ y5.b f51999b;

        ViewOnLayoutChangeListenerC1037a(FrameLayout frameLayout, y5.b bVar) {
            this.f51998a = frameLayout;
            this.f51999b = bVar;
        }

        @Override // android.view.View.OnLayoutChangeListener
        public void onLayoutChange(View view, int i10, int i11, int i12, int i13, int i14, int i15, int i16, int i17) {
            if (this.f51998a.getParent() != null) {
                this.f51998a.removeOnLayoutChangeListener(this);
                a.this.U(this.f51999b);
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes.dex */
    public class b implements r {

        /* renamed from: p, reason: collision with root package name */
        final /* synthetic */ y5.b f52001p;

        b(y5.b bVar) {
            this.f52001p = bVar;
        }

        @Override // androidx.lifecycle.r
        public void f(u uVar, n.a aVar) {
            if (a.this.Y()) {
                return;
            }
            uVar.getLifecycle().d(this);
            if (s0.P(this.f52001p.P())) {
                a.this.U(this.f52001p);
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes.dex */
    public class c extends z.l {

        /* renamed from: a, reason: collision with root package name */
        final /* synthetic */ androidx.fragment.app.f f52003a;

        /* renamed from: b, reason: collision with root package name */
        final /* synthetic */ FrameLayout f52004b;

        c(androidx.fragment.app.f fVar, FrameLayout frameLayout) {
            this.f52003a = fVar;
            this.f52004b = frameLayout;
        }

        @Override // androidx.fragment.app.z.l
        public void onFragmentViewCreated(z zVar, androidx.fragment.app.f fVar, View view, Bundle bundle) {
            if (fVar == this.f52003a) {
                zVar.L1(this);
                a.this.F(view, this.f52004b);
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes.dex */
    public class d implements Runnable {
        d() {
        }

        @Override // java.lang.Runnable
        public void run() {
            a aVar = a.this;
            aVar.f51996j = false;
            aVar.K();
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes.dex */
    public class e implements r {

        /* renamed from: p, reason: collision with root package name */
        final /* synthetic */ Handler f52007p;

        /* renamed from: q, reason: collision with root package name */
        final /* synthetic */ Runnable f52008q;

        e(Handler handler, Runnable runnable) {
            this.f52007p = handler;
            this.f52008q = runnable;
        }

        @Override // androidx.lifecycle.r
        public void f(u uVar, n.a aVar) {
            if (aVar == n.a.ON_DESTROY) {
                this.f52007p.removeCallbacks(this.f52008q);
                uVar.getLifecycle().d(this);
            }
        }
    }

    /* loaded from: classes.dex */
    private static abstract class f extends RecyclerView.j {
        private f() {
        }

        @Override // androidx.recyclerview.widget.RecyclerView.j
        public abstract void a();

        @Override // androidx.recyclerview.widget.RecyclerView.j
        public final void b(int i10, int i11, Object obj) {
            a();
        }

        @Override // androidx.recyclerview.widget.RecyclerView.j
        public final void c(int i10, int i11) {
            a();
        }

        @Override // androidx.recyclerview.widget.RecyclerView.j
        public final void d(int i10, int i11, int i12) {
            a();
        }

        @Override // androidx.recyclerview.widget.RecyclerView.j
        public final void e(int i10, int i11) {
            a();
        }

        /* synthetic */ f(ViewOnLayoutChangeListenerC1037a viewOnLayoutChangeListenerC1037a) {
            this();
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes.dex */
    public class g {

        /* renamed from: a, reason: collision with root package name */
        private ViewPager2.i f52010a;

        /* renamed from: b, reason: collision with root package name */
        private RecyclerView.j f52011b;

        /* renamed from: c, reason: collision with root package name */
        private r f52012c;

        /* renamed from: d, reason: collision with root package name */
        private ViewPager2 f52013d;

        /* renamed from: e, reason: collision with root package name */
        private long f52014e = -1;

        /* JADX INFO: Access modifiers changed from: package-private */
        /* renamed from: y5.a$g$a, reason: collision with other inner class name */
        /* loaded from: classes.dex */
        public class C1038a extends ViewPager2.i {
            C1038a() {
            }

            @Override // androidx.viewpager2.widget.ViewPager2.i
            public void a(int i10) {
                g.this.d(false);
            }

            @Override // androidx.viewpager2.widget.ViewPager2.i
            public void c(int i10) {
                g.this.d(false);
            }
        }

        /* JADX INFO: Access modifiers changed from: package-private */
        /* loaded from: classes.dex */
        public class b extends f {
            b() {
                super(null);
            }

            @Override // y5.a.f, androidx.recyclerview.widget.RecyclerView.j
            public void a() {
                g.this.d(true);
            }
        }

        /* JADX INFO: Access modifiers changed from: package-private */
        /* loaded from: classes.dex */
        public class c implements r {
            c() {
            }

            @Override // androidx.lifecycle.r
            public void f(u uVar, n.a aVar) {
                g.this.d(false);
            }
        }

        g() {
        }

        private ViewPager2 a(RecyclerView recyclerView) {
            ViewParent parent = recyclerView.getParent();
            if (parent instanceof ViewPager2) {
                return (ViewPager2) parent;
            }
            throw new IllegalStateException("Expected ViewPager2 instance. Got: " + parent);
        }

        void b(RecyclerView recyclerView) {
            this.f52013d = a(recyclerView);
            C1038a c1038a = new C1038a();
            this.f52010a = c1038a;
            this.f52013d.g(c1038a);
            b bVar = new b();
            this.f52011b = bVar;
            a.this.C(bVar);
            c cVar = new c();
            this.f52012c = cVar;
            a.this.f51990d.a(cVar);
        }

        void c(RecyclerView recyclerView) {
            a(recyclerView).n(this.f52010a);
            a.this.E(this.f52011b);
            a.this.f51990d.d(this.f52012c);
            this.f52013d = null;
        }

        void d(boolean z10) {
            int currentItem;
            androidx.fragment.app.f fVar;
            if (a.this.Y() || this.f52013d.getScrollState() != 0 || a.this.f51992f.h() || a.this.g() == 0 || (currentItem = this.f52013d.getCurrentItem()) >= a.this.g()) {
                return;
            }
            long h10 = a.this.h(currentItem);
            if ((h10 != this.f52014e || z10) && (fVar = (androidx.fragment.app.f) a.this.f51992f.d(h10)) != null && fVar.isAdded()) {
                this.f52014e = h10;
                k0 p10 = a.this.f51991e.p();
                androidx.fragment.app.f fVar2 = null;
                for (int i10 = 0; i10 < a.this.f51992f.n(); i10++) {
                    long i11 = a.this.f51992f.i(i10);
                    androidx.fragment.app.f fVar3 = (androidx.fragment.app.f) a.this.f51992f.o(i10);
                    if (fVar3.isAdded()) {
                        if (i11 != this.f52014e) {
                            p10.v(fVar3, n.b.STARTED);
                        } else {
                            fVar2 = fVar3;
                        }
                        fVar3.setMenuVisibility(i11 == this.f52014e);
                    }
                }
                if (fVar2 != null) {
                    p10.v(fVar2, n.b.RESUMED);
                }
                if (p10.q()) {
                    return;
                }
                p10.k();
            }
        }
    }

    public a(z zVar, n nVar) {
        this.f51991e = zVar;
        this.f51990d = nVar;
        super.D(true);
    }

    private static String I(String str, long j10) {
        return str + j10;
    }

    private void J(int i10) {
        long h10 = h(i10);
        if (this.f51992f.c(h10)) {
            return;
        }
        androidx.fragment.app.f H = H(i10);
        H.setInitialSavedState((f.o) this.f51993g.d(h10));
        this.f51992f.j(h10, H);
    }

    private boolean L(long j10) {
        View view;
        if (this.f51994h.c(j10)) {
            return true;
        }
        androidx.fragment.app.f fVar = (androidx.fragment.app.f) this.f51992f.d(j10);
        return (fVar == null || (view = fVar.getView()) == null || view.getParent() == null) ? false : true;
    }

    private static boolean M(String str, String str2) {
        return str.startsWith(str2) && str.length() > str2.length();
    }

    private Long N(int i10) {
        Long l10 = null;
        for (int i11 = 0; i11 < this.f51994h.n(); i11++) {
            if (((Integer) this.f51994h.o(i11)).intValue() == i10) {
                if (l10 != null) {
                    throw new IllegalStateException("Design assumption violated: a ViewHolder can only be bound to one item at a time.");
                }
                l10 = Long.valueOf(this.f51994h.i(i11));
            }
        }
        return l10;
    }

    private static long T(String str, String str2) {
        return Long.parseLong(str.substring(str2.length()));
    }

    private void V(long j10) {
        ViewParent parent;
        androidx.fragment.app.f fVar = (androidx.fragment.app.f) this.f51992f.d(j10);
        if (fVar == null) {
            return;
        }
        if (fVar.getView() != null && (parent = fVar.getView().getParent()) != null) {
            ((FrameLayout) parent).removeAllViews();
        }
        if (!G(j10)) {
            this.f51993g.k(j10);
        }
        if (!fVar.isAdded()) {
            this.f51992f.k(j10);
            return;
        }
        if (Y()) {
            this.f51997k = true;
            return;
        }
        if (fVar.isAdded() && G(j10)) {
            this.f51993g.j(j10, this.f51991e.A1(fVar));
        }
        this.f51991e.p().r(fVar).k();
        this.f51992f.k(j10);
    }

    private void W() {
        Handler handler = new Handler(Looper.getMainLooper());
        d dVar = new d();
        this.f51990d.a(new e(handler, dVar));
        handler.postDelayed(dVar, 10000L);
    }

    private void X(androidx.fragment.app.f fVar, FrameLayout frameLayout) {
        this.f51991e.o1(new c(fVar, frameLayout), false);
    }

    void F(View view, FrameLayout frameLayout) {
        if (frameLayout.getChildCount() > 1) {
            throw new IllegalStateException("Design assumption violated.");
        }
        if (view.getParent() == frameLayout) {
            return;
        }
        if (frameLayout.getChildCount() > 0) {
            frameLayout.removeAllViews();
        }
        if (view.getParent() != null) {
            ((ViewGroup) view.getParent()).removeView(view);
        }
        frameLayout.addView(view);
    }

    public boolean G(long j10) {
        return j10 >= 0 && j10 < ((long) g());
    }

    public abstract androidx.fragment.app.f H(int i10);

    void K() {
        if (!this.f51997k || Y()) {
            return;
        }
        androidx.collection.b bVar = new androidx.collection.b();
        for (int i10 = 0; i10 < this.f51992f.n(); i10++) {
            long i11 = this.f51992f.i(i10);
            if (!G(i11)) {
                bVar.add(Long.valueOf(i11));
                this.f51994h.k(i11);
            }
        }
        if (!this.f51996j) {
            this.f51997k = false;
            for (int i12 = 0; i12 < this.f51992f.n(); i12++) {
                long i13 = this.f51992f.i(i12);
                if (!L(i13)) {
                    bVar.add(Long.valueOf(i13));
                }
            }
        }
        Iterator it = bVar.iterator();
        while (it.hasNext()) {
            V(((Long) it.next()).longValue());
        }
    }

    @Override // androidx.recyclerview.widget.RecyclerView.h
    /* renamed from: O, reason: merged with bridge method [inline-methods] */
    public final void u(y5.b bVar, int i10) {
        long m10 = bVar.m();
        int id2 = bVar.P().getId();
        Long N = N(id2);
        if (N != null && N.longValue() != m10) {
            V(N.longValue());
            this.f51994h.k(N.longValue());
        }
        this.f51994h.j(m10, Integer.valueOf(id2));
        J(i10);
        FrameLayout P = bVar.P();
        if (s0.P(P)) {
            if (P.getParent() != null) {
                throw new IllegalStateException("Design assumption violated.");
            }
            P.addOnLayoutChangeListener(new ViewOnLayoutChangeListenerC1037a(P, bVar));
        }
        K();
    }

    @Override // androidx.recyclerview.widget.RecyclerView.h
    /* renamed from: P, reason: merged with bridge method [inline-methods] */
    public final y5.b w(ViewGroup viewGroup, int i10) {
        return y5.b.O(viewGroup);
    }

    @Override // androidx.recyclerview.widget.RecyclerView.h
    /* renamed from: Q, reason: merged with bridge method [inline-methods] */
    public final boolean y(y5.b bVar) {
        return true;
    }

    @Override // androidx.recyclerview.widget.RecyclerView.h
    /* renamed from: R, reason: merged with bridge method [inline-methods] */
    public final void z(y5.b bVar) {
        U(bVar);
        K();
    }

    @Override // androidx.recyclerview.widget.RecyclerView.h
    /* renamed from: S, reason: merged with bridge method [inline-methods] */
    public final void B(y5.b bVar) {
        Long N = N(bVar.P().getId());
        if (N != null) {
            V(N.longValue());
            this.f51994h.k(N.longValue());
        }
    }

    void U(y5.b bVar) {
        androidx.fragment.app.f fVar = (androidx.fragment.app.f) this.f51992f.d(bVar.m());
        if (fVar == null) {
            throw new IllegalStateException("Design assumption violated.");
        }
        FrameLayout P = bVar.P();
        View view = fVar.getView();
        if (!fVar.isAdded() && view != null) {
            throw new IllegalStateException("Design assumption violated.");
        }
        if (fVar.isAdded() && view == null) {
            X(fVar, P);
            return;
        }
        if (fVar.isAdded() && view.getParent() != null) {
            if (view.getParent() != P) {
                F(view, P);
                return;
            }
            return;
        }
        if (fVar.isAdded()) {
            F(view, P);
            return;
        }
        if (Y()) {
            if (this.f51991e.M0()) {
                return;
            }
            this.f51990d.a(new b(bVar));
            return;
        }
        X(fVar, P);
        this.f51991e.p().e(fVar, "f" + bVar.m()).v(fVar, n.b.STARTED).k();
        this.f51995i.d(false);
    }

    boolean Y() {
        return this.f51991e.T0();
    }

    @Override // y5.c
    public final Parcelable a() {
        Bundle bundle = new Bundle(this.f51992f.n() + this.f51993g.n());
        for (int i10 = 0; i10 < this.f51992f.n(); i10++) {
            long i11 = this.f51992f.i(i10);
            androidx.fragment.app.f fVar = (androidx.fragment.app.f) this.f51992f.d(i11);
            if (fVar != null && fVar.isAdded()) {
                this.f51991e.n1(bundle, I("f#", i11), fVar);
            }
        }
        for (int i12 = 0; i12 < this.f51993g.n(); i12++) {
            long i13 = this.f51993g.i(i12);
            if (G(i13)) {
                bundle.putParcelable(I("s#", i13), (Parcelable) this.f51993g.d(i13));
            }
        }
        return bundle;
    }

    @Override // y5.c
    public final void b(Parcelable parcelable) {
        if (!this.f51993g.h() || !this.f51992f.h()) {
            throw new IllegalStateException("Expected the adapter to be 'fresh' while restoring state.");
        }
        Bundle bundle = (Bundle) parcelable;
        if (bundle.getClassLoader() == null) {
            bundle.setClassLoader(getClass().getClassLoader());
        }
        for (String str : bundle.keySet()) {
            if (M(str, "f#")) {
                this.f51992f.j(T(str, "f#"), this.f51991e.v0(bundle, str));
            } else {
                if (!M(str, "s#")) {
                    throw new IllegalArgumentException("Unexpected key in savedState: " + str);
                }
                long T = T(str, "s#");
                f.o oVar = (f.o) bundle.getParcelable(str);
                if (G(T)) {
                    this.f51993g.j(T, oVar);
                }
            }
        }
        if (this.f51992f.h()) {
            return;
        }
        this.f51997k = true;
        this.f51996j = true;
        K();
        W();
    }

    @Override // androidx.recyclerview.widget.RecyclerView.h
    public long h(int i10) {
        return i10;
    }

    @Override // androidx.recyclerview.widget.RecyclerView.h
    public void t(RecyclerView recyclerView) {
        i.a(this.f51995i == null);
        g gVar = new g();
        this.f51995i = gVar;
        gVar.b(recyclerView);
    }

    @Override // androidx.recyclerview.widget.RecyclerView.h
    public void x(RecyclerView recyclerView) {
        this.f51995i.c(recyclerView);
        this.f51995i = null;
    }
}
