package yq;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.RejectedExecutionException;
import java.util.logging.Level;
import java.util.logging.Logger;
import kotlin.jvm.internal.t;
import rm.n0;
import vq.p;

/* loaded from: classes3.dex */
public final class c {

    /* renamed from: a, reason: collision with root package name */
    private final d f52919a;

    /* renamed from: b, reason: collision with root package name */
    private final String f52920b;

    /* renamed from: c, reason: collision with root package name */
    private boolean f52921c;

    /* renamed from: d, reason: collision with root package name */
    private yq.a f52922d;

    /* renamed from: e, reason: collision with root package name */
    private final List f52923e;

    /* renamed from: f, reason: collision with root package name */
    private boolean f52924f;

    /* loaded from: classes3.dex */
    public static final class a extends yq.a {

        /* renamed from: e, reason: collision with root package name */
        final /* synthetic */ dn.a f52925e;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        a(String str, boolean z10, dn.a aVar) {
            super(str, z10);
            this.f52925e = aVar;
        }

        @Override // yq.a
        public long f() {
            this.f52925e.invoke();
            return -1L;
        }
    }

    /* loaded from: classes3.dex */
    public static final class b extends yq.a {

        /* renamed from: e, reason: collision with root package name */
        final /* synthetic */ dn.a f52926e;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        b(String str, dn.a aVar) {
            super(str, false, 2, null);
            this.f52926e = aVar;
        }

        @Override // yq.a
        public long f() {
            return ((Number) this.f52926e.invoke()).longValue();
        }
    }

    public c(d taskRunner, String name) {
        t.g(taskRunner, "taskRunner");
        t.g(name, "name");
        this.f52919a = taskRunner;
        this.f52920b = name;
        this.f52923e = new ArrayList();
    }

    public static /* synthetic */ void d(c cVar, String str, long j10, boolean z10, dn.a aVar, int i10, Object obj) {
        if ((i10 & 2) != 0) {
            j10 = 0;
        }
        long j11 = j10;
        if ((i10 & 4) != 0) {
            z10 = true;
        }
        cVar.c(str, j11, z10, aVar);
    }

    public static /* synthetic */ void m(c cVar, yq.a aVar, long j10, int i10, Object obj) {
        if ((i10 & 2) != 0) {
            j10 = 0;
        }
        cVar.l(aVar, j10);
    }

    public final void a() {
        if (p.f40482e && Thread.holdsLock(this)) {
            throw new AssertionError("Thread " + Thread.currentThread().getName() + " MUST NOT hold lock on " + this);
        }
        synchronized (this.f52919a) {
            if (b()) {
                this.f52919a.h(this);
            }
            n0 n0Var = n0.f34904a;
        }
    }

    public final boolean b() {
        yq.a aVar = this.f52922d;
        if (aVar != null) {
            t.d(aVar);
            if (aVar.a()) {
                this.f52924f = true;
            }
        }
        boolean z10 = false;
        for (int size = this.f52923e.size() - 1; -1 < size; size--) {
            if (((yq.a) this.f52923e.get(size)).a()) {
                Logger g10 = this.f52919a.g();
                yq.a aVar2 = (yq.a) this.f52923e.get(size);
                if (g10.isLoggable(Level.FINE)) {
                    yq.b.c(g10, aVar2, this, "canceled");
                }
                this.f52923e.remove(size);
                z10 = true;
            }
        }
        return z10;
    }

    public final void c(String name, long j10, boolean z10, dn.a block) {
        t.g(name, "name");
        t.g(block, "block");
        l(new a(name, z10, block), j10);
    }

    public final yq.a e() {
        return this.f52922d;
    }

    public final boolean f() {
        return this.f52924f;
    }

    public final List g() {
        return this.f52923e;
    }

    public final String h() {
        return this.f52920b;
    }

    public final boolean i() {
        return this.f52921c;
    }

    public final d j() {
        return this.f52919a;
    }

    public final void k(String name, long j10, dn.a block) {
        t.g(name, "name");
        t.g(block, "block");
        l(new b(name, block), j10);
    }

    public final void l(yq.a task, long j10) {
        t.g(task, "task");
        synchronized (this.f52919a) {
            if (!this.f52921c) {
                if (n(task, j10, false)) {
                    this.f52919a.h(this);
                }
                n0 n0Var = n0.f34904a;
            } else if (task.a()) {
                Logger g10 = this.f52919a.g();
                if (g10.isLoggable(Level.FINE)) {
                    yq.b.c(g10, task, this, "schedule canceled (queue is shutdown)");
                }
            } else {
                Logger g11 = this.f52919a.g();
                if (g11.isLoggable(Level.FINE)) {
                    yq.b.c(g11, task, this, "schedule failed (queue is shutdown)");
                }
                throw new RejectedExecutionException();
            }
        }
    }

    public final boolean n(yq.a task, long j10, boolean z10) {
        String str;
        t.g(task, "task");
        task.e(this);
        long nanoTime = this.f52919a.f().nanoTime();
        long j11 = nanoTime + j10;
        int indexOf = this.f52923e.indexOf(task);
        if (indexOf != -1) {
            if (task.c() <= j11) {
                Logger g10 = this.f52919a.g();
                if (g10.isLoggable(Level.FINE)) {
                    yq.b.c(g10, task, this, "already scheduled");
                }
                return false;
            }
            this.f52923e.remove(indexOf);
        }
        task.g(j11);
        Logger g11 = this.f52919a.g();
        if (g11.isLoggable(Level.FINE)) {
            if (z10) {
                str = "run again after " + yq.b.b(j11 - nanoTime);
            } else {
                str = "scheduled after " + yq.b.b(j11 - nanoTime);
            }
            yq.b.c(g11, task, this, str);
        }
        Iterator it = this.f52923e.iterator();
        int i10 = 0;
        while (true) {
            if (!it.hasNext()) {
                i10 = -1;
                break;
            }
            if (((yq.a) it.next()).c() - nanoTime > j10) {
                break;
            }
            i10++;
        }
        if (i10 == -1) {
            i10 = this.f52923e.size();
        }
        this.f52923e.add(i10, task);
        return i10 == 0;
    }

    public final void o(yq.a aVar) {
        this.f52922d = aVar;
    }

    public final void p(boolean z10) {
        this.f52924f = z10;
    }

    public final void q() {
        if (p.f40482e && Thread.holdsLock(this)) {
            throw new AssertionError("Thread " + Thread.currentThread().getName() + " MUST NOT hold lock on " + this);
        }
        synchronized (this.f52919a) {
            this.f52921c = true;
            if (b()) {
                this.f52919a.h(this);
            }
            n0 n0Var = n0.f34904a;
        }
    }

    public String toString() {
        return this.f52920b;
    }
}
