package m4;

import I2.C0317a;
import a.AbstractC0872a;
import java.nio.charset.Charset;
import java.util.Locale;
import java.util.concurrent.CancellationException;
import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import k4.AbstractC2396i;
import k4.AbstractC2397j;
import k4.AbstractC2400m;
import k4.C2389b;
import k4.C2393f;
import k4.C2394g;
import k4.C2401n;
import k4.C2403p;
import k4.C2407u;
import k4.C2408v;
import k4.C2410x;
import u4.C2965a;

/* JADX INFO: renamed from: m4.t, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes3.dex */
public final class C2544t extends AbstractC2397j {

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public static final Logger f21151q = Logger.getLogger(C2544t.class.getName());

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public static final double f21152r;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final k4.i0 f21153a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final u4.c f21154b;

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

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final boolean f21156d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final C0.J f21157e;

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

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public volatile ScheduledFuture f21159g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final boolean f21160h;

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

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

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public volatile boolean f21163k;
    public boolean l;

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

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public final J3.v f21165n;

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    public final ScheduledExecutorService f21166o;

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    public C2410x f21167p = C2410x.f19979d;

    static {
        "gzip".getBytes(Charset.forName("US-ASCII"));
        f21152r = TimeUnit.SECONDS.toNanos(1L) * 1.0d;
    }

    public C2544t(k4.i0 i0Var, Executor executor, C2394g c2394g, J3.v vVar, ScheduledExecutorService scheduledExecutorService, C0.J j4) {
        C2403p c2403p = C2403p.f19908b;
        this.f21153a = i0Var;
        String str = i0Var.f19879b;
        System.identityHashCode(this);
        u4.b.f24588a.getClass();
        this.f21154b = C2965a.f24586a;
        if (executor == J3.r.f3539a) {
            this.f21155c = new W1();
            this.f21156d = true;
        } else {
            this.f21155c = new Z1(executor);
            this.f21156d = false;
        }
        this.f21157e = j4;
        this.f21158f = C2407u.b();
        k4.h0 h0Var = k4.h0.f19874a;
        k4.h0 h0Var2 = i0Var.f19878a;
        this.f21160h = h0Var2 == h0Var || h0Var2 == k4.h0.f19875b;
        this.f21161i = c2394g;
        this.f21165n = vVar;
        this.f21166o = scheduledExecutorService;
    }

    public final void a(String str, Throwable th) {
        if (str == null && th == null) {
            th = new CancellationException("Cancelled without a message or cause");
            f21151q.log(Level.WARNING, "Cancelling without a message or cause is suboptimal", th);
        }
        if (this.l) {
            return;
        }
        this.l = true;
        try {
            if (this.f21162j != null) {
                k4.r0 r0Var = k4.r0.f19939f;
                k4.r0 r0VarG = str != null ? r0Var.g(str) : r0Var.g("Call cancelled without message");
                if (th != null) {
                    r0VarG = r0VarG.f(th);
                }
                this.f21162j.g(r0VarG);
            }
            b();
        } catch (Throwable th2) {
            b();
            throw th2;
        }
    }

    public final void b() {
        this.f21158f.getClass();
        ScheduledFuture scheduledFuture = this.f21159g;
        if (scheduledFuture != null) {
            scheduledFuture.cancel(false);
        }
    }

    public final void c(Object obj) {
        A4.g.t(this.f21162j != null, "Not started");
        A4.g.t(!this.l, "call was cancelled");
        A4.g.t(!this.f21164m, "call was half-closed");
        try {
            InterfaceC2547u interfaceC2547u = this.f21162j;
            if (interfaceC2547u instanceof H0) {
                ((H0) interfaceC2547u).w(obj);
            } else {
                interfaceC2547u.f(this.f21153a.d(obj));
            }
            if (this.f21160h) {
                return;
            }
            this.f21162j.flush();
        } catch (Error e6) {
            this.f21162j.g(k4.r0.f19939f.g("Client sendMessage() failed with Error"));
            throw e6;
        } catch (RuntimeException e7) {
            this.f21162j.g(k4.r0.f19939f.f(e7).g("Failed to stream message"));
        }
    }

