package com.ironsource.mediationsdk;

import android.os.Build;
import android.security.NetworkSecurityPolicy;
import android.text.TextUtils;
import com.google.android.exoplayer2.C;
import com.google.firebase.remoteconfig.FirebaseRemoteConfig;
import com.ironsource.c5;
import com.ironsource.environment.ContextProvider;
import com.ironsource.environment.thread.IronSourceThreadManager;
import com.ironsource.j5;
import com.ironsource.jj;
import com.ironsource.jl;
import com.ironsource.l9;
import com.ironsource.md;
import com.ironsource.mediationsdk.IronSource;
import com.ironsource.mediationsdk.demandOnly.p;
import com.ironsource.mediationsdk.logger.IronLog;
import com.ironsource.mediationsdk.utils.IronSourceConstants;
import com.ironsource.mediationsdk.utils.IronSourceUtils;
import com.ironsource.ob;
import com.ironsource.oe;
import com.ironsource.s4;
import com.ironsource.u2;
import com.ironsource.vp;
import com.ironsource.y4;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/* loaded from: classes4.dex */
public class d {
    public static final boolean A = false;
    private static d B = new d();
    public static final String c = "auctionId";
    public static final String d = "armData";
    public static final String e = "larmData";
    public static final String f = "isAdUnitCapped";
    public static final String g = "settings";
    public static final String h = "waterfall";
    public static final String i = "genericParams";
    public static final String j = "configurations";
    public static final String k = "instances";
    public static final String l = "${AUCTION_LOSS}";
    public static final String m = "${AUCTION_MBR}";
    public static final String n = "${AUCTION_PRICE}";
    public static final String o = "${DYNAMIC_DEMAND_SOURCE}";
    public static final String p = "${INSTANCE}";
    public static final String q = "${INSTANCE_TYPE}";
    public static final String r = "${PLACEMENT_NAME}";
    private static final String s = "adMarkup";
    private static final String t = "dynamicDemandSource";
    private static final String u = "params";
    public static final String v = "dlpl";
    public static final String w = "adUnit";
    public static final String x = "parallelLoad";
    public static final String y = "bidderExclusive";
    public static final String z = "showPriorityEnabled";

    /* renamed from: a  reason: collision with root package name */
    private final AtomicBoolean f2603a = new AtomicBoolean(false);
    private final oe b = jl.P().f();

    /* loaded from: classes4.dex */
    public static class a {

        /* renamed from: a  reason: collision with root package name */
        private String f2604a;
        private List<j5> b;
        private j5 c;
        private JSONObject d;
        private JSONObject e;
        private int f;
        private String g;
        private s4 h;

        public a(String str) {
            this.f2604a = str;
        }

        public com.ironsource.mediationsdk.demandOnly.p a(String str) {
            s4 s4Var = this.h;
            return s4Var != null ? s4Var.a(str) : new p.b();
        }

        public String a() {
            return this.f2604a;
        }

        public JSONObject b() {
            return this.e;
        }

        public int c() {
            return this.f;
        }

        public String d() {
            return this.g;
        }

        public j5 e() {
            return this.c;
        }

        public JSONObject f() {
            return this.d;
        }

        public s4 g() {
            return this.h;
        }

        public List<j5> h() {
            return this.b;
        }
    }

    /* loaded from: classes4.dex */
    static class b implements Runnable {
        private static final int d = 15000;

        /* renamed from: a  reason: collision with root package name */
        private String f2605a;
        private String b;
        private String c;

        public b(String str, String str2, String str3) {
            this.f2605a = str;
            this.b = str2;
            this.c = str3;
        }

