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

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Parcel;
import android.os.RemoteException;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.gms.ads.AdFormat;
import com.google.android.gms.ads.internal.ClientApi;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

/* JADX INFO: loaded from: classes2.dex */
public abstract class lf1 {

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

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

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

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

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public n7.p3 f13664e;

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

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final Queue f13666g;

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

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public final String f13668i;

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

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

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public final tc1 f13671l;

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

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public final AtomicBoolean f13673n;

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    public cf1 f13674o;

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

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public final gf1 f13676q;

    @Nullable
    protected final te1 zzf;

    @Nullable
    protected n7.z0 zzh;

    @Nullable
    private n7.x0 zzi;

    public lf1(ClientApi clientApi, Context context, int i10, pb1 pb1Var, @NonNull n7.p3 p3Var, @Nullable n7.x0 x0Var, @NonNull ScheduledExecutorService scheduledExecutorService, @NonNull tc1 tc1Var, ye1 ye1Var, i8.c cVar) {
        this("none", clientApi, context, i10, pb1Var, p3Var, scheduledExecutorService, tc1Var, ye1Var, cVar, null);
        this.zzi = x0Var;
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* JADX WARN: Removed duplicated region for block: B:18:0x0030 A[DONT_GENERATE] */
    /* JADX WARN: Removed duplicated region for block: B:23:0x0020 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX INFO: renamed from: zzT, reason: merged with bridge method [inline-methods] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final synchronized void b(@Nullable n7.m2 m2Var) {
        n7.z0 z0Var;
        n7.x0 x0Var = this.zzi;
        if (x0Var != null) {
            try {
                n7.p3 p3Var = this.f13664e;
                n7.w0 w0Var = (n7.w0) x0Var;
                Parcel parcelO = w0Var.O();
                aj.b(parcelO, p3Var);
                w0Var.O0(1, parcelO);
            } catch (RemoteException unused) {
                q7.j.d("Failed to call onAdsAvailable");
            }
            z0Var = this.zzh;
            if (z0Var == null) {
                try {
                    ((n7.y0) z0Var).zze(this.f13668i, m2Var);
                    return;
                } catch (RemoteException unused2) {
                    q7.j.d("Failed to call onAdPreloaded");
                    return;
                }
            }
            return;
        }
        z0Var = this.zzh;
        if (z0Var == null) {
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    @Nullable
    public static final String zzX(@Nullable n7.m2 m2Var) {
        if (m2Var instanceof oe0) {
            return ((oe0) m2Var).zzk();
        }
        return null;
    }

    public final void a(n7.l2 l2Var) {
        synchronized (this) {
            try {
                if (this.f13672m.get()) {
                    p7.r0.f31763l.post(new nw0(this, l2Var));
                }
                this.f13669j.set(false);
                int i10 = l2Var.f30997a;
                if (i10 != 1 && i10 != 8 && i10 != 10 && i10 != 11) {
                    f(true);
                    return;
                }
                n7.p3 p3Var = this.f13664e;
                int i11 = p3Var.f31010b;
                String str = p3Var.f31009a;
                StringBuilder sb = new StringBuilder(String.valueOf(i11).length() + 26 + String.valueOf(str).length() + 61);
                sb.append("Preloading ");
                sb.append(i11);
                sb.append(", for adUnitId:");
                sb.append(str);
                sb.append(", Ad load failed. Stop preloading due to non-retriable error:");
                q7.j.c(sb.toString());
                this.f13665f.set(false);
                te1 te1Var = this.zzf;
                if (te1Var != null) {
                    te1Var.a(this);
                }
                ff1 ff1Var = new ff1(this.f13664e.f31009a, zzo());
                ff1Var.a(this.f13668i);
                gf1 gf1Var = new gf1(ff1Var);
                cf1 cf1Var = this.f13674o;
                ((i8.d) this.f13675p).getClass();
                cf1Var.g(System.currentTimeMillis(), gf1Var, l2Var, this.f13664e.f31012d, p(), i());
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:21:0x0020 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void c() {
        n7.z0 z0Var;
        synchronized (this) {
            n7.x0 x0Var = this.zzi;
            if (x0Var != null) {
                try {
                    n7.p3 p3Var = this.f13664e;
                    n7.w0 w0Var = (n7.w0) x0Var;
                    Parcel parcelO = w0Var.O();
                    aj.b(parcelO, p3Var);
                    w0Var.O0(2, parcelO);
                } catch (RemoteException unused) {
                    q7.j.d("Failed to call onAdsExhausted");
                }
                z0Var = this.zzh;
                if (z0Var != null) {
                    try {
                        String str = this.f13668i;
                        n7.y0 y0Var = (n7.y0) z0Var;
                        Parcel parcelO2 = y0Var.O();
                        parcelO2.writeString(str);
                        y0Var.O0(2, parcelO2);
                    } catch (RemoteException unused2) {
                        q7.j.d("Failed to call onAdsExhausted");
                        return;
                    }
                }
            } else {
                z0Var = this.zzh;
                if (z0Var != null) {
                }
            }
        }
    }

    public final synchronized void e(Object obj) {
        try {
            n7.m2 m2VarZzc = zzc(obj);
            double d10 = !(m2VarZzc instanceof oe0) ? 0.0d : ((oe0) m2VarZzc).f15046g;
            n7.m2 m2VarZzc2 = zzc(obj);
            int i10 = m2VarZzc2 instanceof oe0 ? ((oe0) m2VarZzc2).f15047h : 2;
            i8.c cVar = this.f13675p;
            hf1 hf1Var = new hf1(obj, cVar, d10, i10);
            this.f13666g.add(hf1Var);
            n7.m2 m2VarZzc3 = zzc(obj);
            ((i8.d) cVar).getClass();
            long jCurrentTimeMillis = System.currentTimeMillis();
            if (this.f13672m.get()) {
                p7.r0.f31763l.post(new nw0(this, m2VarZzc3));
            }
            ScheduledExecutorService scheduledExecutorService = this.f13670k;
            scheduledExecutorService.execute(new o1(this, jCurrentTimeMillis, m2VarZzc3));
            if (this.zzf != null) {
                if (((Boolean) n7.y.f31093e.f31096c.b(ao.E)).booleanValue()) {
                    this.f13671l.a(new if1(this, 4, 0), hf1Var.a(), TimeUnit.MILLISECONDS);
                    return;
                } else {
                    scheduledExecutorService.schedule(new if1(this, 3, 0), hf1Var.a(), TimeUnit.MILLISECONDS);
                    return;
                }
            }
            if (((Boolean) n7.y.f31093e.f31096c.b(ao.E)).booleanValue()) {
                this.f13671l.a(new if1(this, 0), hf1Var.a(), TimeUnit.MILLISECONDS);
            } else {
                scheduledExecutorService.schedule(new if1(this, 0), hf1Var.a(), TimeUnit.MILLISECONDS);
            }
        } catch (Throwable th) {
            throw th;
        }
    }

    public final synchronized void f(boolean z10) {
        try {
            te1 te1Var = this.zzf;
            if (te1Var != null) {
                if (z10) {
                    this.f13667h.c();
                }
                te1Var.a(this);
            } else {
                ye1 ye1Var = this.f13667h;
                if (ye1Var.d()) {
                    return;
                }
                if (z10) {
                    ye1Var.c();
                }
                this.f13670k.schedule(new if1(this, 0), ye1Var.b(), TimeUnit.MILLISECONDS);
            }
        } catch (Throwable th) {
            throw th;
        }
    }

    public final synchronized void g() {
        Iterator it = this.f13666g.iterator();
        while (it.hasNext()) {
            hf1 hf1Var = (hf1) it.next();
            ((i8.d) hf1Var.f12141c).getClass();
            if (System.currentTimeMillis() >= hf1Var.f12140b + hf1Var.f12142d) {
                it.remove();
                te1 te1Var = this.zzf;
                if (te1Var != null) {
                    te1Var.a(this);
                }
            }
        }
    }

    public final synchronized void h() {
        try {
            if (this.f13673n.get() && this.f13666g.isEmpty()) {
                this.f13673n.set(false);
                if (this.f13672m.get()) {
                    p7.r0.f31763l.post(new if1(this, 1));
                }
                this.f13670k.execute(new if1(this, 2));
            }
        } catch (Throwable th) {
            throw th;
        }
    }

    public final String i() {
        return true != "none".equals(this.f13668i) ? "2" : "1";
    }

    public abstract bu1 j(Context context);

    public long k() {
        throw null;
    }

    public final synchronized void l() {
        if (!this.f13669j.get() && this.f13665f.get() && this.f13666g.size() < this.f13664e.f31012d) {
            this.f13669j.set(true);
            this.f13670k.submit(new if1(this, 5, 0));
        }
    }

    public final synchronized boolean m() {
        try {
            tn tnVar = ao.K;
            n7.y yVar = n7.y.f31093e;
            if (((Boolean) yVar.f31096c.b(tnVar)).booleanValue()) {
                this.f13667h.a();
            }
            if (((Boolean) yVar.f31096c.b(ao.D)).booleanValue() && this.zzf == null) {
                r();
            } else {
                g();
            }
        } catch (Throwable th) {
            throw th;
        }
        return !this.f13666g.isEmpty();
    }

    public final synchronized void n() {
        this.f13665f.set(true);
        this.f13672m.set(true);
        if (this.zzf == null) {
            this.f13670k.submit(new if1(this, 0));
        }
    }

    public final synchronized void o(int i10) {
        com.google.android.gms.common.internal.w.a(i10 >= 5);
        ye1 ye1Var = this.f13667h;
        synchronized (ye1Var) {
            com.google.android.gms.common.internal.w.a(i10 > 0);
            ye1Var.f18821d = i10;
        }
    }

    public final synchronized int p() {
        return this.f13666g.size();
    }

    public final boolean q() {
        if (!this.f13665f.get() || this.f13669j.get() || p() >= this.f13664e.f31012d) {
            return false;
        }
        ye1 ye1Var = this.f13667h;
        ((i8.d) ye1Var.f18823f).getClass();
        return System.currentTimeMillis() >= ye1Var.f18822e;
    }

    public final synchronized void r() {
        g();
        h();
        if (!this.f13669j.get() && this.f13665f.get() && this.f13666g.size() < this.f13664e.f31012d) {
            this.f13669j.set(true);
            s();
        }
    }

    public final synchronized void s() {
        bu1 bu1VarJ;
        try {
            Activity activityZzd = m7.n.C.f30468g.zzd();
            if (activityZzd == null) {
                q7.j.d("Empty activity context at preloading: ".concat(String.valueOf(this.f13664e.f31009a)));
                bu1VarJ = j(this.f13661b);
            } else {
                bu1VarJ = j(activityZzd);
            }
            ih1.z1(bu1VarJ, new jc1(this), this.f13670k);
        } catch (Throwable th) {
            throw th;
        }
    }

    public final void t(int i10) {
        te1 te1Var;
        com.google.android.gms.common.internal.w.a(i10 > 0);
        AdFormat adFormat = AdFormat.getAdFormat(this.f13664e.f31010b);
        int i11 = this.f13664e.f31012d;
        int size = this.f13666g.size();
        synchronized (this) {
            try {
                n7.p3 p3Var = this.f13664e;
                this.f13664e = new n7.p3(p3Var.f31009a, p3Var.f31010b, p3Var.f31011c, i10 > 0 ? i10 : p3Var.f31012d);
                Queue queue = this.f13666g;
                if (queue.size() > i10) {
                    if (((Boolean) n7.y.f31093e.f31096c.b(ao.f9143x)).booleanValue()) {
                        ArrayList arrayList = new ArrayList();
                        for (int i12 = 0; i12 < i10; i12++) {
                            hf1 hf1Var = (hf1) queue.poll();
                            if (hf1Var != null) {
                                arrayList.add(hf1Var);
                            }
                        }
                        queue.clear();
                        queue.addAll(arrayList);
                        if (size > arrayList.size() && (te1Var = this.zzf) != null) {
                            int size2 = size - arrayList.size();
                            if (arrayList.isEmpty()) {
                                size2--;
                            }
                            te1Var.c(this, size2);
                        }
                    }
                }
            } catch (Throwable th) {
                throw th;
            }
        }
        cf1 cf1Var = this.f13674o;
        if (cf1Var == null || adFormat == null) {
            return;
        }
        ((i8.d) this.f13675p).getClass();
        long jCurrentTimeMillis = System.currentTimeMillis();
        gf1 gf1Var = new gf1(new ff1(this.f13664e.f31009a, adFormat));
        br0 br0VarA = cf1Var.f9881a.a();
        br0VarA.zzc("action", "cache_resize");
        br0VarA.zzc("cs_ts", Long.toString(jCurrentTimeMillis));
        br0VarA.zzc("orig_ma", Integer.toString(i11));
        br0VarA.zzc("max_ads", Integer.toString(i10));
        br0VarA.zzc("ad_format", gf1Var.a());
        br0VarA.zzc("ad_unit_id", gf1Var.f11731a);
        br0VarA.zzc("pid", gf1Var.zzc());
        br0VarA.zzc("pv", "1");
        br0VarA.b();
    }

    public final void u(n7.a4 a4Var) {
        if (((Boolean) n7.y.f31093e.f31096c.b(ao.C)).booleanValue()) {
            Bundle bundle = a4Var.f30894b0;
            bundle.putInt("plcs", p());
            bundle.putInt("plbs", this.f13664e.f31012d);
            bundle.putString("plid", this.f13668i);
        }
    }

    @Nullable
    public abstract n7.m2 zzc(Object obj);

    @Nullable
    public final synchronized Object zzg() {
        try {
            tn tnVar = ao.N;
            n7.y yVar = n7.y.f31093e;
            boolean z10 = true;
            if (((Boolean) yVar.f31096c.b(tnVar)).booleanValue()) {
                if (!((Boolean) yVar.f31096c.b(ao.L)).booleanValue() || this.f13666g.size() == 1) {
                    this.f13667h.a();
                }
            }
            if (((Boolean) yVar.f31096c.b(ao.M)).booleanValue()) {
                g();
            }
            Queue queue = this.f13666g;
            hf1 hf1Var = (hf1) queue.poll();
            AtomicBoolean atomicBoolean = this.f13673n;
            if (hf1Var == null) {
                z10 = false;
            }
            atomicBoolean.set(z10);
            if (hf1Var == null) {
                hf1Var = null;
            } else if (!queue.isEmpty()) {
                hf1 hf1Var2 = (hf1) queue.peek();
                AdFormat adFormat = AdFormat.getAdFormat(this.f13664e.f31010b);
                String strZzX = zzX(zzc(hf1Var.f12139a));
                if (hf1Var2 != null && adFormat != null && strZzX != null && hf1Var2.f12140b < hf1Var.f12140b) {
                    cf1 cf1Var = this.f13674o;
                    ((i8.d) this.f13675p).getClass();
                    cf1Var.e(System.currentTimeMillis(), this.f13664e.f31012d, p(), strZzX, this.f13676q, i());
                }
            }
            te1 te1Var = this.zzf;
            if (te1Var != null) {
                te1Var.b(this);
            } else {
                long jK = k();
                if (jK < 0) {
                    jK = ((Long) yVar.f31096c.b(ao.P)).longValue();
                }
                if (jK > 0) {
                    this.f13670k.schedule(new if1(this, 0), jK, TimeUnit.MILLISECONDS);
                } else {
                    r();
                }
            }
            if (hf1Var == null) {
                return null;
            }
            return hf1Var.f12139a;
        } catch (Throwable th) {
            throw th;
        }
    }

    @Nullable
    public final synchronized String zzk() {
        Object objZzs;
        objZzs = zzs();
        return zzX(objZzs == null ? null : zzc(objZzs));
    }

    @Nullable
    public final AdFormat zzo() {
        return AdFormat.getAdFormat(this.f13664e.f31010b);
    }

    @Nullable
    public final synchronized Object zzs() {
        hf1 hf1Var = (hf1) this.f13666g.peek();
        if (hf1Var == null) {
            return null;
        }
        return hf1Var.f12139a;
    }

    public lf1(String str, ClientApi clientApi, Context context, int i10, pb1 pb1Var, @NonNull n7.p3 p3Var, @Nullable n7.z0 z0Var, @NonNull ScheduledExecutorService scheduledExecutorService, @NonNull tc1 tc1Var, ye1 ye1Var, i8.c cVar, @Nullable te1 te1Var) {
        this(str, clientApi, context, i10, pb1Var, p3Var, scheduledExecutorService, tc1Var, ye1Var, cVar, te1Var);
        this.zzh = z0Var;
    }

    private lf1(String str, ClientApi clientApi, Context context, int i10, pb1 pb1Var, @NonNull n7.p3 p3Var, @NonNull ScheduledExecutorService scheduledExecutorService, @NonNull tc1 tc1Var, ye1 ye1Var, i8.c cVar, @Nullable te1 te1Var) {
        Queue priorityQueue;
        this.f13668i = str;
        this.f13660a = clientApi;
        this.f13661b = context;
        this.f13662c = i10;
        this.f13663d = pb1Var;
        this.f13664e = p3Var;
        int iMax = Math.max(1, p3Var.f31012d);
        if (((Boolean) n7.y.f31093e.f31096c.b(ao.X)).booleanValue()) {
            priorityQueue = new nf1();
        } else {
            priorityQueue = new PriorityQueue(iMax, jf1.f12840a);
        }
        this.f13666g = priorityQueue;
        this.f13665f = new AtomicBoolean(true);
        this.f13669j = new AtomicBoolean(false);
        this.f13670k = scheduledExecutorService;
        this.f13671l = tc1Var;
        this.f13667h = ye1Var;
        this.f13672m = new AtomicBoolean(true);
        this.f13673n = new AtomicBoolean(false);
        this.f13675p = cVar;
        ff1 ff1Var = new ff1(p3Var.f31009a, AdFormat.getAdFormat(this.f13664e.f31010b));
        ff1Var.a(str);
        this.f13676q = new gf1(ff1Var);
        this.zzf = te1Var;
    }
}
