package m4;

import C0.C0109j;
import a.AbstractC0872a;
import java.io.InputStream;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Logger;

/* JADX INFO: loaded from: classes3.dex */
public final class k2 implements InterfaceC2553w {

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public static final i2 f21085d = new i2(16);

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final /* synthetic */ int f21086a;

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

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

    public /* synthetic */ k2(int i6, Object obj, Object obj2) {
        this.f21086a = i6;
        this.f21088c = obj;
        this.f21087b = obj2;
    }

    @Override // m4.InterfaceC2553w
    public void a(l2.c cVar) {
        switch (this.f21086a) {
            case 2:
                ((InterfaceC2553w) this.f21087b).a(cVar);
                break;
            default:
                M1 m12 = ((H0) this.f21088c).f20625o;
                A4.g.t(m12.f20686f != null, "Headers should be received prior to messages.");
                if (m12.f20686f == ((P1) this.f21087b)) {
                    ((H0) this.f21088c).f20614c.execute(new E(23, this, cVar));
                } else {
                    Logger logger = AbstractC2504f0.f20995a;
                    while (true) {
                        InputStream inputStreamG = cVar.g();
                        if (inputStreamG != null) {
                            AbstractC2504f0.b(inputStreamG);
                        }
                    }
                }
                break;
        }
    }

