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.ArrayList;
import java.util.EnumMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

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

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

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

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

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

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

    @Nullable
    private volatile ConnectivityManager zzf;

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

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final ConcurrentHashMap f10772a = new ConcurrentHashMap();

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

    public ef1(mf1 mf1Var, cf1 cf1Var, Context context, i8.c cVar) {
        this.f10774c = mf1Var;
        this.f10775d = cf1Var;
        this.f10776e = context;
        this.f10778g = cVar;
    }

    public static String zzh(String str, @Nullable AdFormat adFormat) {
        String strName = adFormat == null ? "NULL" : adFormat.name();
        return defpackage.e.p(new StringBuilder(String.valueOf(str).length() + 1 + String.valueOf(strName).length()), str, "#", strName);
    }

    @Nullable
    private final synchronized Object zzp(Class cls, String str, AdFormat adFormat) {
        gf1 gf1Var = new gf1(new ff1(str, adFormat));
        cf1 cf1Var = this.f10775d;
        ((i8.d) this.f10778g).getClass();
        cf1Var.d(System.currentTimeMillis(), gf1Var, -1, -1, "1");
        lf1 lf1VarZzq = zzq(str, adFormat);
        if (lf1VarZzq == null) {
            return null;
        }
        try {
            String strZzk = lf1VarZzq.zzk();
            Object objZzg = lf1VarZzq.zzg();
            Object objCast = objZzg == null ? null : cls.cast(objZzg);
            if (objCast != null) {
                cf1Var.zzh(System.currentTimeMillis(), lf1VarZzq.f13664e.f31012d, lf1VarZzq.p(), strZzk, gf1Var, "1");
            }
            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;
        }
    }

    @Nullable
    private final synchronized lf1 zzq(String str, AdFormat adFormat) {
        return (lf1) this.f10772a.get(zzh(str, adFormat));
    }

    /* JADX WARN: Code restructure failed: missing block: B:47:0x00d0, code lost:
    
        r5 = 0;
        r7 = q7.e.f32222b;
     */
    /* JADX WARN: Code restructure failed: missing block: B:48:0x00da, code lost:
    
        if (r0.containsKey(r4) == false) goto L50;
     */
    /* JADX WARN: Code restructure failed: missing block: B:49:0x00dc, code lost:
    
        r5 = r0.get(r4);
     */
    /* JADX WARN: Code restructure failed: missing block: B:50:0x00e0, code lost:
    
        r0.put(r4, java.lang.Integer.valueOf(((java.lang.Integer) r5).intValue() + 1));
        r7 = new com.google.android.gms.internal.ads.gf1(new com.google.android.gms.internal.ads.ff1(r3, r4));
        r3 = r2.f31012d;
        ((i8.d) r11.f10778g).getClass();
        r6.a(r3, java.lang.System.currentTimeMillis(), r7, "1");
     */
    /* JADX WARN: Removed duplicated region for block: B:29:0x0059 A[Catch: all -> 0x0028, TryCatch #4 {all -> 0x0028, blocks: (B:3:0x0001, B:31:0x007c, B:32:0x008b, B:34:0x0091, B:37:0x00a9, B:39:0x00ad, B:40:0x00b4, B:41:0x00bc, B:46:0x00cf, B:47:0x00d0, B:49:0x00dc, B:50:0x00e0, B:57:0x0114, B:58:0x0115, B:59:0x0116, B:6:0x000c, B:8:0x0010, B:18:0x0027, B:21:0x002b, B:23:0x0031, B:26:0x0036, B:30:0x0070, B:28:0x0041, B:29:0x0059, B:9:0x0011, B:11:0x0015, B:15:0x0024, B:42:0x00bd, B:44:0x00c9, B:45:0x00ca, B:55:0x0112, B:56:0x0113), top: B:73:0x0001, inners: #2, #3, #5 }] */
    /* JADX WARN: Removed duplicated region for block: B:34:0x0091 A[Catch: all -> 0x0028, TryCatch #4 {all -> 0x0028, blocks: (B:3:0x0001, B:31:0x007c, B:32:0x008b, B:34:0x0091, B:37:0x00a9, B:39:0x00ad, B:40:0x00b4, B:41:0x00bc, B:46:0x00cf, B:47:0x00d0, B:49:0x00dc, B:50:0x00e0, B:57:0x0114, B:58:0x0115, B:59:0x0116, B:6:0x000c, B:8:0x0010, B:18:0x0027, B:21:0x002b, B:23:0x0031, B:26:0x0036, B:30:0x0070, B:28:0x0041, B:29:0x0059, B:9:0x0011, B:11:0x0015, B:15:0x0024, B:42:0x00bd, B:44:0x00c9, B:45:0x00ca, B:55:0x0112, B:56:0x0113), top: B:73:0x0001, inners: #2, #3, #5 }] */
    /* JADX WARN: Type inference fix 'apply assigned field type' failed
    java.lang.UnsupportedOperationException: ArgType.getObject(), call class: class jadx.core.dex.instructions.args.ArgType$UnknownArg
    	at jadx.core.dex.instructions.args.ArgType.getObject(ArgType.java:593)
    	at jadx.core.dex.attributes.nodes.ClassTypeVarsAttr.getTypeVarsMapFor(ClassTypeVarsAttr.java:35)
    	at jadx.core.dex.nodes.utils.TypeUtils.replaceClassGenerics(TypeUtils.java:177)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.insertExplicitUseCast(FixTypesVisitor.java:397)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.tryFieldTypeWithNewCasts(FixTypesVisitor.java:359)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.applyFieldType(FixTypesVisitor.java:309)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.visit(FixTypesVisitor.java:94)
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final synchronized void a(ArrayList arrayList, n7.x0 x0Var) {
        try {
            if (!this.f10777f.getAndSet(true)) {
                if (this.zzf == null) {
                    synchronized (this) {
                        if (this.zzf == null) {
                            try {
                                this.zzf = (ConnectivityManager) this.f10776e.getSystemService("connectivity");
                            } catch (ClassCastException unused) {
                            }
                        }
                    }
                }
                if (com.bumptech.glide.g.l() || this.zzf == null) {
                    this.f10779h = new AtomicInteger(((Integer) n7.y.f31093e.f31096c.b(ao.I)).intValue());
                } else {
                    try {
                        this.zzf.registerDefaultNetworkCallback(new m4.e(this));
                    } catch (RuntimeException unused2) {
                        this.f10779h = new AtomicInteger(((Integer) n7.y.f31093e.f31096c.b(ao.I)).intValue());
                    }
                }
                m7.n.C.f30468g.b(new rk(this));
            }
            ArrayList<n7.p3> arrayListD = d(arrayList);
            EnumMap enumMap = new EnumMap(AdFormat.class);
            for (n7.p3 p3Var : arrayListD) {
                String str = p3Var.f31009a;
                AdFormat adFormat = AdFormat.getAdFormat(p3Var.f31010b);
                lf1 lf1VarZza = this.f10774c.zza(p3Var, x0Var);
                if (adFormat != null && lf1VarZza != null) {
                    AtomicInteger atomicInteger = this.f10779h;
                    if (atomicInteger != null) {
                        lf1VarZza.o(atomicInteger.get());
                    }
                    cf1 cf1Var = this.f10775d;
                    lf1VarZza.f13674o = cf1Var;
                    String strZzh = zzh(str, adFormat);
                    synchronized (this) {
                        synchronized (lf1VarZza) {
                            lf1VarZza.f13670k.submit(new if1(lf1VarZza, 0));
                        }
                        this.f10772a.put(strZzh, lf1VarZza);
                    }
                }
            }
            cf1 cf1Var2 = this.f10775d;
            ((i8.d) this.f10778g).getClass();
            cf1Var2.b(enumMap, System.currentTimeMillis());
        } catch (Throwable th) {
            throw th;
        }
        if (com.bumptech.glide.g.l()) {
            this.f10779h = new AtomicInteger(((Integer) n7.y.f31093e.f31096c.b(ao.I)).intValue());
            m7.n.C.f30468g.b(new rk(this));
        }
        ArrayList<n7.p3> arrayListD2 = d(arrayList);
        EnumMap enumMap2 = new EnumMap(AdFormat.class);
        while (r12.hasNext()) {
        }
        cf1 cf1Var22 = this.f10775d;
        ((i8.d) this.f10778g).getClass();
        cf1Var22.b(enumMap2, System.currentTimeMillis());
    }

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

    public final synchronized void c(boolean z10) {
        try {
            if (z10) {
                Iterator it = this.f10772a.values().iterator();
                while (it.hasNext()) {
                    ((lf1) it.next()).n();
                }
            } else {
                Iterator it2 = this.f10772a.values().iterator();
                while (it2.hasNext()) {
                    ((lf1) it2.next()).f13665f.set(false);
                }
            }
        } catch (Throwable th) {
            throw th;
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:34:0x00fe A[Catch: all -> 0x0048, TRY_LEAVE, TryCatch #0 {all -> 0x0048, blocks: (B:3:0x0001, B:4:0x000f, B:6:0x0015, B:8:0x0034, B:10:0x003c, B:13:0x004b, B:14:0x0051, B:16:0x0059, B:18:0x0067, B:19:0x0076, B:20:0x007a, B:21:0x007e, B:22:0x0088, B:24:0x008e, B:26:0x00a0, B:27:0x00b5, B:28:0x00bf, B:30:0x00c5, B:32:0x00ee, B:37:0x0105, B:39:0x010b, B:34:0x00fe, B:36:0x0104, B:41:0x0110, B:42:0x0111, B:35:0x00ff), top: B:47:0x0001, inners: #1 }] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final synchronized ArrayList d(ArrayList arrayList) {
        ArrayList arrayList2;
        try {
            HashSet hashSet = new HashSet();
            arrayList2 = new ArrayList();
            Iterator it = arrayList.iterator();
            while (it.hasNext()) {
                n7.p3 p3Var = (n7.p3) it.next();
                String strZzh = zzh(p3Var.f31009a, AdFormat.getAdFormat(p3Var.f31010b));
                hashSet.add(strZzh);
                ConcurrentHashMap concurrentHashMap = this.f10772a;
                lf1 lf1Var = (lf1) concurrentHashMap.get(strZzh);
                if (lf1Var == null) {
                    ConcurrentHashMap concurrentHashMap2 = this.f10773b;
                    if (concurrentHashMap2.containsKey(strZzh)) {
                        lf1 lf1Var2 = (lf1) concurrentHashMap2.get(strZzh);
                        if (lf1Var2.f13664e.equals(p3Var)) {
                            lf1Var2.t(p3Var.f31012d);
                            lf1Var2.n();
                            concurrentHashMap.put(strZzh, lf1Var2);
                            concurrentHashMap2.remove(strZzh);
                        } else {
                            arrayList2.add(p3Var);
                        }
                    } else {
                        arrayList2.add(p3Var);
                    }
                } else if (lf1Var.f13664e.equals(p3Var)) {
                    lf1Var.t(p3Var.f31012d);
                } else {
                    this.f10773b.put(strZzh, lf1Var);
                    concurrentHashMap.remove(strZzh);
                    arrayList2.add(p3Var);
                }
            }
            Iterator it2 = this.f10772a.entrySet().iterator();
            while (it2.hasNext()) {
                Map.Entry entry = (Map.Entry) it2.next();
                if (!hashSet.contains((String) entry.getKey())) {
                    this.f10773b.put((String) entry.getKey(), (lf1) entry.getValue());
                    it2.remove();
                }
            }
            Iterator it3 = this.f10773b.entrySet().iterator();
            while (it3.hasNext()) {
                lf1 lf1Var3 = (lf1) ((Map.Entry) it3.next()).getValue();
                lf1Var3.f13665f.set(false);
                lf1Var3.f13672m.set(false);
                tn tnVar = ao.A;
                n7.y yVar = n7.y.f31093e;
                if (!((Boolean) yVar.f31096c.b(tnVar)).booleanValue()) {
                    if (((Boolean) yVar.f31096c.b(ao.B)).booleanValue()) {
                        synchronized (lf1Var3) {
                            lf1Var3.f13666g.clear();
                        }
                    }
                }
                if (!lf1Var3.m()) {
                    it3.remove();
                }
            }
        } catch (Throwable th) {
            throw th;
        }
        return arrayList2;
    }

    public final synchronized boolean e(AdFormat adFormat, String str) {
        boolean z10;
        try {
            ((i8.d) this.f10778g).getClass();
            long jCurrentTimeMillis = System.currentTimeMillis();
            lf1 lf1VarZzq = zzq(str, adFormat);
            z10 = lf1VarZzq != null && lf1VarZzq.m();
            this.f10775d.zzd(lf1VarZzq == null ? 0 : lf1VarZzq.f13664e.f31012d, lf1VarZzq != null ? lf1VarZzq.p() : 0, jCurrentTimeMillis, z10 ? Long.valueOf(System.currentTimeMillis()) : null, lf1VarZzq == null ? null : lf1VarZzq.zzk(), new gf1(new ff1(str, adFormat)), "1");
        } catch (Throwable th) {
            throw th;
        }
        return z10;
    }

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

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

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