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

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Pair;
import androidx.annotation.Nullable;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

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

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    public static final Pattern f18213p = Pattern.compile("\\?");

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

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final q7.a f18216c;

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

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

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

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

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

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

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

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

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public final Object f18225l = new Object();

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public String f18226m;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public List f18227n;

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    public final a00 f18228o;

    @Nullable
    private Bundle zzp;

    public wt0(h70 h70Var, Context context, q7.a aVar, ib1 ib1Var, a30 a30Var, String str, md1 md1Var, zq0 zq0Var, a00 a00Var, ScheduledExecutorService scheduledExecutorService, re1 re1Var, eh0 eh0Var) {
        this.f18214a = h70Var;
        this.f18215b = context;
        this.f18216c = aVar;
        this.f18217d = ib1Var;
        this.f18218e = a30Var;
        this.f18220g = str;
        this.f18221h = md1Var;
        this.f18222i = zq0Var;
        this.f18228o = a00Var;
        this.f18219f = scheduledExecutorService;
        this.f18223j = re1Var;
        this.f18224k = eh0Var;
    }

    public static final String e(String str) {
        try {
            return new JSONObject(str).optString("request_id", "");
        } catch (JSONException unused) {
            return "";
        }
    }

    private final com.google.common.util.concurrent.w zzg(@Nullable String str, String str2) {
        com.google.common.util.concurrent.w wVarK;
        String str3 = "";
        if (TextUtils.isEmpty(str)) {
            return ih1.q0(new zzenv(15, "Invalid ad string."));
        }
        Context context = this.f18215b;
        id1 id1VarD = id1.d(context, 11);
        id1VarD.zza();
        jv jvVarZzb = m7.n.C.f30479r.zzb(context, this.f18216c, this.f18214a.c());
        va2 va2Var = za2.f19208j;
        lv lvVarA = jvVarZzb.a("google.afma.response.normalize", va2Var, va2Var);
        boolean zBooleanValue = ((Boolean) n7.y.f31093e.f31096c.b(ao.R7)).booleanValue();
        Executor executor = this.f18218e;
        zq0 zq0Var = this.f18222i;
        if (zBooleanValue) {
            try {
                JSONObject jSONObject = new JSONObject(str);
                this.f18226m = jSONObject.optString("fetch_url", "");
                this.f18227n = p7.y.zza(new JSONObject(jSONObject.optString("settings", "")).getJSONArray("nofill_urls"), null);
            } catch (JSONException unused) {
                q7.j.d("Invalid ad response.");
            }
            String string = this.f18226m;
            List list = this.f18227n;
            if (TextUtils.isEmpty(string)) {
                wVarK = ih1.k(str);
                zq0Var.zzd("sst", "1");
            } else {
                zq0Var.zzd("sst", "2");
                tn tnVar = ao.T7;
                n7.y yVar = n7.y.f31093e;
                String str4 = (String) yVar.f31096c.b(tnVar);
                tn tnVar2 = ao.S7;
                if (((Boolean) yVar.f31096c.b(tnVar2)).booleanValue()) {
                    zzgqv zzgqvVar = new zzgqv(f18213p);
                    ih1.R0(!zzgqvVar.a("").f14704a.matches(), "The pattern may not match the empty string: %s", zzgqvVar);
                    List listN = new ru(new jc1(zzgqvVar, 11)).n(string);
                    if (listN.size() < 2) {
                        wVarK = ih1.q0(new zzenv(1, "Invalid fetch URL."));
                    } else {
                        str3 = (String) listN.get(1);
                        p7.r0 r0Var = m7.n.C.f30464c;
                        string = Uri.parse(string).buildUpon().query(null).build().toString();
                        wVarK = ih1.W0((wu1) ih1.Z0(wu1.s(b30.f9359a.g(new hg(this, new fv0(string, 60000, new HashMap(), str3.getBytes(StandardCharsets.UTF_8), str4), 7))), ((Integer) r8.b(ao.U7)).intValue(), TimeUnit.MILLISECONDS, this.f18219f), Exception.class, new nv(this, list, 7), executor);
                    }
                } else {
                    wVarK = ih1.W0((wu1) ih1.Z0(wu1.s(b30.f9359a.g(new hg(this, new fv0(string, 60000, new HashMap(), str3.getBytes(StandardCharsets.UTF_8), str4), 7))), ((Integer) r8.b(ao.U7)).intValue(), TimeUnit.MILLISECONDS, this.f18219f), Exception.class, new nv(this, list, 7), executor);
                }
            }
        } else {
            wVarK = ih1.k(str);
            zq0Var.zzd("sst", "1");
        }
        ju1 ju1VarE1 = ih1.e1(ih1.e1(ih1.e1(wVarK, new wt(str2), executor), new nv(this, lvVarA, 6), executor), new wt(this, 6), executor);
        ni.L(ju1VarE1, this.f18221h, id1VarD, false);
        ih1.z1(ju1VarE1, new dh0(this), b30.f9365g);
        return ju1VarE1;
    }

    /* JADX WARN: Removed duplicated region for block: B:107:0x0253 A[Catch: all -> 0x00dc, TRY_LEAVE, TryCatch #7 {all -> 0x00dc, blocks: (B:21:0x0078, B:23:0x009a, B:25:0x00a2, B:27:0x00af, B:29:0x00c7, B:33:0x00f3, B:36:0x00ff, B:38:0x0107, B:40:0x010d, B:44:0x0116, B:53:0x014e, B:47:0x012a, B:52:0x0139, B:55:0x0153, B:32:0x00df, B:56:0x0167, B:63:0x0184, B:66:0x018c, B:70:0x01ae, B:72:0x01c4, B:76:0x01e5, B:78:0x01fa, B:81:0x020e, B:86:0x0221, B:87:0x0222, B:88:0x022f, B:92:0x0233, B:93:0x0234, B:98:0x024a, B:110:0x025a, B:100:0x024c, B:103:0x024f, B:104:0x0250, B:105:0x0251, B:106:0x0252, B:107:0x0253, B:109:0x0259, B:117:0x026c, B:118:0x026d, B:77:0x01ef, B:73:0x01d7, B:69:0x019a, B:60:0x0174, B:61:0x0179, B:82:0x020f, B:84:0x0219, B:108:0x0254, B:94:0x0235, B:96:0x023f), top: B:158:0x0078, inners: #1, #4, #6, #8, #9 }] */
    /* JADX WARN: Removed duplicated region for block: B:114:0x0262  */
    /* JADX WARN: Removed duplicated region for block: B:152:0x0254 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:76:0x01e5 A[Catch: all -> 0x00dc, TryCatch #7 {all -> 0x00dc, blocks: (B:21:0x0078, B:23:0x009a, B:25:0x00a2, B:27:0x00af, B:29:0x00c7, B:33:0x00f3, B:36:0x00ff, B:38:0x0107, B:40:0x010d, B:44:0x0116, B:53:0x014e, B:47:0x012a, B:52:0x0139, B:55:0x0153, B:32:0x00df, B:56:0x0167, B:63:0x0184, B:66:0x018c, B:70:0x01ae, B:72:0x01c4, B:76:0x01e5, B:78:0x01fa, B:81:0x020e, B:86:0x0221, B:87:0x0222, B:88:0x022f, B:92:0x0233, B:93:0x0234, B:98:0x024a, B:110:0x025a, B:100:0x024c, B:103:0x024f, B:104:0x0250, B:105:0x0251, B:106:0x0252, B:107:0x0253, B:109:0x0259, B:117:0x026c, B:118:0x026d, B:77:0x01ef, B:73:0x01d7, B:69:0x019a, B:60:0x0174, B:61:0x0179, B:82:0x020f, B:84:0x0219, B:108:0x0254, B:94:0x0235, B:96:0x023f), top: B:158:0x0078, inners: #1, #4, #6, #8, #9 }] */
    /* JADX WARN: Removed duplicated region for block: B:77:0x01ef A[Catch: all -> 0x00dc, TryCatch #7 {all -> 0x00dc, blocks: (B:21:0x0078, B:23:0x009a, B:25:0x00a2, B:27:0x00af, B:29:0x00c7, B:33:0x00f3, B:36:0x00ff, B:38:0x0107, B:40:0x010d, B:44:0x0116, B:53:0x014e, B:47:0x012a, B:52:0x0139, B:55:0x0153, B:32:0x00df, B:56:0x0167, B:63:0x0184, B:66:0x018c, B:70:0x01ae, B:72:0x01c4, B:76:0x01e5, B:78:0x01fa, B:81:0x020e, B:86:0x0221, B:87:0x0222, B:88:0x022f, B:92:0x0233, B:93:0x0234, B:98:0x024a, B:110:0x025a, B:100:0x024c, B:103:0x024f, B:104:0x0250, B:105:0x0251, B:106:0x0252, B:107:0x0253, B:109:0x0259, B:117:0x026c, B:118:0x026d, B:77:0x01ef, B:73:0x01d7, B:69:0x019a, B:60:0x0174, B:61:0x0179, B:82:0x020f, B:84:0x0219, B:108:0x0254, B:94:0x0235, B:96:0x023f), top: B:158:0x0078, inners: #1, #4, #6, #8, #9 }] */
    /* JADX WARN: Removed duplicated region for block: B:80:0x020c A[ADDED_TO_REGION] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final com.google.common.util.concurrent.w a() {
        String strOptString;
        int i10;
        String str;
        String string;
        tn tnVar = ao.D2;
        n7.y yVar = n7.y.f31093e;
        if (((Boolean) yVar.f31096c.b(tnVar)).booleanValue()) {
            this.zzp = this.f18217d.f12466o;
            com.bytedance.sdk.component.adexpress.dynamic.dynamicview.b.n(m7.n.C.f30472k, this.f18222i.f19368e, zzdxh.SCAR_PRELOADER_READY.a());
        }
        String strA = this.f18217d.f12452a.zzx;
        if (!TextUtils.isEmpty(strA)) {
            String strE = e(strA);
            tn tnVar2 = ao.N7;
            if (((Boolean) yVar.f31096c.b(tnVar2)).booleanValue() && strE.isEmpty()) {
                int iLastIndexOf = strA.lastIndexOf("&request_id=");
                strE = iLastIndexOf != -1 ? strA.substring(iLastIndexOf + 12) : "";
            }
            if (TextUtils.isEmpty(strE)) {
                return ih1.q0(new zzenv(15, "Invalid ad string."));
            }
            synchronized (this.f18225l) {
                try {
                    com.google.android.gms.ads.nonagon.signalgeneration.r0 r0Var = (com.google.android.gms.ads.nonagon.signalgeneration.r0) ((a80) this.f18214a).T.zzb();
                    zq0 zq0Var = this.f18222i;
                    String strZzb = r0Var.zzb(strE, zq0Var);
                    String str2 = null;
                    if (((Boolean) yVar.f31096c.b(tnVar2)).booleanValue() && !TextUtils.isEmpty(strZzb)) {
                        try {
                            JSONObject jSONObjectOptJSONObject = new JSONObject(strZzb).optJSONObject("extras");
                            if (jSONObjectOptJSONObject != null) {
                                if ((((Boolean) yVar.f31096c.b(ao.P7)).booleanValue() ? Arrays.asList(((String) yVar.f31096c.b(ao.Q7)).split(",")) : Arrays.asList(((String) yVar.f31096c.b(ao.O7)).split(","))).contains(com.google.android.gms.ads.nonagon.signalgeneration.b.zzb(jSONObjectOptJSONObject.optString("query_info_type", "")))) {
                                    int iLastIndexOf2 = strA.lastIndexOf("&");
                                    String strSubstring = iLastIndexOf2 != -1 ? strA.substring(0, iLastIndexOf2) : null;
                                    if (!TextUtils.isEmpty(strSubstring)) {
                                        try {
                                            byte[] bArrDecode = Base64.decode(strSubstring, 11);
                                            byte[] bytes = strE.getBytes(StandardCharsets.UTF_8);
                                            if (TextUtils.isEmpty(strZzb)) {
                                                string = null;
                                                strA = nb1.a(bArrDecode, bytes, string, zq0Var);
                                            } else {
                                                try {
                                                    string = new JSONObject(strZzb).getString("arek");
                                                } catch (JSONException e10) {
                                                    p7.l0.g("Failed to get key from QueryJSONMap".concat(e10.toString()));
                                                    m7.n.C.f30469h.d("CryptoUtils.getKeyFromQueryJsonMap", e10);
                                                    string = null;
                                                }
                                                strA = nb1.a(bArrDecode, bytes, string, zq0Var);
                                            }
                                        } catch (IllegalArgumentException e11) {
                                            p7.l0.g("Failed to decode the adResponse. ".concat(e11.toString()));
                                            m7.n.C.f30469h.d("PreloadedLoader.decryptAdResponseIfNecessary", e11);
                                        }
                                    }
                                }
                            }
                        } catch (JSONException unused) {
                        }
                    }
                    if (TextUtils.isEmpty(strA)) {
                        strOptString = "";
                    } else {
                        try {
                            strOptString = new JSONObject(strA).optString("render_id", "");
                        } catch (JSONException unused2) {
                            strOptString = "";
                        }
                    }
                    if (TextUtils.isEmpty(strOptString)) {
                        i10 = 0;
                        if (str2 != null) {
                        }
                        str = (String) pair.first;
                        int iIntValue = ((Integer) pair.second).intValue();
                        if (TextUtils.isEmpty(str)) {
                        }
                    } else {
                        String str3 = "";
                        try {
                            str3 = new String(Base64.decode(strOptString, 0), StandardCharsets.UTF_8);
                        } catch (IllegalArgumentException e12) {
                            p7.l0.g("Ad grouping: Has render_id, but not base64 encoded: ".concat(String.valueOf(strOptString)));
                            m7.n.C.f30469h.d("PreloadedLoader.decodeRenderId", e12);
                        }
                        List listN = ru.c(new kq1(':')).n(str3);
                        if (listN.size() == 2) {
                            str2 = (String) listN.get(0);
                            i10 = Integer.parseInt((String) listN.get(1));
                            Pair pair = str2 != null ? new Pair(str2, Integer.valueOf(i10)) : new Pair("", 0);
                            str = (String) pair.first;
                            int iIntValue2 = ((Integer) pair.second).intValue();
                            if (TextUtils.isEmpty(str) || iIntValue2 <= 0) {
                                synchronized (r0Var) {
                                    r0Var.f8128e.remove(strE);
                                }
                                if (!TextUtils.isEmpty(strZzb)) {
                                    return zzg(strA, d(strZzb));
                                }
                            } else {
                                synchronized (r0Var) {
                                    try {
                                        com.google.android.gms.ads.nonagon.signalgeneration.q0 q0Var = (com.google.android.gms.ads.nonagon.signalgeneration.q0) r0Var.f8128e.get(strE);
                                        if (q0Var != null) {
                                            if (q0Var.f8122c.contains(str)) {
                                                return ih1.q0(new zzenv(10, "The ad has already been shown."));
                                            }
                                        }
                                        synchronized (r0Var) {
                                            com.google.android.gms.ads.nonagon.signalgeneration.q0 q0Var2 = (com.google.android.gms.ads.nonagon.signalgeneration.q0) r0Var.f8128e.get(strE);
                                            if (q0Var2 != null) {
                                                Set set = q0Var2.f8122c;
                                                set.add(str);
                                                if (set.size() < iIntValue2) {
                                                }
                                            }
                                            synchronized (r0Var) {
                                            }
                                        }
                                        if (!TextUtils.isEmpty(strZzb)) {
                                        }
                                    } finally {
                                    }
                                }
                            }
                        } else {
                            p7.l0.g("Ad grouping: Has render_id, but invalid format: ".concat(String.valueOf(strOptString)));
                            i10 = 0;
                            if (str2 != null) {
                            }
                            str = (String) pair.first;
                            int iIntValue22 = ((Integer) pair.second).intValue();
                            if (TextUtils.isEmpty(str)) {
                                synchronized (r0Var) {
                                }
                            }
                        }
                    }
                } catch (Throwable th) {
                    throw th;
                }
            }
        }
        n7.u0 u0Var = this.f18217d.f12452a.zzs;
        if (u0Var != null) {
            if (((Boolean) n7.y.f31093e.f31096c.b(ao.F7)).booleanValue()) {
                String str4 = u0Var.f31047a;
                String str5 = u0Var.f31048b;
                String strE2 = e(str4);
                String strE3 = e(str5);
                if (TextUtils.isEmpty(strE3) || !strE2.equals(strE3)) {
                    this.f18222i.f19364a.put("ridmm", "true");
                } else {
                    com.google.android.gms.ads.nonagon.signalgeneration.r0 r0Var2 = (com.google.android.gms.ads.nonagon.signalgeneration.r0) ((a80) this.f18214a).T.zzb();
                    synchronized (r0Var2) {
                        r0Var2.f8128e.remove(strE2);
                    }
                    this.f18222i.f19364a.put("request_id", strE2);
                }
            }
            return zzg(u0Var.f31047a, d(u0Var.f31048b));
        }
        return ih1.q0(new zzenv(14, "Mismatch request IDs."));
    }

    public final /* synthetic */ bv1 b(JSONObject jSONObject) {
        dh0 dh0Var = new dh0(this.f18217d, 29);
        String string = jSONObject.toString();
        return ih1.k(new eb1(dh0Var, db1.zza(new StringReader(string), this.zzp)));
    }

    public final void c(zzdxh zzdxhVar) {
        Bundle bundle = this.f18222i.f19368e;
        if (((Boolean) n7.y.f31093e.f31096c.b(ao.W7)).booleanValue()) {
            com.bytedance.sdk.component.adexpress.dynamic.dynamicview.b.n(m7.n.C.f30472k, bundle, zzdxhVar.a());
        }
    }

    public final String d(String str) {
        try {
            JSONObject jSONObject = new JSONObject(str);
            JSONArray jSONArray = jSONObject.getJSONArray("ad_types");
            if (jSONArray != null && "unknown".equals(jSONArray.getString(0))) {
                jSONObject.put("ad_types", new JSONArray().put(this.f18220g));
            }
            return jSONObject.toString();
        } catch (JSONException e10) {
            q7.j.d("Failed to update the ad types for rendering. ".concat(e10.toString()));
            return str;
        }
    }
}
