package com.google.android.gms.internal.ads;

import androidx.annotation.Nullable;
import com.google.android.gms.ads.AdFormat;
import java.util.LinkedHashMap;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

/* JADX INFO: loaded from: classes2.dex */
public final class te1 {

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final AtomicBoolean f16863b = new AtomicBoolean(false);

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final LinkedHashMap f16864c = new LinkedHashMap();

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final AtomicBoolean f16865d = new AtomicBoolean(false);

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

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

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final i8.c f16868g;

    @Nullable
    private ScheduledFuture zze;

    public te1(int i10, ScheduledExecutorService scheduledExecutorService, cf1 cf1Var, i8.c cVar) {
        this.f16862a = i10;
        this.f16866e = scheduledExecutorService;
        this.f16867f = cf1Var;
        this.f16868g = cVar;
    }

    public final void a(lf1 lf1Var) {
        int i10 = 1;
        if (lf1Var.p() <= 0 && !lf1Var.f13669j.get()) {
            i10 = 0;
        }
        c(lf1Var, i10);
    }

    public final synchronized void b(lf1 lf1Var) {
        try {
            if (g(lf1Var)) {
                return;
            }
            long jK = lf1Var.k();
            if (jK < 0) {
                jK = ((Long) n7.y.f31093e.f31096c.b(ao.P)).longValue();
            }
            if (jK <= 0) {
                a(lf1Var);
                return;
            }
            if (lf1Var.p() > 0 || lf1Var.f13669j.get()) {
                l(lf1Var, 1);
            }
            this.f16863b.set(true);
            ScheduledFuture scheduledFuture = this.zze;
            if (scheduledFuture != null) {
                scheduledFuture.cancel(false);
            }
            this.zze = null;
            this.f16865d.set(true);
            this.zze = this.f16866e.schedule(new se1(this, 4), jK, TimeUnit.MILLISECONDS);
        } catch (Throwable th) {
            throw th;
        }
    }

    public final void c(lf1 lf1Var, int i10) {
        if (g(lf1Var)) {
            return;
        }
        if (i10 > 0) {
            l(lf1Var, i10);
        }
        if (!m(lf1Var)) {
            i(0L);
        } else {
            i(((Long) n7.y.f31093e.f31096c.b(ao.U)).longValue());
        }
    }