    @Override // m4.InterfaceC2553w
    public void b(k4.r0 r0Var, EnumC2550v enumC2550v, k4.g0 g0Var) {
        boolean z6;
        E3.p pVar;
        H0 h0;
        C0109j c0109j;
        switch (this.f21086a) {
            case 2:
                C0.J j4 = ((C2534p0) this.f21088c).f21129b.f21135b;
                if (r0Var.e()) {
                    ((C0) j4.f535d).a();
                } else {
                    ((C0) j4.f536e).a();
                }
                ((InterfaceC2553w) this.f21087b).b(r0Var, enumC2550v, g0Var);
                return;
            default:
                synchronized (((H0) this.f21088c).f20620i) {
                    H0 h02 = (H0) this.f21088c;
                    h02.f20625o = h02.f20625o.d((P1) this.f21087b);
                    ((H0) this.f21088c).f20624n.f2658b.add(String.valueOf(r0Var.f19949a));
                    break;
                }
                if (((H0) this.f21088c).f20628r.decrementAndGet() == Integer.MIN_VALUE) {
                    ((H0) this.f21088c).f20614c.execute(new O1(this, 0));
                    return;
                }
                P1 p12 = (P1) this.f21087b;
                if (p12.f20724c) {
                    H0 h03 = (H0) this.f21088c;
                    C1 c1P = h03.p(p12);
                    if (c1P != null) {
                        h03.f20613b.execute(c1P);
                    }
                    if (((H0) this.f21088c).f20625o.f20686f == ((P1) this.f21087b)) {
                        ((H0) this.f21088c).v(r0Var, enumC2550v, g0Var);
                        return;
                    }
                    return;
                }
                EnumC2550v enumC2550v2 = EnumC2550v.f21204d;
                if (enumC2550v == enumC2550v2 && ((H0) this.f21088c).f20627q.incrementAndGet() > 1000) {
                    H0 h04 = (H0) this.f21088c;
                    C1 c1P2 = h04.p((P1) this.f21087b);
                    if (c1P2 != null) {
                        h04.f20613b.execute(c1P2);
                    }
                    if (((H0) this.f21088c).f20625o.f20686f == ((P1) this.f21087b)) {
                        ((H0) this.f21088c).v(k4.r0.f19945m.g("Too many transparent retries. Might be a bug in gRPC").f(new k4.t0(r0Var)), enumC2550v, g0Var);
                        return;
                    }
                    return;
                }
                if (((H0) this.f21088c).f20625o.f20686f == null) {
                    if (enumC2550v == enumC2550v2 || (enumC2550v == EnumC2550v.f21202b && ((H0) this.f21088c).f20626p.compareAndSet(false, true))) {
                        P1 p1Q = ((H0) this.f21088c).q(((P1) this.f21087b).f20725d, true);
                        if (p1Q == null) {
                            return;
                        }
                        H0 h05 = (H0) this.f21088c;
                        if (h05.f20619h) {
                            synchronized (h05.f20620i) {
                                H0 h06 = (H0) this.f21088c;
                                h06.f20625o = h06.f20625o.c((P1) this.f21087b, p1Q);
                                break;
                            }
                        }
                        ((H0) this.f21088c).f20613b.execute(new N1(this, p1Q, 1));
                        return;
                    }
                    if (enumC2550v != EnumC2550v.f21203c) {
                        ((H0) this.f21088c).f20626p.set(true);
                        H0 h07 = (H0) this.f21088c;
                        Integer numValueOf = null;
                        if (!h07.f20619h) {
                            R1 r12 = h07.f20617f;
                            long nanos = 0;
                            if (r12 == null) {
                                pVar = new E3.p(0L, false);
                            } else {
                                boolean zContains = r12.f20755f.contains(r0Var.f19949a);
                                String str = (String) g0Var.c(H0.f20605F);
                                if (str != null) {
                                    try {
                                        numValueOf = Integer.valueOf(str);
                                    } catch (NumberFormatException unused) {
                                        numValueOf = -1;
                                    }
                                }
                                boolean z7 = (h07.f20623m == null || (!zContains && (numValueOf == null || numValueOf.intValue() >= 0))) ? false : !h07.f20623m.a();
                                if (h07.f20617f.f20750a <= ((P1) this.f21087b).f20725d + 1 || z7) {
                                    z6 = false;
                                    pVar = new E3.p(nanos, z6);
                                } else {
                                    if (numValueOf == null) {
                                        if (zContains) {
                                            nanos = (long) (H0.f20607H.nextDouble() * h07.f20634x);
                                            double d6 = h07.f20634x;
                                            R1 r13 = h07.f20617f;
                                            h07.f20634x = Math.min((long) (d6 * r13.f20753d), r13.f20752c);
                                            z6 = true;
                                        }
                                        z6 = false;
                                    } else {
                                        if (numValueOf.intValue() >= 0) {
                                            nanos = TimeUnit.MILLISECONDS.toNanos(numValueOf.intValue());
                                            h07.f20634x = h07.f20617f.f20751b;
                                            z6 = true;
                                        }
                                        z6 = false;
                                    }
                                    pVar = new E3.p(nanos, z6);
                                }
                            }
                            if (pVar.f1167b) {
                                P1 p1Q2 = ((H0) this.f21088c).q(((P1) this.f21087b).f20725d + 1, false);
                                if (p1Q2 == null) {
                                    return;
                                }
                                synchronized (((H0) this.f21088c).f20620i) {
                                    h0 = (H0) this.f21088c;
                                    c0109j = new C0109j(h0.f20620i);
                                    h0.f20632v = c0109j;
                                    break;
                                }
                                c0109j.f(h0.f20615d.schedule(new N1(this, p1Q2, 0), pVar.f1168c, TimeUnit.NANOSECONDS));
                                return;
                            }
                            break;
                        } else {
                            String str2 = (String) g0Var.c(H0.f20605F);
                            if (str2 != null) {
                                try {
                                    numValueOf = Integer.valueOf(str2);
                                } catch (NumberFormatException unused2) {
                                    numValueOf = -1;
                                }
                            }
                            H0 h08 = (H0) this.f21088c;
                            boolean zContains2 = h08.f20618g.f21046c.contains(r0Var.f19949a);
                            boolean z8 = (h08.f20623m == null || (!zContains2 && (numValueOf == null || numValueOf.intValue() >= 0))) ? false : !h08.f20623m.a();
                            if (zContains2 && !z8 && !r0Var.e() && numValueOf != null && numValueOf.intValue() > 0) {
                                numValueOf = 0;
                            }
                            boolean z9 = zContains2 && !z8;
                            if (z9) {
                                H0.o((H0) this.f21088c, numValueOf);
                            }
                            synchronized (((H0) this.f21088c).f20620i) {
                                try {
                                    H0 h09 = (H0) this.f21088c;
                                    h09.f20625o = h09.f20625o.b((P1) this.f21087b);
                                    if (z9) {
                                        H0 h010 = (H0) this.f21088c;
                                        if (!h010.u(h010.f20625o)) {
                                            if (!((H0) this.f21088c).f20625o.f20684d.isEmpty()) {
                                            }
                                        }
                                        return;
                                    }
                                } finally {
                                }
                            }
                        }
                    } else {
                        H0 h011 = (H0) this.f21088c;
                        if (h011.f20619h) {
                            h011.t();
                        }
                    }
                    break;
                }
                H0 h012 = (H0) this.f21088c;
                C1 c1P3 = h012.p((P1) this.f21087b);
                if (c1P3 != null) {
                    h012.f20613b.execute(c1P3);
                }
                if (((H0) this.f21088c).f20625o.f20686f == ((P1) this.f21087b)) {
                    ((H0) this.f21088c).v(r0Var, enumC2550v, g0Var);
                    return;
                }
                return;
        }
    }

