package m4;

import I2.C0317a;
import a.AbstractC0872a;
import com.google.android.gms.common.internal.ServiceSpecificExtraArgs;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import k4.AbstractC2396i;
import k4.AbstractC2397j;
import k4.AbstractC2400m;
import k4.C2389b;
import k4.C2394g;
import k4.C2407u;
import k4.C2408v;

/* JADX INFO: loaded from: classes3.dex */
public final class O0 extends AbstractC2397j {

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    public static final H f20699o;

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

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

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

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

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

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

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public k4.r0 f20706g;

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

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

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

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public final k4.i0 f20710k;
    public final C2394g l;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public final long f20711m;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public final /* synthetic */ P0 f20712n;

    static {
        Logger.getLogger(O0.class.getName());
        f20699o = new H(0);
    }

    public O0(P0 p02, C2407u c2407u, k4.i0 i0Var, C2394g c2394g) {
        ScheduledFuture<?> scheduledFutureSchedule;
        this.f20712n = p02;
        S0 s02 = p02.f20721d;
        Logger logger = S0.f20758d0;
        s02.getClass();
        Executor executor = c2394g.f19859b;
        executor = executor == null ? s02.f20796h : executor;
        S0 s03 = p02.f20721d;
        Q0 q02 = s03.f20795g;
        this.f20707h = new ArrayList();
        A4.g.o(executor, "callExecutor");
        this.f20701b = executor;
        A4.g.o(q02, "scheduler");
        C2407u c2407uB = C2407u.b();
        this.f20702c = c2407uB;
        c2407uB.getClass();
        C2408v c2408v = c2394g.f19858a;
        if (c2408v == null) {
            scheduledFutureSchedule = null;
        } else {
            TimeUnit timeUnit = TimeUnit.NANOSECONDS;
            long jB = c2408v.b();
            long jAbs = Math.abs(jB);
            TimeUnit timeUnit2 = TimeUnit.SECONDS;
            long nanos = jAbs / timeUnit2.toNanos(1L);
            long jAbs2 = Math.abs(jB) % timeUnit2.toNanos(1L);
            StringBuilder sb = new StringBuilder();
            if (jB < 0) {
                sb.append("ClientCall started after CallOptions deadline was exceeded. Deadline has been exceeded for ");
            } else {
                sb.append("Deadline CallOptions will be exceeded in ");
            }
            sb.append(nanos);
            sb.append(String.format(Locale.US, ".%09d", Long.valueOf(jAbs2)));
            sb.append("s. ");
            scheduledFutureSchedule = q02.f20735a.schedule(new E(0, this, sb), jB, timeUnit);
        }
        this.f20700a = scheduledFutureSchedule;
        this.f20709j = c2407u;
        this.f20710k = i0Var;
        this.l = c2394g;
        s03.Y.getClass();
        this.f20711m = System.nanoTime();
    }

