package rg;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import com.adobe.marketing.mobile.internal.util.g;
import java.io.File;
import wg.i0;
import wg.t;

/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes4.dex */
public class b {

    /* renamed from: a, reason: collision with root package name */
    private final Object f34783a;

    /* renamed from: b, reason: collision with root package name */
    private File f34784b;

    /* renamed from: c, reason: collision with root package name */
    private SQLiteDatabase f34785c;

    /* JADX INFO: Access modifiers changed from: package-private */
    public b() {
        Object obj = new Object();
        this.f34783a = obj;
        this.f34784b = null;
        this.f34785c = null;
        File d10 = d();
        this.f34784b = d10;
        if (d10 == null) {
            throw new d("An error occurred while creating the \"Events\" table in the Android Event History database, error message: ApplicationContext is null");
        }
        synchronized (obj) {
            if (!g.c(this.f34784b.getPath(), "CREATE TABLE IF NOT EXISTS Events (eventHash INTEGER, timestamp INTEGER);")) {
                throw new d("An error occurred while creating the \"Events\" table in the Android Event History database.");
            }
        }
    }

    private void c() {
        this.f34785c = g.e(this.f34784b.getPath(), g.a.READ_WRITE);
    }

    private File d() {
        File c10;
        Context a10 = i0.f().a().a();
        if (a10 == null) {
            t.a("MobileCore", "AndroidEventHistoryDatabase", "AndroidEventHistoryDatabase", "Failed to create database (%s), the ApplicationContext is null", "com.adobe.module.core.eventhistory");
            return null;
        }
        File databasePath = a10.getDatabasePath("com.adobe.module.core.eventhistory");
        if (!databasePath.exists() && (c10 = i0.f().e().c()) != null) {
            File file = new File(c10, "EventHistory");
            try {
                if (file.exists()) {
                    com.adobe.marketing.mobile.internal.util.c.f(file, databasePath);
                    t.a("MobileCore", "AndroidEventHistoryDatabase", "Successfully moved database (%s) from cache directory to database directory", "EventHistory");
                }
            } catch (Exception unused) {
                t.a("MobileCore", "AndroidEventHistoryDatabase", "Failed to move database (%s) from cache directory to database directory", "EventHistory");
            }
        }
        return databasePath;
    }

    public void a() {
        g.b(this.f34785c);
    }

    public boolean b(long j10, long j11) {
        boolean z10;
        synchronized (this.f34783a) {
            try {
                c();
                ContentValues contentValues = new ContentValues();
                contentValues.put("eventHash", Long.valueOf(j10));
                contentValues.put("timestamp", Long.valueOf(j11));
                z10 = this.f34785c.insert("Events", null, contentValues) != -1;
            } catch (SQLException e10) {
                Object[] objArr = new Object[1];
                objArr[0] = e10.getLocalizedMessage() != null ? e10.getLocalizedMessage() : e10.getMessage();
                t.f("MobileCore", "AndroidEventHistoryDatabase", "Failed to insert rows into the table (%s)", objArr);
                return false;
            } finally {
                a();
            }
        }
        return z10;
    }

    public Cursor e(long j10, long j11, long j12) {
        Cursor rawQuery;
        if (j12 == 0) {
            j12 = System.currentTimeMillis();
        }
        synchronized (this.f34783a) {
            try {
                try {
                    c();
                    rawQuery = this.f34785c.rawQuery("SELECT count(*) as count, min(timestamp) as oldest, max(timestamp) as newest FROM Events  WHERE eventHash = ? AND timestamp >= ? AND timestamp <= ?", new String[]{String.valueOf(j10), String.valueOf(j11), String.valueOf(j12)});
                    rawQuery.moveToFirst();
                } catch (SQLException e10) {
                    Object[] objArr = new Object[1];
                    objArr[0] = e10.getLocalizedMessage() != null ? e10.getLocalizedMessage() : e10.getMessage();
                    t.f("MobileCore", "AndroidEventHistoryDatabase", "Failed to execute query (%s)", objArr);
                    return null;
                }
            } finally {
                a();
            }
        }
        return rawQuery;
    }
}