    public final void d(String str, AdFormat adFormat, lf1 lf1Var) {
        LinkedHashMap linkedHashMap = this.f16864c;
        String str2 = String.format("%s:%s", str, adFormat);
        synchronized (linkedHashMap) {
            try {
                if (!linkedHashMap.containsKey(str2)) {
                    linkedHashMap.put(str2, lf1Var);
                    m(lf1Var);
                    i(((Long) n7.y.f31093e.f31096c.b(ao.U)).longValue());
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final void e(AdFormat adFormat, String str) {
        lf1 lf1Var;
        LinkedHashMap linkedHashMap = this.f16864c;
        String str2 = String.format("%s:%s", str, adFormat);
        synchronized (linkedHashMap) {
            lf1Var = (lf1) linkedHashMap.remove(str2);
        }
        if (lf1Var == null) {
            return;
        }
        int iP = lf1Var.p();
        int i10 = iP - 1;
        if (!lf1Var.f13669j.get()) {
            iP = i10;
        }
        int iMax = Math.max(iP, 0);
        if (iMax > 0) {
            l(lf1Var, iMax);
        }
        if (lf1Var.p() > 0 || lf1Var.f13669j.get()) {
            i(0L);
        }
    }

    public final int f() {
        int iMax;
        LinkedHashMap linkedHashMap = this.f16864c;
        synchronized (linkedHashMap) {
            try {
                iMax = 0;
                for (lf1 lf1Var : linkedHashMap.values()) {
                    int iP = lf1Var.p();
                    int i10 = iP - 1;
                    if (!lf1Var.f13669j.get()) {
                        iP = i10;
                    }
                    iMax += Math.max(iP, 0);
                }
            } catch (Throwable th) {
                throw th;
            }
        }
        return iMax;
    }

    public final boolean g(lf1 lf1Var) {
        AdFormat adFormatZzo = lf1Var.zzo();
        if (adFormatZzo == null) {
            return true;
        }
        LinkedHashMap linkedHashMap = this.f16864c;
        synchronized (linkedHashMap) {
            try {
                return !linkedHashMap.containsKey(String.format("%s:%s", lf1Var.f13668i, adFormatZzo));
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final boolean h() {
        LinkedHashMap linkedHashMap = this.f16864c;
        synchronized (linkedHashMap) {
            try {
                for (lf1 lf1Var : linkedHashMap.values()) {
                    if (lf1Var.p() == 0 && lf1Var.q()) {
                        return true;
                    }
                }
                return false;
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final synchronized void i(long j10) {
        if (this.f16865d.compareAndSet(false, true)) {
            if (j10 > 0) {
                this.zze = this.f16866e.schedule(new se1(this, 1), j10, TimeUnit.MILLISECONDS);
            } else {
                this.f16866e.execute(new se1(this, 0));
            }
        }
    }

    public final synchronized void j() {
        zzguf zzgufVarS;
        try {
            if (this.f16863b.get()) {
                return;
            }
            lf1 lf1Var = null;
            this.zze = null;
            if (f() < this.f16862a || h()) {
                LinkedHashMap linkedHashMap = this.f16864c;
                synchronized (linkedHashMap) {
                    zzgufVarS = zzguf.s(linkedHashMap.values());
                }
                int size = zzgufVarS.size();
                double d10 = Double.MAX_VALUE;
                for (int i10 = 0; i10 < size; i10++) {
                    lf1 lf1Var2 = (lf1) zzgufVarS.get(i10);
                    if (lf1Var2.q()) {
                        double dP = ((double) lf1Var2.p()) / ((double) lf1Var2.f13664e.f31012d);
                        if (dP < d10) {
                            d10 = dP;
                        }
                        if (dP < d10) {
                            lf1Var = lf1Var2;
                        }
                    }
                }
                if (lf1Var != null) {
                    lf1Var.l();
                    if (lf1Var.p() > 0) {
                        cf1 cf1Var = this.f16867f;
                        ((i8.d) this.f16868g).getClass();
                        cf1Var.zzq(System.currentTimeMillis(), lf1Var.f13668i, lf1Var.f13664e.f31009a, lf1Var.zzo(), lf1Var.f13664e.f31012d, lf1Var.p(), f(), this.f16862a);
                    }
                }
                int iF = f();
                int i11 = this.f16862a;
                if (iF >= i11) {
                    k();
                }
                if (f() >= i11) {
                    if (h()) {
                    }
                }
                if (h()) {
                    this.f16866e.execute(new se1(this, 3));
                    return;
                }
                this.zze = this.f16866e.schedule(new se1(this, 2), ((Long) n7.y.f31093e.f31096c.b(ao.V)).longValue(), TimeUnit.MILLISECONDS);
                return;
            }
            k();
            this.f16865d.set(false);
        } catch (Throwable th) {
            throw th;
        }
    }

    public final void k() {
        int size;
        LinkedHashMap linkedHashMap = this.f16864c;
        synchronized (linkedHashMap) {
            size = linkedHashMap.size();
        }
        cf1 cf1Var = this.f16867f;
        i8.c cVar = this.f16868g;
        int i10 = this.f16862a;
        ((i8.d) cVar).getClass();
        long jCurrentTimeMillis = System.currentTimeMillis();
        br0 br0VarA = cf1Var.f9881a.a();
        br0VarA.zzc("action", "acmlr");
        br0VarA.zzc("pat", Long.toString(jCurrentTimeMillis));
        br0VarA.zzc("mpl", Integer.toString(i10));
        br0VarA.zzc("pas", Integer.toString(size));
        br0VarA.b();
    }

    public final void l(lf1 lf1Var, int i10) {
        ((i8.d) this.f16868g).getClass();
        this.f16867f.zzr(System.currentTimeMillis(), lf1Var.f13668i, lf1Var.f13664e.f31009a, lf1Var.zzo(), lf1Var.f13664e.f31012d, lf1Var.p(), i10, f(), this.f16862a);
    }

    public final synchronized boolean m(lf1 lf1Var) {
        if (!this.f16863b.get() && lf1Var.p() == 0 && lf1Var.q()) {
            lf1Var.l();
            return true;
        }
        return false;
    }
}