    public final void a(k4.r0 r0Var, boolean z6) {
        AbstractC2396i abstractC2396i;
        synchronized (this) {
            try {
                AbstractC2397j abstractC2397j = this.f20705f;
                boolean z7 = true;
                if (abstractC2397j == null) {
                    H h5 = f20699o;
                    if (abstractC2397j != null) {
                        z7 = false;
                    }
                    A4.g.r(abstractC2397j, "realCall already set to %s", z7);
                    ScheduledFuture scheduledFuture = this.f20700a;
                    if (scheduledFuture != null) {
                        scheduledFuture.cancel(false);
                    }
                    this.f20705f = h5;
                    abstractC2396i = this.f20704e;
                    this.f20706g = r0Var;
                    z7 = false;
                } else if (z6) {
                    return;
                } else {
                    abstractC2396i = null;
                }
                if (z7) {
                    b(new E(1, this, r0Var));
                } else {
                    if (abstractC2396i != null) {
                        this.f20701b.execute(new I(this, abstractC2396i, r0Var));
                    }
                    c();
                }
                this.f20712n.f20721d.f20800m.execute(new G(this, 1));
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final void b(Runnable runnable) {
        synchronized (this) {
            try {
                if (this.f20703d) {
                    runnable.run();
                } else {
                    this.f20707h.add(runnable);
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    /* JADX WARN: Code restructure failed: missing block: B:15:0x002b, code lost:
    
        r0 = r1.iterator();
     */
    /* JADX WARN: Code restructure failed: missing block: B:17:0x0033, code lost:
    
        if (r0.hasNext() == false) goto L25;
     */
    /* JADX WARN: Code restructure failed: missing block: B:18:0x0035, code lost:
    
        ((java.lang.Runnable) r0.next()).run();
     */
    /* JADX WARN: Removed duplicated region for block: B:26:? A[RETURN, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:9:0x0019  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void c() {
        J j4;
        List list;
        List arrayList = new ArrayList();
        while (true) {
            synchronized (this) {
                if (this.f20707h.isEmpty()) {
                    break;
                }
                list = this.f20707h;
                this.f20707h = arrayList;
            }
            if (j4 == null) {
                this.f20701b.execute(new r(this, j4));
                return;
            }
            return;
            list.clear();
            arrayList = list;
        }
        this.f20707h = null;
        this.f20703d = true;
        j4 = this.f20708i;
        if (j4 == null) {
        }
    }

    @Override // k4.AbstractC2397j
    public final void cancel(String str, Throwable th) {
        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);
        }
        a(r0VarG, false);
    }

    public final void d() {
        r rVar;
        C2407u c2407uA = this.f20709j.a();
        try {
            C2394g c2394g = this.l;
            C0317a c0317a = AbstractC2400m.f19896a;
            this.f20712n.f20721d.Y.getClass();
            AbstractC2397j abstractC2397jS = this.f20712n.s(this.f20710k, c2394g.c(c0317a, Long.valueOf(System.nanoTime() - this.f20711m)));
            synchronized (this) {
                try {
                    AbstractC2397j abstractC2397j = this.f20705f;
                    if (abstractC2397j != null) {
                        rVar = null;
                    } else {
                        A4.g.r(abstractC2397j, "realCall already set to %s", abstractC2397j == null);
                        ScheduledFuture scheduledFuture = this.f20700a;
                        if (scheduledFuture != null) {
                            scheduledFuture.cancel(false);
                        }
                        this.f20705f = abstractC2397jS;
                        rVar = new r(this, this.f20702c);
                    }
                } finally {
                }
            }
            if (rVar == null) {
                this.f20712n.f20721d.f20800m.execute(new G(this, 1));
                return;
            }
            S0 s02 = this.f20712n.f20721d;
            C2394g c2394g2 = this.l;
            s02.getClass();
            Executor executor = c2394g2.f19859b;
            if (executor == null) {
                executor = s02.f20796h;
            }
            executor.execute(new E(19, this, rVar));
        } finally {
            this.f20709j.c(c2407uA);
        }
    }

    @Override // k4.AbstractC2397j
    public final C2389b getAttributes() {
        AbstractC2397j abstractC2397j;
        synchronized (this) {
            abstractC2397j = this.f20705f;
        }
        return abstractC2397j != null ? abstractC2397j.getAttributes() : C2389b.f19841b;
    }

    @Override // k4.AbstractC2397j
    public final void halfClose() {
        b(new G(this, 0));
    }

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

    @Override // k4.AbstractC2397j
    public final void request(int i6) {
        if (this.f20703d) {
            this.f20705f.request(i6);
        } else {
            b(new C1.b(i6, 4, this));
        }
    }

    @Override // k4.AbstractC2397j
    public final void sendMessage(Object obj) {
        if (this.f20703d) {
            this.f20705f.sendMessage(obj);
        } else {
            b(new E(2, this, obj));
        }
    }

    @Override // k4.AbstractC2397j
    public final void setMessageCompression(boolean z6) {
        if (this.f20703d) {
            this.f20705f.setMessageCompression(z6);
        } else {
            b(new F(0, this, z6));
        }
    }

    @Override // k4.AbstractC2397j
    public final void start(AbstractC2396i abstractC2396i, k4.g0 g0Var) {
        k4.r0 r0Var;
        boolean z6;
        A4.g.t(this.f20704e == null, "already started");
        synchronized (this) {
            try {
                A4.g.o(abstractC2396i, ServiceSpecificExtraArgs.CastExtraArgs.LISTENER);
                this.f20704e = abstractC2396i;
                r0Var = this.f20706g;
                z6 = this.f20703d;
                if (!z6) {
                    J j4 = new J(abstractC2396i);
                    this.f20708i = j4;
                    abstractC2396i = j4;
                }
            } catch (Throwable th) {
                throw th;
            }
        }
        if (r0Var != null) {
            this.f20701b.execute(new I(this, abstractC2396i, r0Var));
        } else if (z6) {
            this.f20705f.start(abstractC2396i, g0Var);
        } else {
            b(new C1.o(this, abstractC2396i, g0Var, 6));
        }
    }

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