    @Override // k4.AbstractC2397j
    public final void cancel(String str, Throwable th) {
        u4.b.c();
        try {
            u4.b.a();
            a(str, th);
            u4.b.f24588a.getClass();
        } catch (Throwable th2) {
            try {
                u4.b.f24588a.getClass();
            } catch (Throwable th3) {
                th2.addSuppressed(th3);
            }
            throw th2;
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:22:0x008b  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void d(AbstractC2396i abstractC2396i, k4.g0 g0Var) {
        InterfaceC2547u h0;
        C2394g c2394g;
        A4.g.t(this.f21162j == null, "Already started");
        A4.g.t(!this.l, "call was cancelled");
        A4.g.o(abstractC2396i, "observer");
        A4.g.o(g0Var, "headers");
        this.f21158f.getClass();
        C2394g c2394g2 = this.f21161i;
        C0317a c0317a = W0.f20895g;
        W0 w02 = (W0) c2394g2.a(c0317a);
        if (w02 != null) {
            Long l = w02.f20896a;
            if (l != null) {
                long jLongValue = l.longValue();
                TimeUnit timeUnit = TimeUnit.NANOSECONDS;
                C2401n c2401n = C2408v.f19963d;
                if (timeUnit == null) {
                    throw new NullPointerException("units");
                }
                C2408v c2408v = new C2408v(timeUnit.toNanos(jLongValue));
                C2394g c2394g3 = this.f21161i;
                C2408v c2408v2 = c2394g3.f19858a;
                if (c2408v2 != null) {
                    C2401n c2401n2 = c2408v2.f19967a;
                    C2401n c2401n3 = c2408v.f19967a;
                    if (c2401n3 != c2401n2) {
                        throw new AssertionError("Tickers (" + c2401n3 + " and " + c2408v2.f19967a + ") don't match. Custom Ticker should only be used in tests!");
                    }
                    if (c2408v.f19968b - c2408v2.f19968b < 0) {
                        c2394g3.getClass();
                        C2393f c2393fB = C2394g.b(c2394g3);
                        c2393fB.f19849a = c2408v;
                        this.f21161i = new C2394g(c2393fB);
                    }
                }
            }
            Boolean bool = w02.f20897b;
            if (bool != null) {
                if (bool.booleanValue()) {
                    C2394g c2394g4 = this.f21161i;
                    c2394g4.getClass();
                    C2393f c2393fB2 = C2394g.b(c2394g4);
                    c2393fB2.f19854f = Boolean.TRUE;
                    c2394g = new C2394g(c2393fB2);
                } else {
                    C2394g c2394g5 = this.f21161i;
                    c2394g5.getClass();
                    C2393f c2393fB3 = C2394g.b(c2394g5);
                    c2393fB3.f19854f = Boolean.FALSE;
                    c2394g = new C2394g(c2393fB3);
                }
                this.f21161i = c2394g;
            }
            Integer num = w02.f20898c;
            if (num != null) {
                C2394g c2394g6 = this.f21161i;
                Integer num2 = c2394g6.f19864g;
                if (num2 != null) {
                    int iMin = Math.min(num2.intValue(), num.intValue());
                    A4.g.h(iMin, "invalid maxsize %s", iMin >= 0);
                    C2393f c2393fB4 = C2394g.b(c2394g6);
                    c2393fB4.f19855g = Integer.valueOf(iMin);
                    this.f21161i = new C2394g(c2393fB4);
                } else {
                    int iIntValue = num.intValue();
                    A4.g.h(iIntValue, "invalid maxsize %s", iIntValue >= 0);
                    C2393f c2393fB5 = C2394g.b(c2394g6);
                    c2393fB5.f19855g = num;
                    this.f21161i = new C2394g(c2393fB5);
                }
            }
            Integer num3 = w02.f20899d;
            if (num3 != null) {
                C2394g c2394g7 = this.f21161i;
                Integer num4 = c2394g7.f19865h;
                if (num4 != null) {
                    int iMin2 = Math.min(num4.intValue(), num3.intValue());
                    A4.g.h(iMin2, "invalid maxsize %s", iMin2 >= 0);
                    C2393f c2393fB6 = C2394g.b(c2394g7);
                    c2393fB6.f19856h = Integer.valueOf(iMin2);
                    this.f21161i = new C2394g(c2393fB6);
                } else {
                    int iIntValue2 = num3.intValue();
                    A4.g.h(iIntValue2, "invalid maxsize %s", iIntValue2 >= 0);
                    C2393f c2393fB7 = C2394g.b(c2394g7);
                    c2393fB7.f19856h = num3;
                    this.f21161i = new C2394g(c2393fB7);
                }
            }
        }
        this.f21161i.getClass();
        C2401n c2401n4 = C2401n.f19897b;
        C2410x c2410x = this.f21167p;
        g0Var.a(AbstractC2504f0.f21002h);
        g0Var.a(AbstractC2504f0.f20998d);
        k4.d0 d0Var = AbstractC2504f0.f20999e;
        g0Var.a(d0Var);
        byte[] bArr = c2410x.f19981b;
        if (bArr.length != 0) {
            g0Var.f(d0Var, bArr);
        }
        g0Var.a(AbstractC2504f0.f21000f);
        g0Var.a(AbstractC2504f0.f21001g);
        C2408v c2408v3 = this.f21161i.f19858a;
        this.f21158f.getClass();
        C2408v c2408v4 = c2408v3 == null ? null : c2408v3;
        if (c2408v4 == null || !c2408v4.a()) {
            this.f21158f.getClass();
            C2408v c2408v5 = this.f21161i.f19858a;
            Level level = Level.FINE;
            Logger logger = f21151q;
            if (logger.isLoggable(level) && c2408v4 != null && c2408v4.equals(null)) {
                TimeUnit timeUnit2 = TimeUnit.NANOSECONDS;
                long jMax = Math.max(0L, c2408v4.b());
                Locale locale = Locale.US;
                StringBuilder sb = new StringBuilder("Call timeout set to '" + jMax + "' ns, due to context deadline.");
                if (c2408v5 == null) {
                    sb.append(" Explicit call timeout was not set.");
                } else {
                    sb.append(" Explicit call timeout was '" + c2408v5.b() + "' ns.");
                }
                logger.fine(sb.toString());
            }
            J3.v vVar = this.f21165n;
            k4.i0 i0Var = this.f21153a;
            C2394g c2394g8 = this.f21161i;
            C2407u c2407u = this.f21158f;
            if (((S0) vVar.f3543a).X) {
                W0 w03 = (W0) c2394g8.a(c0317a);
                h0 = new H0(vVar, i0Var, g0Var, c2394g8, w03 == null ? null : w03.f20900e, w03 == null ? null : w03.f20901f, c2407u);
            } else {
                InterfaceC2556x interfaceC2556xB = vVar.b(new C2546t1(i0Var, g0Var, c2394g8));
                C2407u c2407uA = c2407u.a();
                try {
                    h0 = interfaceC2556xB.c(i0Var, g0Var, c2394g8, AbstractC2504f0.c(c2394g8, g0Var, 0, false));
                } finally {
                    c2407u.c(c2407uA);
                }
            }
            this.f21162j = h0;
        } else {
            AbstractC2400m[] abstractC2400mArrC = AbstractC2504f0.c(this.f21161i, g0Var, 0, false);
            C2408v c2408v6 = this.f21161i.f19858a;
            this.f21158f.getClass();
            String str = c2408v6 == null ? "Context" : "CallOptions";
            Long l6 = (Long) this.f21161i.a(AbstractC2400m.f19896a);
            TimeUnit timeUnit3 = TimeUnit.NANOSECONDS;
            double dB = c2408v4.b();
            double d6 = f21152r;
            this.f21162j = new X(k4.r0.f19941h.g(String.format("ClientCall started after %s deadline was exceeded %.9f seconds ago. Name resolution delay %.9f seconds.", str, Double.valueOf(dB / d6), Double.valueOf(l6 == null ? 0.0d : l6.longValue() / d6))), EnumC2550v.f21201a, abstractC2400mArrC);
        }
        if (this.f21156d) {
            this.f21162j.m();
        }
        this.f21161i.getClass();
        Integer num5 = this.f21161i.f19864g;
        if (num5 != null) {
            this.f21162j.j(num5.intValue());
        }
        Integer num6 = this.f21161i.f19865h;
        if (num6 != null) {
            this.f21162j.c(num6.intValue());
        }
        if (c2408v4 != null) {
            this.f21162j.n(c2408v4);
        }
        this.f21162j.d(c2401n4);
        this.f21162j.k(this.f21167p);
        C0.J j4 = this.f21157e;
        ((C0) j4.f534c).a();
        ((i2) j4.f533b).e();
        this.f21162j.l(new C4.f(this, abstractC2396i));
        this.f21158f.getClass();
        Logger logger2 = C2407u.f19957a;
        if (c2408v4 != null) {
            this.f21158f.getClass();
            if (!c2408v4.equals(null) && this.f21166o != null) {
                TimeUnit timeUnit4 = TimeUnit.NANOSECONDS;
                long jB = c2408v4.b();
                this.f21159g = this.f21166o.schedule(new B0(new RunnableC2541s(this, jB)), jB, timeUnit4);
            }
        }
        if (this.f21163k) {
            b();
        }
    }

    @Override // k4.AbstractC2397j
    public final C2389b getAttributes() {
        InterfaceC2547u interfaceC2547u = this.f21162j;
        return interfaceC2547u != null ? interfaceC2547u.b() : C2389b.f19841b;
    }

    @Override // k4.AbstractC2397j
    public final void halfClose() {
        u4.b.c();
        try {
            u4.b.a();
            A4.g.t(this.f21162j != null, "Not started");
            A4.g.t(!this.l, "call was cancelled");
            A4.g.t(!this.f21164m, "call already half-closed");
            this.f21164m = true;
            this.f21162j.h();
            u4.b.f24588a.getClass();
        } catch (Throwable th) {
            try {
                u4.b.f24588a.getClass();
            } catch (Throwable th2) {
                th.addSuppressed(th2);
            }
            throw th;
        }
    }

    @Override // k4.AbstractC2397j
    public final boolean isReady() {
        if (this.f21164m) {
            return false;
        }
        return this.f21162j.isReady();
    }

    @Override // k4.AbstractC2397j
    public final void request(int i6) {
        u4.b.c();
        try {
            u4.b.a();
            A4.g.t(this.f21162j != null, "Not started");
            A4.g.l(i6 >= 0, "Number requested must be non-negative");
            this.f21162j.i(i6);
            u4.b.f24588a.getClass();
        } catch (Throwable th) {
            try {
                u4.b.f24588a.getClass();
            } catch (Throwable th2) {
                th.addSuppressed(th2);
            }
            throw th;
        }
    }

    @Override // k4.AbstractC2397j
    public final void sendMessage(Object obj) {
        u4.b.c();
        try {
            u4.b.a();
            c(obj);
            u4.b.f24588a.getClass();
        } catch (Throwable th) {
            try {
                u4.b.f24588a.getClass();
            } catch (Throwable th2) {
                th.addSuppressed(th2);
            }
            throw th;
        }
    }

    @Override // k4.AbstractC2397j
    public final void setMessageCompression(boolean z6) {
        A4.g.t(this.f21162j != null, "Not started");
        this.f21162j.a(z6);
    }

    @Override // k4.AbstractC2397j
    public final void start(AbstractC2396i abstractC2396i, k4.g0 g0Var) {
        u4.b.c();
        try {
            u4.b.a();
            d(abstractC2396i, g0Var);
            u4.b.f24588a.getClass();
        } catch (Throwable th) {
            try {
                u4.b.f24588a.getClass();
            } catch (Throwable th2) {
                th.addSuppressed(th2);
            }
            throw th;
        }
    }

    public final String toString() {
        E3.j jVarI0 = AbstractC0872a.i0(this);
        jVarI0.a(this.f21153a, "method");
        return jVarI0.toString();
    }
}
