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

import android.content.Context;
import android.net.ConnectivityManager;
import androidx.annotation.Nullable;
import com.google.android.gms.ads.AdFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

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

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

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

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

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

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

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

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public AtomicInteger f17218g;

    @Nullable
    private volatile ConnectivityManager zze;

    @Nullable
    private final te1 zzi;

    public ue1(mf1 mf1Var, cf1 cf1Var, Context context, i8.c cVar, @Nullable te1 te1Var) {
        HashMap map = new HashMap();
        this.f17212a = map;
        map.put(AdFormat.APP_OPEN_AD, new HashMap());
        map.put(AdFormat.INTERSTITIAL, new HashMap());
        map.put(AdFormat.REWARDED, new HashMap());
        this.f17213b = mf1Var;
        this.f17214c = cf1Var;
        this.f17215d = context;
        this.f17217f = cVar;
        this.zzi = te1Var;
    }

    @Nullable
    private final synchronized Object zzo(Class cls, AdFormat adFormat, String str) {
        cf1 cf1Var = this.f17214c;
        ((i8.d) this.f17217f).getClass();
        cf1Var.c(System.currentTimeMillis());
        HashMap map = this.f17212a;
        if (!map.containsKey(adFormat)) {
            return null;
        }
        lf1 lf1Var = (lf1) ((Map) map.get(adFormat)).get(str);
        if (lf1Var != null && adFormat.equals(lf1Var.zzo())) {
            ff1 ff1Var = new ff1(lf1Var.f13664e.f31009a, lf1Var.zzo());
            ff1Var.a(str);
            gf1 gf1Var = new gf1(ff1Var);
            cf1Var.d(System.currentTimeMillis(), gf1Var, lf1Var.f13664e.f31012d, lf1Var.p(), "2");
            try {
                String strZzk = lf1Var.zzk();
                Object objZzg = lf1Var.zzg();
                Object objCast = objZzg == null ? null : cls.cast(objZzg);
                if (objCast != null) {
                    cf1Var.zzh(System.currentTimeMillis(), lf1Var.f13664e.f31012d, lf1Var.p(), strZzk, gf1Var, "2");
                }
                return objCast;
            } catch (ClassCastException e10) {
                m7.n.C.f30469h.d("PreloadAdManager.pollAd", e10);
                "Unable to cast ad to the requested type:".concat(cls.getName());
                p7.l0.h();
                return null;
            }
        }
        return null;
    }

    public final synchronized boolean a(AdFormat adFormat, String str) {
        lf1 lf1Var;
        try {
            HashMap map = this.f17212a;
            if (map.containsKey(adFormat) && (lf1Var = (lf1) ((Map) map.get(adFormat)).get(str)) != null) {
                ((Map) map.get(adFormat)).remove(str);
                lf1Var.f13665f.set(false);
                lf1Var.f13672m.set(false);
                te1 te1Var = this.zzi;
                if (te1Var != null) {
                    te1Var.e(adFormat, str);
                }
                synchronized (lf1Var) {
                    lf1Var.f13666g.clear();
                }
                cf1 cf1Var = this.f17214c;
                ((i8.d) this.f17217f).getClass();
                long jCurrentTimeMillis = System.currentTimeMillis();
                n7.p3 p3Var = lf1Var.f13664e;
                cf1Var.i(jCurrentTimeMillis, str, p3Var.f31009a, adFormat, p3Var.f31012d, lf1Var.p());
                return true;
            }
            return false;
        } finally {
        }
    }

    public final synchronized void b(int i10) {
        try {
            AdFormat adFormat = AdFormat.getAdFormat(i10);
            if (adFormat != null) {
                HashMap map = this.f17212a;
                if (map.containsKey(adFormat)) {
                    Map map2 = (Map) map.get(adFormat);
                    int size = map2.size();
                    for (String str : map2.keySet()) {
                        lf1 lf1Var = (lf1) map2.get(str);
                        if (lf1Var != null) {
                            lf1Var.f13665f.set(false);
                            lf1Var.f13672m.set(false);
                            te1 te1Var = this.zzi;
                            if (te1Var != null) {
                                te1Var.e(adFormat, str);
                            }
                            synchronized (lf1Var) {
                                lf1Var.f13666g.clear();
                            }
                            q7.j.c("Destroyed ad preloader for preloadId: ".concat(String.valueOf(str)));
                        }
                    }
                    map2.clear();
                    q7.j.c("Destroyed all ad preloaders for ad format: ".concat(adFormat.toString()));
                    cf1 cf1Var = this.f17214c;
                    ((i8.d) this.f17217f).getClass();
                    cf1Var.h(size, System.currentTimeMillis(), adFormat);
                }
            }
        } catch (Throwable th) {
            throw th;
        }
    }

    public final void c(boolean z10) {
        synchronized (this) {
            if (((Boolean) n7.y.f31093e.f31096c.b(ao.f9160y)).booleanValue()) {
                d(z10);
            }
        }
    }

    public final synchronized void d(boolean z10) {
        try {
            Iterator it = this.f17212a.values().iterator();
            while (it.hasNext()) {
                for (lf1 lf1Var : ((Map) it.next()).values()) {
                    if (z10) {
                        lf1Var.n();
                    } else {
                        lf1Var.f13665f.set(false);
                    }
                }
            }
        } catch (Throwable th) {
            throw th;
        }
    }

    public final synchronized boolean e(AdFormat adFormat) {
        int size;
        int iOrdinal;
        try {
            HashMap map = this.f17212a;
            size = map.containsKey(adFormat) ? ((Map) map.get(adFormat)).size() : 0;
            iOrdinal = adFormat.ordinal();
        } finally {
        }
        return size < (iOrdinal != 1 ? iOrdinal != 2 ? iOrdinal != 5 ? 0 : Math.max(((Integer) n7.y.f31093e.f31096c.b(ao.f8948l5)).intValue(), 1) : Math.max(((Integer) n7.y.f31093e.f31096c.b(ao.f8932k5)).intValue(), 1) : Math.max(((Integer) n7.y.f31093e.f31096c.b(ao.f8916j5)).intValue(), 1));
    }

    public final synchronized boolean zza(String str, n7.p3 p3Var, @Nullable n7.z0 z0Var) {
        lf1 lf1VarZzb;
        try {
            if (!this.f17216e.getAndSet(true)) {
                if (this.zze == null) {
                    synchronized (this) {
                        if (this.zze == null) {
                            try {
                                this.zze = (ConnectivityManager) this.f17215d.getSystemService("connectivity");
                            } catch (ClassCastException unused) {
                            }
                        }
                    }
                }
                if (!com.bumptech.glide.g.l() || this.zze == null) {
                    this.f17218g = new AtomicInteger(((Integer) n7.y.f31093e.f31096c.b(ao.I)).intValue());
                } else {
                    try {
                        this.zze.registerDefaultNetworkCallback(new m4.e(this));
                    } catch (RuntimeException unused2) {
                        this.f17218g = new AtomicInteger(((Integer) n7.y.f31093e.f31096c.b(ao.I)).intValue());
                    }
                }
                m7.n.C.f30468g.b(new rk(this));
            }
            AdFormat adFormat = AdFormat.getAdFormat(p3Var.f31010b);
            if (adFormat != null) {
                HashMap map = this.f17212a;
                if (map.containsKey(adFormat) && !((Map) map.get(adFormat)).containsKey(str) && e(adFormat) && (lf1VarZzb = this.f17213b.zzb(str, p3Var, z0Var)) != null) {
                    AtomicInteger atomicInteger = this.f17218g;
                    if (atomicInteger != null) {
                        lf1VarZzb.o(atomicInteger.get());
                    }
                    cf1 cf1Var = this.f17214c;
                    lf1VarZzb.f13674o = cf1Var;
                    te1 te1Var = this.zzi;
                    if (te1Var != null) {
                        te1Var.d(str, adFormat, lf1VarZzb);
                    } else {
                        synchronized (lf1VarZzb) {
                            lf1VarZzb.f13670k.submit(new if1(lf1VarZzb, 0));
                        }
                    }
                    ((Map) map.get(adFormat)).put(str, lf1VarZzb);
                    ff1 ff1Var = new ff1(p3Var.f31009a, adFormat);
                    ff1Var.a(str);
                    gf1 gf1Var = new gf1(ff1Var);
                    int i10 = p3Var.f31012d;
                    ((i8.d) this.f17217f).getClass();
                    cf1Var.a(i10, System.currentTimeMillis(), gf1Var, "2");
                    return true;
                }
            }
            return false;
        } catch (Throwable th) {
            throw th;
        }
    }

    @Nullable
    public final synchronized s00 zzc(String str) {
        return (s00) zzo(s00.class, AdFormat.REWARDED, str);
    }

    @Nullable
    public final synchronized jk zzd(String str) {
        return (jk) zzo(jk.class, AdFormat.APP_OPEN_AD, str);
    }

    @Nullable
    public final synchronized n7.r0 zze(String str) {
        return (n7.r0) zzo(n7.r0.class, AdFormat.INTERSTITIAL, str);
    }

    @Nullable
    public final synchronized n7.p3 zzf(AdFormat adFormat, String str) {
        HashMap map = this.f17212a;
        if (map.containsKey(adFormat)) {
            lf1 lf1Var = (lf1) ((Map) map.get(adFormat)).get(str);
            cf1 cf1Var = this.f17214c;
            ((i8.d) this.f17217f).getClass();
            cf1Var.zzo(System.currentTimeMillis(), str, lf1Var == null ? null : lf1Var.f13664e.f31009a, adFormat, lf1Var == null ? -1 : lf1Var.f13664e.f31012d, lf1Var != null ? lf1Var.p() : -1);
            if (lf1Var != null) {
                return lf1Var.f13664e;
            }
        }
        return null;
    }
}