    @Override // m4.InterfaceC2553w
    public void d() {
        switch (this.f21086a) {
            case 2:
                ((InterfaceC2553w) this.f21087b).d();
                break;
            default:
                H0 h0 = (H0) this.f21088c;
                if (h0.isReady()) {
                    h0.f20614c.execute(new O1(this, 1));
                    break;
                }
                break;
        }
    }

    @Override // m4.InterfaceC2553w
    public void e(k4.g0 g0Var) {
        AtomicInteger atomicInteger;
        int i6;
        int i7;
        switch (this.f21086a) {
            case 2:
                ((InterfaceC2553w) this.f21087b).e(g0Var);
                break;
            default:
                if (((P1) this.f21087b).f20725d > 0) {
                    k4.b0 b0Var = H0.f20604E;
                    g0Var.a(b0Var);
                    g0Var.f(b0Var, String.valueOf(((P1) this.f21087b).f20725d));
                }
                H0 h0 = (H0) this.f21088c;
                P1 p12 = (P1) this.f21087b;
                k4.b0 b0Var2 = H0.f20604E;
                C1 c1P = h0.p(p12);
                if (c1P != null) {
                    h0.f20613b.execute(c1P);
                }
                if (((H0) this.f21088c).f20625o.f20686f == ((P1) this.f21087b)) {
                    Q1 q12 = ((H0) this.f21088c).f20623m;
                    if (q12 != null) {
                        do {
                            atomicInteger = q12.f20739d;
                            i6 = atomicInteger.get();
                            i7 = q12.f20736a;
                            if (i6 == i7) {
                            }
                        } while (!atomicInteger.compareAndSet(i6, Math.min(q12.f20738c + i6, i7)));
                    }
                    ((H0) this.f21088c).f20614c.execute(new E(22, this, g0Var));
                }
                break;
        }
    }

    public String toString() {
        switch (this.f21086a) {
            case 2:
                E3.j jVarI0 = AbstractC0872a.i0(this);
                jVarI0.a((InterfaceC2553w) this.f21087b, "delegate");
                return jVarI0.toString();
            default:
                return super.toString();
        }
    }

    public k2() {
        this.f21086a = 0;
        i2 i2Var = i2.f21060b;
        this.f21088c = c2.o();
        this.f21087b = i2Var;
    }

    public k2(String str) {
        this.f21086a = 1;
        k4.V vA = k4.V.a();
        A4.g.o(vA, "registry");
        this.f21087b = vA;
        A4.g.o(str, "defaultPolicy");
        this.f21088c = str;
    }
}
