package com.google.android.datatransport.runtime.scheduling.persistence;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Base64;
import com.google.android.datatransport.runtime.e;
import com.google.android.datatransport.runtime.scheduling.persistence.SQLiteEventStore;
import com.google.android.gms.measurement.api.AppMeasurementSdk;
import defpackage.co9;
import defpackage.h11;
import defpackage.hi7;
import defpackage.l59;
import defpackage.nl2;
import defpackage.tg5;
import defpackage.th5;
import defpackage.yg5;
import defpackage.zk2;
import defpackage.zv3;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/* JADX INFO: loaded from: classes.dex */
public final /* synthetic */ class f implements SQLiteEventStore.b {
    public final /* synthetic */ int a;
    public final /* synthetic */ SQLiteEventStore b;
    public final /* synthetic */ Object c;
    public final /* synthetic */ Object d;

    public /* synthetic */ f(SQLiteEventStore sQLiteEventStore, Object obj, Object obj2, int i) {
        this.a = i;
        this.b = sQLiteEventStore;
        this.c = obj;
        this.d = obj2;
    }

    @Override // com.google.android.datatransport.runtime.scheduling.persistence.SQLiteEventStore.b
    public final Object apply(Object obj) {
        int i;
        long jInsert;
        int i2 = this.a;
        tg5.b bVar = tg5.b.CACHE_FULL;
        int i3 = 5;
        int i4 = 3;
        int i5 = 2;
        int i6 = 4;
        int i7 = 0;
        int i8 = 1;
        Object obj2 = this.d;
        Object obj3 = this.c;
        SQLiteEventStore sQLiteEventStore = this.b;
        switch (i2) {
            case 0:
                HashMap map = (HashMap) obj3;
                h11.a aVar = (h11.a) obj2;
                ArrayList arrayList = aVar.b;
                Cursor cursor = (Cursor) obj;
                nl2 nl2Var = SQLiteEventStore.f;
                while (cursor.moveToNext()) {
                    String string = cursor.getString(0);
                    int i9 = cursor.getInt(1);
                    tg5.b bVar2 = tg5.b.REASON_UNKNOWN;
                    if (i9 != 0) {
                        if (i9 == 1) {
                            bVar2 = tg5.b.MESSAGE_TOO_OLD;
                        } else {
                            i = 2;
                            if (i9 == 2) {
                                bVar2 = bVar;
                            } else if (i9 == 3) {
                                bVar2 = tg5.b.PAYLOAD_TOO_BIG;
                            } else if (i9 == 4) {
                                bVar2 = tg5.b.MAX_RETRIES_REACHED;
                                i = 2;
                            } else {
                                if (i9 == 5) {
                                    bVar2 = tg5.b.INVALID_PAYLOD;
                                } else if (i9 == 6) {
                                    bVar2 = tg5.b.SERVER_ERROR;
                                } else {
                                    th5.a("SQLiteEventStore", "%n is not valid. No matched LogEventDropped-Reason found. Treated it as REASON_UNKNOWN", Integer.valueOf(i9));
                                }
                                i = 2;
                            }
                        }
                        i = 2;
                    } else {
                        i = 2;
                    }
                    long j = cursor.getLong(i);
                    if (!map.containsKey(string)) {
                        map.put(string, new ArrayList());
                    }
                    ((List) map.get(string)).add(new tg5(j, bVar2));
                }
                for (Map.Entry entry : map.entrySet()) {
                    int i10 = yg5.c;
                    yg5.a aVar2 = new yg5.a();
                    aVar2.a = (String) entry.getKey();
                    aVar2.b = (List) entry.getValue();
                    arrayList.add(new yg5(aVar2.a, Collections.unmodifiableList(aVar2.b)));
                }
                final long jA = sQLiteEventStore.b.a();
                aVar.a = (co9) sQLiteEventStore.n(new SQLiteEventStore.b() { // from class: com.google.android.datatransport.runtime.scheduling.persistence.g
                    @Override // com.google.android.datatransport.runtime.scheduling.persistence.SQLiteEventStore.b
                    public final Object apply(Object obj4) {
                        long j2 = jA;
                        nl2 nl2Var2 = SQLiteEventStore.f;
                        Cursor cursorRawQuery = ((SQLiteDatabase) obj4).rawQuery("SELECT last_metrics_upload_ms FROM global_log_event_state LIMIT 1", new String[0]);
                        try {
                            nl2 nl2Var3 = SQLiteEventStore.f;
                            cursorRawQuery.moveToNext();
                            long j3 = cursorRawQuery.getLong(0);
                            co9.a aVar3 = new co9.a();
                            aVar3.a = 0L;
                            aVar3.b = 0L;
                            aVar3.a = j3;
                            aVar3.b = j2;
                            return new co9(aVar3.a, aVar3.b);
                        } finally {
                            cursorRawQuery.close();
                        }
                    }
                });
                aVar.c = new zv3(new l59(sQLiteEventStore.k().compileStatement("PRAGMA page_size").simpleQueryForLong() * sQLiteEventStore.k().compileStatement("PRAGMA page_count").simpleQueryForLong(), c.a.b));
                aVar.d = (String) sQLiteEventStore.e.get();
                return new h11(aVar.a, Collections.unmodifiableList(arrayList), aVar.c, aVar.d);
            case 1:
                com.google.android.datatransport.runtime.e eVar = (com.google.android.datatransport.runtime.e) obj3;
                com.google.android.datatransport.runtime.j jVar = (com.google.android.datatransport.runtime.j) obj2;
                SQLiteDatabase sQLiteDatabase = (SQLiteDatabase) obj;
                nl2 nl2Var2 = SQLiteEventStore.f;
                long jSimpleQueryForLong = sQLiteEventStore.k().compileStatement("PRAGMA page_size").simpleQueryForLong() * sQLiteEventStore.k().compileStatement("PRAGMA page_count").simpleQueryForLong();
                c cVar = sQLiteEventStore.d;
                if (jSimpleQueryForLong >= cVar.e()) {
                    sQLiteEventStore.h(1L, bVar, eVar.l());
                    return -1L;
                }
                Long l = SQLiteEventStore.l(sQLiteDatabase, jVar);
                if (l != null) {
                    jInsert = l.longValue();
                } else {
                    ContentValues contentValues = new ContentValues();
                    contentValues.put("backend_name", jVar.b());
                    contentValues.put("priority", Integer.valueOf(hi7.a(jVar.d())));
                    contentValues.put("next_request_ms", (Integer) 0);
                    if (jVar.c() != null) {
                        contentValues.put("extras", Base64.encodeToString(jVar.c(), 0));
                    }
                    jInsert = sQLiteDatabase.insert("transport_contexts", null, contentValues);
                }
                int iD = cVar.d();
                byte[] bArr = eVar.e().b;
                boolean z = bArr.length <= iD;
                ContentValues contentValues2 = new ContentValues();
                contentValues2.put("context_id", Long.valueOf(jInsert));
                contentValues2.put("transport_name", eVar.l());
                contentValues2.put("timestamp_ms", Long.valueOf(eVar.f()));
                contentValues2.put("uptime_ms", Long.valueOf(eVar.m()));
                contentValues2.put("payload_encoding", eVar.e().a.a);
                contentValues2.put("code", eVar.d());
                contentValues2.put("num_attempts", (Integer) 0);
                contentValues2.put("inline", Boolean.valueOf(z));
                contentValues2.put("payload", z ? bArr : new byte[0]);
                contentValues2.put("product_id", eVar.j());
                contentValues2.put("pseudonymous_id", eVar.k());
                contentValues2.put("experiment_ids_clear_blob", eVar.g());
                contentValues2.put("experiment_ids_encrypted_blob", eVar.h());
                long jInsert2 = sQLiteDatabase.insert("events", null, contentValues2);
                if (!z) {
                    int iCeil = (int) Math.ceil(((double) bArr.length) / ((double) iD));
                    for (int i11 = 1; i11 <= iCeil; i11++) {
                        byte[] bArrCopyOfRange = Arrays.copyOfRange(bArr, (i11 - 1) * iD, Math.min(i11 * iD, bArr.length));
                        ContentValues contentValues3 = new ContentValues();
                        contentValues3.put("event_id", Long.valueOf(jInsert2));
                        contentValues3.put("sequence_num", Integer.valueOf(i11));
                        contentValues3.put("bytes", bArrCopyOfRange);
                        sQLiteDatabase.insert("event_payloads", null, contentValues3);
                    }
                }
                for (Map.Entry entry2 : Collections.unmodifiableMap(eVar.c()).entrySet()) {
                    ContentValues contentValues4 = new ContentValues();
                    contentValues4.put("event_id", Long.valueOf(jInsert2));
                    contentValues4.put(AppMeasurementSdk.ConditionalUserProperty.NAME, (String) entry2.getKey());
                    contentValues4.put(AppMeasurementSdk.ConditionalUserProperty.VALUE, (String) entry2.getValue());
                    sQLiteDatabase.insert("event_metadata", null, contentValues4);
                }
                return Long.valueOf(jInsert2);
            default:
                ArrayList arrayList2 = (ArrayList) obj3;
                com.google.android.datatransport.runtime.j jVar2 = (com.google.android.datatransport.runtime.j) obj2;
                Cursor cursor2 = (Cursor) obj;
                nl2 nl2Var3 = SQLiteEventStore.f;
                while (cursor2.moveToNext()) {
                    long j2 = cursor2.getLong(i7);
                    int i12 = cursor2.getInt(7) != 0 ? i8 : i7;
                    e.a aVarA = com.google.android.datatransport.runtime.e.a();
                    aVarA.k(cursor2.getString(i8));
                    com.google.android.datatransport.runtime.j jVar3 = jVar2;
                    aVarA.f(cursor2.getLong(i5));
                    aVarA.l(cursor2.getLong(i4));
                    if (i12 != 0) {
                        String string2 = cursor2.getString(i6);
                        aVarA.e(new zk2(string2 == null ? SQLiteEventStore.f : new nl2(string2), cursor2.getBlob(i3)));
                    } else {
                        String string3 = cursor2.getString(i6);
                        nl2 nl2Var4 = string3 == null ? SQLiteEventStore.f : new nl2(string3);
                        Cursor cursorQuery = sQLiteEventStore.k().query("event_payloads", new String[]{"bytes"}, "event_id = ?", new String[]{String.valueOf(j2)}, null, null, "sequence_num");
                        try {
                            nl2 nl2Var5 = SQLiteEventStore.f;
                            ArrayList arrayList3 = new ArrayList();
                            int length = 0;
                            while (cursorQuery.moveToNext()) {
                                byte[] blob = cursorQuery.getBlob(0);
                                arrayList3.add(blob);
                                length += blob.length;
                                break;
                            }
                            byte[] bArr2 = new byte[length];
                            int i13 = 0;
                            int length2 = 0;
                            while (i13 < arrayList3.size()) {
                                byte[] bArr3 = (byte[]) arrayList3.get(i13);
                                int i14 = i13;
                                System.arraycopy(bArr3, 0, bArr2, length2, bArr3.length);
                                length2 += bArr3.length;
                                i13 = i14 + 1;
                            }
                            cursorQuery.close();
                            aVarA.e(new zk2(nl2Var4, bArr2));
                        } catch (Throwable th) {
                            cursorQuery.close();
                            throw th;
                        }
                    }
                    if (!cursor2.isNull(6)) {
                        aVarA.d(Integer.valueOf(cursor2.getInt(6)));
                    }
                    if (!cursor2.isNull(8)) {
                        aVarA.i(Integer.valueOf(cursor2.getInt(8)));
                    }
                    if (!cursor2.isNull(9)) {
                        aVarA.j(cursor2.getString(9));
                    }
                    if (!cursor2.isNull(10)) {
                        aVarA.g(cursor2.getBlob(10));
                    }
                    if (!cursor2.isNull(11)) {
                        aVarA.h(cursor2.getBlob(11));
                    }
                    arrayList2.add(new b(j2, jVar3, aVarA.b()));
                    jVar2 = jVar3;
                    i3 = 5;
                    i4 = 3;
                    i5 = 2;
                    i6 = 4;
                    i7 = 0;
                    i8 = 1;
                }
                return null;
        }
    }
}
