package com.mbridge.msdk.mbsignalcommon.a;

import android.content.Context;
import android.util.Base64;
import com.ironsource.y8;
import com.mbridge.msdk.foundation.controller.c;
import com.mbridge.msdk.foundation.db.g;
import com.mbridge.msdk.foundation.db.o;
import com.mbridge.msdk.foundation.entity.n;
import com.mbridge.msdk.foundation.same.report.d.d;
import com.mbridge.msdk.foundation.tools.af;
import io.invertase.firebase.functions.UniversalFirebaseFunctionsModule;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/* compiled from: H5ReportManager.java */
/* loaded from: classes2.dex */
public class a {
    private static final String c = "a";

    /* renamed from: a  reason: collision with root package name */
    int f3867a = 0;
    int b = 1;

    /* compiled from: H5ReportManager.java */
    /* renamed from: com.mbridge.msdk.mbsignalcommon.a.a$a  reason: collision with other inner class name */
    /* loaded from: classes2.dex */
    private static final class C0286a {

        /* renamed from: a  reason: collision with root package name */
        private static a f3868a = new a();
    }

    public static a a() {
        return C0286a.f3868a;
    }

    public final void a(Object obj, String str, JSONArray jSONArray, int i) {
        JSONObject optJSONObject;
        JSONObject optJSONObject2;
        JSONObject optJSONObject3;
        JSONObject optJSONObject4;
        JSONObject optJSONObject5;
        JSONObject optJSONObject6;
        JSONObject optJSONObject7;
        JSONObject optJSONObject8;
        JSONObject optJSONObject9;
        JSONObject optJSONObject10;
        JSONObject optJSONObject11;
        int i2;
        try {
            if (jSONArray == null) {
                a(this.b, "called reporter failed, params empty", obj);
                return;
            }
            int length = jSONArray.length();
            if (length == 0) {
                a(this.b, "called reporter failed, params empty", obj);
                return;
            }
            int i3 = 0;
            if ("reportMessageR".equalsIgnoreCase(str)) {
                StringBuffer stringBuffer = new StringBuffer();
                while (true) {
                    i2 = length - 1;
                    if (i3 >= i2) {
                        break;
                    }
                    JSONObject optJSONObject12 = jSONArray.optJSONObject(i3);
                    if (optJSONObject12 != null) {
                        stringBuffer = stringBuffer.append(optJSONObject12.optString("key")).append("=").append(optJSONObject12.opt("value")).append(y8.i.c);
                    }
                    i3++;
                }
                JSONObject optJSONObject13 = jSONArray.optJSONObject(i2);
                if (optJSONObject13 != null) {
                    stringBuffer = stringBuffer.append(optJSONObject13.optString("key")).append("=").append(optJSONObject13.opt("value"));
                }
                d.a().c(stringBuffer.toString());
            } else if ("reportMessageD".equalsIgnoreCase(str)) {
                String str2 = "";
                String string = (length <= 0 || (optJSONObject11 = jSONArray.optJSONObject(0)) == null) ? "" : optJSONObject11.getString("value");
                int i4 = -1;
                int i5 = (length <= 1 || (optJSONObject10 = jSONArray.optJSONObject(1)) == null) ? -1 : optJSONObject10.getInt("value");
                int i6 = (length <= 2 || (optJSONObject9 = jSONArray.optJSONObject(2)) == null) ? -1 : optJSONObject9.getInt("value");
                int i7 = (length <= 3 || (optJSONObject8 = jSONArray.optJSONObject(3)) == null) ? -1 : optJSONObject8.getInt("value");
                int i8 = (length <= 4 || (optJSONObject7 = jSONArray.optJSONObject(4)) == null) ? -1 : optJSONObject7.getInt("value");
                String string2 = (length <= 5 || (optJSONObject6 = jSONArray.optJSONObject(5)) == null) ? "" : optJSONObject6.getString("value");
                String string3 = (length <= 6 || (optJSONObject5 = jSONArray.optJSONObject(6)) == null) ? "" : optJSONObject5.getString("value");
                int i9 = (length <= 7 || (optJSONObject4 = jSONArray.optJSONObject(7)) == null) ? -1 : optJSONObject4.getInt("value");
                String string4 = (length <= 8 || (optJSONObject3 = jSONArray.optJSONObject(8)) == null) ? "" : optJSONObject3.getString("value");
                if (length > 9 && (optJSONObject2 = jSONArray.optJSONObject(9)) != null) {
                    i4 = optJSONObject2.getInt("value");
                }
                int i10 = i4;
                if (length > 10 && (optJSONObject = jSONArray.optJSONObject(10)) != null) {
                    str2 = optJSONObject.getString("value");
                }
                String str3 = str2;
                Context c2 = c.m().c();
                if (c2 != null) {
                    o.a(g.a(c2)).a(new n(string, i5, i6, i7, i8, string2, string3, i9, string4, i10, str3));
                }
            }
            a(this.f3867a, "called reporter success", obj);
        } catch (Throwable th) {
            af.a(c, th.getMessage());
            a(this.b, "exception: " + th.getMessage(), obj);
        }
    }

    private void a(int i, String str, Object obj) {
        try {
            JSONObject jSONObject = new JSONObject();
            jSONObject.put(UniversalFirebaseFunctionsModule.CODE_KEY, i);
            jSONObject.put("message", str);
            com.mbridge.msdk.mbsignalcommon.windvane.g.a().a(obj, Base64.encodeToString(jSONObject.toString().getBytes(), 2));
        } catch (JSONException e) {
            af.a(c, e.getMessage());
        } catch (Throwable th) {
            af.a(c, th.getMessage());
        }
    }
}
