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

import android.text.TextUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import tv.danmaku.ijk.media.player.IjkMediaMeta;

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

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

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

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public long f15241f;

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

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

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public boolean f15245j;

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

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

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

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public final ArrayList f15244i = new ArrayList();

    public p20(String str, long j10) {
        JSONObject jSONObjectOptJSONObject;
        JSONObject jSONObjectOptJSONObject2;
        JSONArray jSONArrayOptJSONArray;
        JSONObject jSONObjectOptJSONObject3;
        this.f15239d = "";
        this.f15243h = false;
        this.f15245j = false;
        this.f15240e = str;
        this.f15241f = j10;
        if (TextUtils.isEmpty(str)) {
            return;
        }
        try {
            this.f15242g = new JSONObject(str);
            if (((Boolean) n7.y.f31093e.f31096c.b(ao.Rc)).booleanValue() && a()) {
                return;
            }
            if (this.f15242g.optInt("status", -1) != 1) {
                this.f15243h = false;
                q7.j.d("App settings could not be fetched successfully.");
                return;
            }
            this.f15243h = true;
            this.f15239d = this.f15242g.optString("app_id");
            JSONArray jSONArrayOptJSONArray2 = this.f15242g.optJSONArray("ad_unit_id_settings");
            if (jSONArrayOptJSONArray2 != null) {
                for (int i10 = 0; i10 < jSONArrayOptJSONArray2.length(); i10++) {
                    JSONObject jSONObject = jSONArrayOptJSONArray2.getJSONObject(i10);
                    String strOptString = jSONObject.optString(IjkMediaMeta.IJKM_KEY_FORMAT);
                    String strOptString2 = jSONObject.optString("ad_unit_id");
                    if (!TextUtils.isEmpty(strOptString) && !TextUtils.isEmpty(strOptString2)) {
                        if ("interstitial".equalsIgnoreCase(strOptString)) {
                            this.f15237b.add(strOptString2);
                        } else if (("rewarded".equalsIgnoreCase(strOptString) || "rewarded_interstitial".equals(strOptString)) && (jSONObjectOptJSONObject3 = jSONObject.optJSONObject("mediation_config")) != null) {
                            this.f15238c.put(strOptString2, new sv(jSONObjectOptJSONObject3));
                        }
                    }
                }
            }
            JSONArray jSONArrayOptJSONArray3 = this.f15242g.optJSONArray("persistable_banner_ad_unit_ids");
            if (jSONArrayOptJSONArray3 != null) {
                for (int i11 = 0; i11 < jSONArrayOptJSONArray3.length(); i11++) {
                    this.f15236a.add(jSONArrayOptJSONArray3.optString(i11));
                }
            }
            if (((Boolean) n7.y.f31093e.f31096c.b(ao.E7)).booleanValue() && (jSONObjectOptJSONObject2 = this.f15242g.optJSONObject("common_settings")) != null && (jSONArrayOptJSONArray = jSONObjectOptJSONObject2.optJSONArray("loeid")) != null) {
                for (int i12 = 0; i12 < jSONArrayOptJSONArray.length(); i12++) {
                    this.f15244i.add(jSONArrayOptJSONArray.get(i12).toString());
                }
            }
            if (!((Boolean) n7.y.f31093e.f31096c.b(ao.Z6)).booleanValue() || (jSONObjectOptJSONObject = this.f15242g.optJSONObject("common_settings")) == null) {
                return;
            }
            this.f15245j = jSONObjectOptJSONObject.optBoolean("is_prefetching_enabled", false);
        } catch (JSONException e10) {
            m7.n.C.f30469h.d("AppSettings.parseAppSettingsJson", e10);
        }
    }

    public final boolean a() {
        if (b()) {
            return false;
        }
        if (((Boolean) n7.y.f31093e.f31096c.b(ao.S4)).booleanValue()) {
            m7.n nVar = m7.n.C;
            cr0 cr0VarJ = nVar.f30469h.j();
            if (cr0VarJ != null) {
                br0 br0VarA = cr0VarJ.a();
                br0VarA.zzc("action", "cld_reset");
                br0VarA.zzc("cld_lut_ms", String.valueOf(this.f15241f));
                nVar.f30472k.getClass();
                br0VarA.zzc("event_timestamp", String.valueOf(System.currentTimeMillis()));
                br0VarA.zzc("cld_ttl_sec", String.valueOf(c()));
                br0VarA.b();
            }
        }
        this.f15236a.clear();
        this.f15237b.clear();
        this.f15238c.clear();
        this.f15239d = "";
        this.f15240e = "";
        this.f15242g = null;
        this.f15243h = false;
        this.f15244i.clear();
        this.f15245j = false;
        return true;
    }

    public final boolean b() {
        if (!TextUtils.isEmpty(this.f15240e) && this.f15242g != null) {
            long jC = c();
            m7.n.C.f30472k.getClass();
            long jCurrentTimeMillis = System.currentTimeMillis();
            if (jC >= 0) {
                long j10 = this.f15241f;
                if (j10 > jCurrentTimeMillis || TimeUnit.MILLISECONDS.toSeconds(jCurrentTimeMillis - j10) > jC) {
                    return false;
                }
            }
        }
        return true;
    }

    public final long c() {
        tn tnVar = ao.Uc;
        n7.y yVar = n7.y.f31093e;
        long jLongValue = ((Long) yVar.f31096c.b(tnVar)).longValue();
        tn tnVar2 = ao.Tc;
        yn ynVar = yVar.f31096c;
        return (!((Boolean) ynVar.b(tnVar2)).booleanValue() || TextUtils.isEmpty(this.f15240e)) ? jLongValue : this.f15242g.optLong("cache_ttl_sec", ((Long) ynVar.b(tnVar)).longValue());
    }
}