        @Override // java.lang.Runnable
        public void run() {
            String str = this.f2605a + ";" + this.b + ";" + this.c;
            try {
                HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(this.c).openConnection();
                httpURLConnection.setRequestMethod("GET");
                httpURLConnection.setReadTimeout(15000);
                httpURLConnection.setConnectTimeout(15000);
                httpURLConnection.connect();
                int responseCode = httpURLConnection.getResponseCode();
                String responseMessage = httpURLConnection.getResponseMessage();
                httpURLConnection.disconnect();
                if (responseCode != 200 && responseCode != 204) {
                    JSONObject jSONObject = new JSONObject();
                    jSONObject.put(IronSourceConstants.EVENTS_PROVIDER, "Mediation");
                    jSONObject.put(IronSourceConstants.EVENTS_PROGRAMMATIC, 1);
                    jSONObject.put(IronSourceConstants.EVENTS_EXT1, str);
                    jSONObject.put("errorCode", responseCode);
                    jSONObject.put("reason", responseMessage);
                    vp.i().a(new ob(IronSourceConstants.TROUBLESHOOTING_FAILED_TO_SEND_AUCTION_URL, jSONObject));
                }
            } catch (Exception e) {
                l9.d().a(e);
                IronLog.INTERNAL.error("Send auction url failed with params - " + str + ";" + e.getMessage());
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes4.dex */
    public enum c {
        NOT_SECURE,
        SECURE
    }

    private c a() {
        c cVar = c.SECURE;
        if (Build.VERSION.SDK_INT >= 28) {
            if (!NetworkSecurityPolicy.getInstance().isCleartextTrafficPermitted()) {
                return cVar;
            }
        } else if ((ContextProvider.getInstance().getApplicationContext().getApplicationInfo().flags & C.BUFFER_FLAG_FIRST_SAMPLE) == 0) {
            return cVar;
        }
        return c.NOT_SECURE;
    }

    private String a(String str, String str2) {
        if (TextUtils.isEmpty(str) || TextUtils.isEmpty(str2)) {
            return "";
        }
        double parseDouble = Double.parseDouble(str);
        double parseDouble2 = Double.parseDouble(str2);
        return parseDouble2 == FirebaseRemoteConfig.DEFAULT_VALUE_FOR_DOUBLE ? "" : String.valueOf(Math.round((parseDouble / parseDouble2) * 1000.0d) / 1000.0d);
    }

    public static d b() {
        return B;
    }

    public a a(JSONObject jSONObject) throws JSONException {
        String optString = jSONObject.optString("auctionId");
        if (TextUtils.isEmpty(optString)) {
            throw new JSONException("Invalid auction response - auction id is missing");
        }
        a aVar = new a(optString);
        if (jSONObject.has("settings")) {
            JSONObject jSONObject2 = jSONObject.getJSONObject("settings");
            aVar.c = new j5(jSONObject2);
            r3 = jSONObject2.has(d) ? jSONObject2.optJSONObject(d) : null;
            if (jSONObject2.has("genericParams")) {
                aVar.d = jSONObject2.optJSONObject("genericParams");
            }
            if (jSONObject2.has("configurations")) {
                aVar.e = jSONObject2.optJSONObject("configurations");
            }
            if (jSONObject2.has(k)) {
                aVar.h = new s4.a(jSONObject2.optJSONObject(k));
            }
        }
        aVar.b = new ArrayList();
        if (jSONObject.has(h)) {
            JSONArray jSONArray = jSONObject.getJSONArray(h);
            for (int i2 = 0; i2 < jSONArray.length(); i2++) {
                j5 j5Var = new j5(jSONArray.getJSONObject(i2), i2, r3);
                if (!j5Var.m()) {
                    aVar.f = 1002;
                    aVar.g = "waterfall " + i2;
                    IronLog.INTERNAL.verbose("AuctionResponseItem " + i2 + " not valid - parsing error");
                    throw new JSONException("invalid response");
                }
                aVar.b.add(j5Var);
            }
        }
        return aVar;
    }

    public String a(String str) {
        try {
            if (TextUtils.isEmpty(str)) {
                return str;
            }
            JSONObject jSONObject = new JSONObject(str);
            return jSONObject.has("adMarkup") ? jSONObject.getString("adMarkup") : str;
        } catch (JSONException e2) {
            l9.d().a(e2);
            IronLog.INTERNAL.error("exception " + e2.getMessage());
            return str;
        }
    }

    public String a(String str, int i2, j5 j5Var, String str2, String str3, String str4) {
        String i3 = j5Var.i();
        return a(str, j5Var.c(), i2, b().c(j5Var.k()), i3, b().a(i3, str2), str3, str4);
    }

    public String a(String str, String str2, int i2, String str3, String str4, String str5, String str6, String str7) {
        return str.replace(n, str4).replace(l, str6).replace(m, str5).replace(p, str2).replace(q, Integer.toString(i2)).replace(o, str3).replace(r, str7);
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public JSONObject a(i iVar) throws JSONException {
        boolean z2;
        boolean z3;
        ISBannerSize iSBannerSize;
        IronSource.AD_UNIT c2 = iVar.c();
        boolean s2 = iVar.s();
        Map<String, Object> g2 = iVar.g();
        List<String> k2 = iVar.k();
        h e2 = iVar.e();
        int n2 = iVar.n();
        ISBannerSize f2 = iVar.f();
        IronSourceSegment m2 = iVar.m();
        boolean o2 = iVar.o();
        boolean p2 = iVar.p();
        ArrayList<c5> j2 = iVar.j();
        JSONObject jSONObject = new JSONObject();
        JSONObject jSONObject2 = new JSONObject();
        Iterator<String> it = g2.keySet().iterator();
        while (true) {
            z2 = o2;
            z3 = s2;
            String str = "";
            iSBannerSize = f2;
            if (!it.hasNext()) {
                break;
            }
            String next = it.next();
            Iterator<String> it2 = it;
            JSONObject jSONObject3 = new JSONObject();
            IronSourceSegment ironSourceSegment = m2;
            jSONObject3.put(md.n0, 2);
            jSONObject3.put(md.e0, new JSONObject((Map) g2.get(next)));
            if (e2 != null) {
                str = e2.a(next);
            }
            jSONObject3.put(md.q0, str);
            jSONObject3.put("ts", p2 ? 1 : 0);
            jSONObject2.put(next, jSONObject3);
            o2 = z2;
            s2 = z3;
            f2 = iSBannerSize;
            it = it2;
            m2 = ironSourceSegment;
        }
        IronSourceSegment ironSourceSegment2 = m2;
        int i2 = 2;
        for (String str2 : k2) {
            JSONObject jSONObject4 = new JSONObject();
            jSONObject4.put(md.n0, 1);
            jSONObject4.put(md.q0, e2 != null ? e2.a(str2) : "");
            jSONObject2.put(str2, jSONObject4);
        }
        Iterator<c5> it3 = j2.iterator();
        while (it3.hasNext()) {
            c5 next2 = it3.next();
            JSONObject jSONObject5 = new JSONObject();
            jSONObject5.put(md.n0, next2.e() ? i2 : 1);
            Map<String, Object> f3 = next2.f();
            if (!f3.isEmpty()) {
                jSONObject5.put(md.e0, new JSONObject(f3));
            }
            jSONObject5.put(md.q0, e2 != null ? e2.a(next2.g()) : "");
            jSONObject5.put("ts", p2 ? 1 : 0);
            if (!next2.h().isEmpty()) {
                jSONObject5.put(v, next2.h());
            }
            jSONObject2.put(next2.g(), jSONObject5);
            i2 = 2;
        }
        jSONObject.put(md.m0, jSONObject2);
        if (iVar.u()) {
            jSONObject.put(md.f1, 1);
        }
        if (iVar.r()) {
            jSONObject.put("do", 1);
        }
        JSONObject a2 = new y4(u2.a(c2)).a();
        a(a2, false);
        a2.put(md.o0, n2);
        a2.put(md.p0, a().ordinal());
        if (ironSourceSegment2 != null) {
            a2.put(md.R0, ironSourceSegment2.toJson());
        }
        jSONObject.put(md.j0, a2);
        if (iSBannerSize != null) {
            JSONObject jSONObject6 = new JSONObject();
            jSONObject6.put(md.g0, iSBannerSize.getDescription());
            jSONObject6.put(md.i0, iSBannerSize.getWidth());
            jSONObject6.put(md.h0, iSBannerSize.getHeight());
            jSONObject.put(md.f0, jSONObject6);
        }
        jSONObject.put(md.a0, c2.toString());
        if (iVar.b() != null) {
            jSONObject.put("adf", iVar.b());
        }
        if (iVar.d() != null) {
            jSONObject.put("mediationAdUnitId", iVar.d());
        }
        if (iVar.t() != null) {
            jSONObject.put(md.d0, iVar.t());
        }
        jSONObject.put(md.k0, !z3);
        Object remove = a2.remove(md.b1);
        if (remove != null) {
            jSONObject.put(md.b1, remove);
        }
        if (z2) {
            jSONObject.put(md.Z0, 1);
        }
        return jSONObject;
    }

    public void a(String str, String str2, String str3) {
        IronSourceThreadManager.INSTANCE.postMediationBackgroundTask(new b(str, str2, str3));
    }

    public void a(JSONObject jSONObject, boolean z2) {
        if (jSONObject == null || jSONObject.length() <= 0 || TextUtils.isEmpty(jSONObject.optString(md.T0)) || !this.f2603a.compareAndSet(false, true)) {
            return;
        }
        vp.i().a(new ob(IronSourceConstants.TROUBLESHOOTING_MEDIATION_TCS_CALCULATED, IronSourceUtils.getMediationAdditionalData(z2, true, -1)));
    }

    public Map<String, String> b(String str) {
        HashMap hashMap = new HashMap();
        try {
            JSONObject jSONObject = new JSONObject(str);
            if (jSONObject.has("params")) {
                JSONObject jSONObject2 = jSONObject.getJSONObject("params");
                Iterator<String> keys = jSONObject2.keys();
                while (keys.hasNext()) {
                    String next = keys.next();
                    Object obj = jSONObject2.get(next);
                    if (obj instanceof String) {
                        hashMap.put(next, (String) obj);
                    }
                }
            }
        } catch (JSONException e2) {
            l9.d().a(e2);
            IronLog.INTERNAL.error("exception " + e2.getMessage());
        }
        return hashMap;
    }

    public String c(String str) {
        String str2 = "";
        try {
            if (TextUtils.isEmpty(str) || !jj.a(str)) {
                return "";
            }
            JSONObject jSONObject = new JSONObject(str);
            if (jSONObject.has("params")) {
                JSONObject jSONObject2 = jSONObject.getJSONObject("params");
                IronLog ironLog = IronLog.INTERNAL;
                ironLog.verbose("parameters = " + jSONObject2);
                if (jSONObject2.has("dynamicDemandSource")) {
                    str2 = jSONObject2.getString("dynamicDemandSource");
                    ironLog.verbose("demand source = " + str2);
                    return str2;
                }
                return "";
            }
            return "";
        } catch (JSONException e2) {
            l9.d().a(e2);
            IronLog.INTERNAL.error("exception " + e2.getMessage());
            return str2;
        }
    }
}
