package com.mbridge.msdk.foundation.db.a;

import android.text.TextUtils;
import com.mbridge.msdk.foundation.controller.c;
import com.mbridge.msdk.foundation.entity.g;
import com.mbridge.msdk.foundation.tools.af;
import com.mbridge.msdk.foundation.tools.an;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/* compiled from: FrequencyDaoMiddle.java */
/* loaded from: classes5.dex */
public class a {

    /* renamed from: a  reason: collision with root package name */
    private static String f3563a = "FrequencyDaoMiddle";
    private static a b = null;
    private static String c = "FrequencyDaoMiddle";
    private static JSONArray d = new JSONArray();

    public static a a() {
        if (b == null) {
            synchronized (a.class) {
                if (b == null) {
                    b = new a();
                }
            }
        }
        return b;
    }

    private a() {
        try {
            String str = (String) an.a(c.m().c(), c, d.toString());
            if (TextUtils.isEmpty(str)) {
                return;
            }
            d = new JSONArray(str);
        } catch (Exception e) {
            af.b(f3563a, e.getMessage());
        }
    }

    public final void a(g gVar) {
        JSONObject a2;
        if (gVar == null || (a2 = a(gVar.a(), gVar.c(), gVar.d(), gVar.f(), gVar.e(), gVar.b())) == null) {
            return;
        }
        if (d == null) {
            d = new JSONArray();
        }
        d.put(a2);
        c();
    }

    public final void a(String str) {
        if (d != null) {
            JSONArray jSONArray = new JSONArray();
            for (int i = 0; i < d.length(); i++) {
                try {
                    JSONObject jSONObject = d.getJSONObject(i);
                    if (jSONObject != null) {
                        if (jSONObject.optString("id", "").equals(str)) {
                            jSONObject.put("impression_count", jSONObject.optInt("impression_count", 0) + 1);
                            jSONArray.put(jSONObject);
                        } else {
                            jSONArray.put(jSONObject);
                        }
                    }
                } catch (JSONException e) {
                    af.b(f3563a, e.getMessage());
                }
            }
            if (jSONArray.length() > 0) {
                d = jSONArray;
            }
            c();
        }
    }

    public final String[] b() {
        ArrayList arrayList = new ArrayList();
        if (d != null) {
            for (int i = 0; i < d.length(); i++) {
                try {
                    JSONObject jSONObject = d.getJSONObject(i);
                    if (jSONObject != null && jSONObject.optInt("fc_a") < jSONObject.optInt("impression_count")) {
                        arrayList.add(jSONObject.optString("id"));
                    }
                } catch (JSONException e) {
                    af.b(f3563a, e.getMessage());
                }
            }
        }
        String[] strArr = new String[arrayList.size()];
        for (int i2 = 0; i2 < arrayList.size(); i2++) {
            strArr[i2] = (String) arrayList.get(i2);
        }
        return strArr;
    }

    public final void a(long j) {
        if (d != null) {
            JSONArray jSONArray = new JSONArray();
            for (int i = 0; i < d.length(); i++) {
                try {
                    JSONObject jSONObject = d.getJSONObject(i);
                    if (jSONObject != null && jSONObject.optInt("ts") >= j) {
                        jSONArray.put(jSONObject);
                    }
                } catch (JSONException e) {
                    af.b(f3563a, e.getMessage());
                }
            }
            if (jSONArray.length() > 0) {
                d = jSONArray;
            }
        }
        c();
    }

    private JSONObject a(String str, int i, int i2, long j, int i3, int i4) {
        JSONObject jSONObject;
        JSONObject jSONObject2 = null;
        try {
            jSONObject = new JSONObject();
        } catch (Exception e) {
            e = e;
        }
        try {
            jSONObject.put("id", str);
            jSONObject.put("fc_a", i);
            jSONObject.put("fc_b", i2);
            jSONObject.put("ts", j);
            jSONObject.put("impression_count", i3);
            jSONObject.put("click_count", i4);
            return jSONObject;
        } catch (Exception e2) {
            e = e2;
            jSONObject2 = jSONObject;
            af.b(f3563a, e.getMessage());
            return jSONObject2;
        }
    }

    private void c() {
        try {
            if (d != null) {
                an.b(c.m().c(), c, d.toString());
            }
        } catch (Exception e) {
            af.b(f3563a, e.getMessage());
        }
    }
}
