package o;

import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Base64;
import android.util.Log;
import com.google.firebase.messaging.FirebaseMessaging;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import org.json.JSONException;
import org.json.JSONObject;

/* compiled from: r8-map-id-3b7cc9102578c509342fa2f0fabed2bfc72239cd258d579fc679bfcbc198b048 */
/* renamed from: o.yo, reason: case insensitive filesystem */
/* loaded from: classes.dex */
public final /* synthetic */ class C3034yo implements InterfaceC0246Jm, InterfaceC0823bc0, InterfaceC2439sb0, J30 {
    public final /* synthetic */ int l;
    public final /* synthetic */ Object m;
    public final /* synthetic */ Object n;

    /* renamed from: o, reason: collision with root package name */
    public final /* synthetic */ Object f813o;

    public /* synthetic */ C3034yo(Object obj, Object obj2, Object obj3, int i) {
        this.l = i;
        this.m = obj;
        this.n = obj2;
        this.f813o = obj3;
    }

    @Override // o.J30
    public Object apply(Object obj) throws Throwable {
        long jInsert;
        Cursor cursor;
        GM gm;
        int i = this.l;
        int i2 = 5;
        int i3 = 4;
        int i4 = 3;
        GM gm2 = GM.f135o;
        int i5 = 2;
        int i6 = 1;
        Object obj2 = this.f813o;
        Object obj3 = this.n;
        int i7 = 0;
        L30 l30 = (L30) this.m;
        switch (i) {
            case 3:
                C0338Nb c0338Nb = (C0338Nb) obj3;
                C0514Tv c0514Tv = c0338Nb.c;
                String str = c0338Nb.a;
                C2156pc c2156pc = (C2156pc) obj2;
                SQLiteDatabase sQLiteDatabase = (SQLiteDatabase) obj;
                long jSimpleQueryForLong = l30.b().compileStatement("PRAGMA page_size").simpleQueryForLong() * l30.b().compileStatement("PRAGMA page_count").simpleQueryForLong();
                C0364Ob c0364Ob = l30.f204o;
                if (jSimpleQueryForLong >= c0364Ob.a) {
                    l30.g(1L, gm2, str);
                    return -1L;
                }
                Long lC = L30.c(sQLiteDatabase, c2156pc);
                if (lC != null) {
                    jInsert = lC.longValue();
                } else {
                    ContentValues contentValues = new ContentValues();
                    contentValues.put("backend_name", c2156pc.a);
                    contentValues.put("priority", Integer.valueOf(JY.a(c2156pc.c)));
                    contentValues.put("next_request_ms", (Integer) 0);
                    byte[] bArr = c2156pc.b;
                    if (bArr != null) {
                        contentValues.put("extras", Base64.encodeToString(bArr, 0));
                    }
                    jInsert = sQLiteDatabase.insert("transport_contexts", null, contentValues);
                }
                int i8 = c0364Ob.e;
                byte[] bArr2 = c0514Tv.b;
                boolean z = bArr2.length <= i8;
                ContentValues contentValues2 = new ContentValues();
                contentValues2.put("context_id", Long.valueOf(jInsert));
                contentValues2.put("transport_name", str);
                contentValues2.put("timestamp_ms", Long.valueOf(c0338Nb.d));
                contentValues2.put("uptime_ms", Long.valueOf(c0338Nb.e));
                contentValues2.put("payload_encoding", c0514Tv.a.a);
                contentValues2.put("code", c0338Nb.b);
                contentValues2.put("num_attempts", (Integer) 0);
                contentValues2.put("inline", Boolean.valueOf(z));
                contentValues2.put("payload", z ? bArr2 : new byte[0]);
                contentValues2.put("product_id", c0338Nb.g);
                contentValues2.put("pseudonymous_id", c0338Nb.h);
                contentValues2.put("experiment_ids_clear_blob", c0338Nb.i);
                contentValues2.put("experiment_ids_encrypted_blob", c0338Nb.j);
                long jInsert2 = sQLiteDatabase.insert("events", null, contentValues2);
                if (!z) {
                    int iCeil = (int) Math.ceil(bArr2.length / i8);
                    for (int i9 = 1; i9 <= iCeil; i9++) {
                        byte[] bArrCopyOfRange = Arrays.copyOfRange(bArr2, (i9 - 1) * i8, Math.min(i9 * i8, bArr2.length));
                        ContentValues contentValues3 = new ContentValues();
                        contentValues3.put("event_id", Long.valueOf(jInsert2));
                        contentValues3.put("sequence_num", Integer.valueOf(i9));
                        contentValues3.put("bytes", bArrCopyOfRange);
                        sQLiteDatabase.insert("event_payloads", null, contentValues3);
                    }
                }
                for (Map.Entry entry : Collections.unmodifiableMap(c0338Nb.f).entrySet()) {
                    ContentValues contentValues4 = new ContentValues();
                    contentValues4.put("event_id", Long.valueOf(jInsert2));
                    contentValues4.put("name", (String) entry.getKey());
                    contentValues4.put("value", (String) entry.getValue());
                    sQLiteDatabase.insert("event_metadata", null, contentValues4);
                }
                return Long.valueOf(jInsert2);
            case 4:
                ArrayList arrayList = (ArrayList) obj3;
                C2156pc c2156pc2 = (C2156pc) obj2;
                Cursor cursor2 = (Cursor) obj;
                while (cursor2.moveToNext()) {
                    long j = cursor2.getLong(0);
                    int i10 = cursor2.getInt(7) != 0 ? i6 : 0;
                    C0312Mb c0312Mb = new C0312Mb(0);
                    c0312Mb.i = new HashMap();
                    String string = cursor2.getString(i6);
                    if (string == null) {
                        throw new NullPointerException("Null transportName");
                    }
                    c0312Mb.b = string;
                    c0312Mb.g = Long.valueOf(cursor2.getLong(i5));
                    c0312Mb.h = Long.valueOf(cursor2.getLong(3));
                    if (i10 != 0) {
                        String string2 = cursor2.getString(4);
                        c0312Mb.f = new C0514Tv(string2 == null ? L30.q : new C0618Xv(string2), cursor2.getBlob(5));
                    } else {
                        String string3 = cursor2.getString(4);
                        C0618Xv c0618Xv = string3 == null ? L30.q : new C0618Xv(string3);
                        Cursor cursorQuery = l30.b().query("event_payloads", new String[]{"bytes"}, "event_id = ?", new String[]{String.valueOf(j)}, null, null, "sequence_num");
                        try {
                            ArrayList arrayList2 = new ArrayList();
                            int length = 0;
                            while (cursorQuery.moveToNext()) {
                                byte[] blob = cursorQuery.getBlob(0);
                                arrayList2.add(blob);
                                length += blob.length;
                            }
                            byte[] bArr3 = new byte[length];
                            int i11 = 0;
                            int length2 = 0;
                            while (i11 < arrayList2.size()) {
                                byte[] bArr4 = (byte[]) arrayList2.get(i11);
                                cursor = cursorQuery;
                                try {
                                    ArrayList arrayList3 = arrayList2;
                                    System.arraycopy(bArr4, 0, bArr3, length2, bArr4.length);
                                    length2 += bArr4.length;
                                    i11++;
                                    cursorQuery = cursor;
                                    arrayList2 = arrayList3;
                                } catch (Throwable th) {
                                    th = th;
                                    cursor.close();
                                    throw th;
                                }
                            }
                            cursorQuery.close();
                            c0312Mb.f = new C0514Tv(c0618Xv, bArr3);
                        } catch (Throwable th2) {
                            th = th2;
                            cursor = cursorQuery;
                        }
                    }
                    if (!cursor2.isNull(6)) {
                        c0312Mb.d = Integer.valueOf(cursor2.getInt(6));
                    }
                    if (!cursor2.isNull(8)) {
                        c0312Mb.e = Integer.valueOf(cursor2.getInt(8));
                    }
                    if (!cursor2.isNull(9)) {
                        c0312Mb.c = cursor2.getString(9);
                    }
                    if (!cursor2.isNull(10)) {
                        c0312Mb.j = cursor2.getBlob(10);
                    }
                    if (!cursor2.isNull(11)) {
                        c0312Mb.k = cursor2.getBlob(11);
                    }
                    arrayList.add(new C0917cc(j, c2156pc2, c0312Mb.c()));
                    i5 = 2;
                    i6 = 1;
                }
                return null;
            default:
                HashMap map = (HashMap) obj3;
                S60 s60 = (S60) obj2;
                ArrayList arrayList4 = (ArrayList) s60.f325o;
                Cursor cursor3 = (Cursor) obj;
                l30.getClass();
                while (cursor3.moveToNext()) {
                    String string4 = cursor3.getString(i7);
                    int i12 = cursor3.getInt(1);
                    GM gm3 = GM.m;
                    if (i12 != 0) {
                        if (i12 == 1) {
                            gm3 = GM.n;
                        } else if (i12 == 2) {
                            gm = gm2;
                        } else if (i12 == i4) {
                            gm3 = GM.p;
                        } else if (i12 == i3) {
                            gm3 = GM.q;
                        } else if (i12 == i2) {
                            gm3 = GM.r;
                        } else if (i12 == 6) {
                            gm3 = GM.s;
                        } else {
                            IJ.j("SQLiteEventStore", "%n is not valid. No matched LogEventDropped-Reason found. Treated it as REASON_UNKNOWN", Integer.valueOf(i12));
                        }
                        gm = gm3;
                    } else {
                        gm = gm3;
                    }
                    long j2 = cursor3.getLong(2);
                    if (!map.containsKey(string4)) {
                        map.put(string4, new ArrayList());
                    }
                    ((List) map.get(string4)).add(new HM(j2, gm));
                    i7 = 0;
                    i2 = 5;
                    i3 = 4;
                    i4 = 3;
                }
                for (Map.Entry entry2 : map.entrySet()) {
                    int i13 = JM.c;
                    new ArrayList();
                    arrayList4.add(new JM((String) entry2.getKey(), Collections.unmodifiableList((List) entry2.getValue())));
                }
                long jU = l30.m.u();
                SQLiteDatabase sQLiteDatabaseB = l30.b();
                sQLiteDatabaseB.beginTransaction();
                try {
                    Cursor cursorRawQuery = sQLiteDatabaseB.rawQuery("SELECT last_metrics_upload_ms FROM global_log_event_state LIMIT 1", new String[0]);
                    try {
                        cursorRawQuery.moveToNext();
                        Af0 af0 = new Af0(cursorRawQuery.getLong(0), jU);
                        cursorRawQuery.close();
                        sQLiteDatabaseB.setTransactionSuccessful();
                        sQLiteDatabaseB.endTransaction();
                        s60.n = af0;
                        s60.p = new RD(new Qa0(l30.b().compileStatement("PRAGMA page_size").simpleQueryForLong() * l30.b().compileStatement("PRAGMA page_count").simpleQueryForLong(), C0364Ob.f.a));
                        s60.m = (String) l30.p.get();
                        return new C1312gi((Af0) s60.n, Collections.unmodifiableList(arrayList4), (RD) s60.p, (String) s60.m);
                    } catch (Throwable th3) {
                        cursorRawQuery.close();
                        throw th3;
                    }
                } catch (Throwable th4) {
                    sQLiteDatabaseB.endTransaction();
                    throw th4;
                }
        }
    }

    @Override // o.InterfaceC0823bc0
    public Object d() {
        C2563tq c2563tq = (C2563tq) this.m;
        C2156pc c2156pc = (C2156pc) this.n;
        C0338Nb c0338Nb = (C0338Nb) this.f813o;
        L30 l30 = (L30) c2563tq.d;
        l30.getClass();
        HY hy = c2156pc.c;
        if (Log.isLoggable(IJ.u("SQLiteEventStore"), 3)) {
            new StringBuilder("Storing event with priority=").append(hy);
        }
        ((Long) l30.d(new C3034yo(l30, c0338Nb, c2156pc, 3))).getClass();
        c2563tq.a.O(c2156pc, 1, false);
        return null;
    }

    @Override // o.InterfaceC0246Jm
    public Object then(Pc0 pc0) {
        Qc0 qc0 = (Qc0) this.m;
        AtomicBoolean atomicBoolean = (AtomicBoolean) this.n;
        OD od = (OD) this.f813o;
        if (pc0.h()) {
            qc0.d(pc0.f());
        } else if (pc0.e() != null) {
            qc0.c(pc0.e());
        } else if (atomicBoolean.getAndSet(true)) {
            ((Hw0) ((C1696kj0) od.m).m).o(null);
        }
        return AbstractC0752ap.K(null);
    }

    @Override // o.InterfaceC2439sb0
    public Pc0 then(Object obj) {
        String strD;
        String string;
        FirebaseMessaging firebaseMessaging = (FirebaseMessaging) this.m;
        String str = (String) this.n;
        Sa0 sa0 = (Sa0) this.f813o;
        String str2 = (String) obj;
        WT wtC = FirebaseMessaging.c(firebaseMessaging.b);
        C0076Cy c0076Cy = firebaseMessaging.a;
        c0076Cy.a();
        if ("[DEFAULT]".equals(c0076Cy.b)) {
            strD = "";
        } else {
            strD = c0076Cy.d();
        }
        String strB = firebaseMessaging.i.b();
        synchronized (wtC) {
            long jCurrentTimeMillis = System.currentTimeMillis();
            int i = Sa0.e;
            try {
                JSONObject jSONObject = new JSONObject();
                jSONObject.put("token", str2);
                jSONObject.put("appVersion", strB);
                jSONObject.put("timestamp", jCurrentTimeMillis);
                string = jSONObject.toString();
            } catch (JSONException e) {
                e.toString();
                string = null;
            }
            if (string != null) {
                SharedPreferences.Editor editorEdit = ((SharedPreferences) wtC.m).edit();
                editorEdit.putString(strD + "|T|" + str + "|*", string);
                editorEdit.commit();
            }
        }
        if (sa0 == null || !str2.equals(sa0.a)) {
            C0076Cy c0076Cy2 = firebaseMessaging.a;
            c0076Cy2.a();
            if ("[DEFAULT]".equals(c0076Cy2.b)) {
                if (Log.isLoggable("FirebaseMessaging", 3)) {
                    c0076Cy2.a();
                }
                Intent intent = new Intent("com.google.firebase.messaging.NEW_TOKEN");
                intent.putExtra("token", str2);
                new DH(firebaseMessaging.b).p(intent);
            }
        }
        return AbstractC0752ap.K(str2);
    }
}
