package defpackage;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Base64;
import android.util.Log;
import com.google.firebase.remoteconfig.FirebaseRemoteConfigClientException;
import com.google.firebase.remoteconfig.FirebaseRemoteConfigException;
import j$.util.DesugarCollections;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicBoolean;

/* renamed from: ˆˈᵲ, reason: contains not printable characters */
/* loaded from: classes3.dex */
public final /* synthetic */ class C0817 implements InterfaceC3610, InterfaceC1777, InterfaceC5560, InterfaceC4038, InterfaceC2070, InterfaceC5372 {

    /* renamed from: ᵢˇᵭ, reason: contains not printable characters */
    public final /* synthetic */ Object f3780;

    /* renamed from: ᵧᵾˀˈ, reason: contains not printable characters */
    public final /* synthetic */ int f3781;

    /* renamed from: ᵾᵬ, reason: contains not printable characters */
    public final /* synthetic */ Object f3782;

    /* renamed from: ᵿʗᵝʽ, reason: contains not printable characters */
    public final /* synthetic */ Object f3783;

    public /* synthetic */ C0817(Object obj, Object obj2, Object obj3, int i) {
        this.f3781 = i;
        this.f3780 = obj;
        this.f3783 = obj2;
        this.f3782 = obj3;
    }

    @Override // defpackage.InterfaceC4038
    public void accept(Object obj) {
        C4782 c4782 = (C4782) this.f3780;
        ((InterfaceC0917) obj).mo1962(c4782.f17765, (C2477) this.f3783, (C3112) this.f3782);
    }

    @Override // defpackage.InterfaceC5372
    public Object apply(Object obj) throws Throwable {
        long jInsert;
        Cursor cursor;
        C3089 c3089;
        EnumC2677 enumC2677;
        int i = this.f3781;
        int i2 = 6;
        int i3 = 5;
        int i4 = 4;
        int i5 = 3;
        EnumC2677 enumC26772 = EnumC2677.f10388;
        int i6 = 2;
        int i7 = 1;
        Object obj2 = this.f3782;
        Object obj3 = this.f3783;
        Object obj4 = this.f3780;
        int i8 = 0;
        switch (i) {
            case C1339.BYTES_FIELD_NUMBER /* 8 */:
                C3089 c30892 = (C3089) obj4;
                C3681 c3681 = (C3681) obj3;
                C4621 c4621 = c3681.f13699;
                String str = c3681.f13701;
                C5456 c5456 = (C5456) obj2;
                SQLiteDatabase sQLiteDatabase = (SQLiteDatabase) obj;
                long jSimpleQueryForLong = c30892.m7421().compileStatement("PRAGMA page_size").simpleQueryForLong() * c30892.m7421().compileStatement("PRAGMA page_count").simpleQueryForLong();
                C5121 c5121 = c30892.f11780;
                if (jSimpleQueryForLong >= c5121.f19081) {
                    c30892.m7420(1L, enumC26772, str);
                    return -1L;
                }
                Long lM7414 = C3089.m7414(sQLiteDatabase, c5456);
                if (lM7414 != null) {
                    jInsert = lM7414.longValue();
                } else {
                    ContentValues contentValues = new ContentValues();
                    contentValues.put("backend_name", c5456.f20490);
                    contentValues.put("priority", Integer.valueOf(AbstractC3999.m8840(c5456.f20489)));
                    contentValues.put("next_request_ms", (Integer) 0);
                    byte[] bArr = c5456.f20491;
                    if (bArr != null) {
                        contentValues.put("extras", Base64.encodeToString(bArr, 0));
                    }
                    jInsert = sQLiteDatabase.insert("transport_contexts", null, contentValues);
                }
                int i9 = c5121.f19079;
                byte[] bArr2 = c4621.f17104;
                boolean z = bArr2.length <= i9;
                ContentValues contentValues2 = new ContentValues();
                contentValues2.put("context_id", Long.valueOf(jInsert));
                contentValues2.put("transport_name", str);
                contentValues2.put("timestamp_ms", Long.valueOf(c3681.f13705));
                contentValues2.put("uptime_ms", Long.valueOf(c3681.f13696));
                contentValues2.put("payload_encoding", c4621.f17103.f7748);
                contentValues2.put("code", c3681.f13702);
                contentValues2.put("num_attempts", (Integer) 0);
                contentValues2.put("inline", Boolean.valueOf(z));
                contentValues2.put("payload", z ? bArr2 : new byte[0]);
                contentValues2.put("product_id", c3681.f13697);
                contentValues2.put("pseudonymous_id", c3681.f13704);
                contentValues2.put("experiment_ids_clear_blob", c3681.f13700);
                contentValues2.put("experiment_ids_encrypted_blob", c3681.f13703);
                long jInsert2 = sQLiteDatabase.insert("events", null, contentValues2);
                if (!z) {
                    int iCeil = (int) Math.ceil(bArr2.length / i9);
                    for (int i10 = 1; i10 <= iCeil; i10++) {
                        byte[] bArrCopyOfRange = Arrays.copyOfRange(bArr2, (i10 - 1) * i9, Math.min(i10 * i9, bArr2.length));
                        ContentValues contentValues3 = new ContentValues();
                        contentValues3.put("event_id", Long.valueOf(jInsert2));
                        contentValues3.put("sequence_num", Integer.valueOf(i10));
                        contentValues3.put("bytes", bArrCopyOfRange);
                        sQLiteDatabase.insert("event_payloads", null, contentValues3);
                    }
                }
                for (Map.Entry entry : DesugarCollections.unmodifiableMap(c3681.f13698).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 9:
                C3089 c30893 = (C3089) obj4;
                ArrayList arrayList = (ArrayList) obj3;
                C5456 c54562 = (C5456) obj2;
                Cursor cursor2 = (Cursor) obj;
                while (cursor2.moveToNext()) {
                    long j = cursor2.getLong(0);
                    int i11 = cursor2.getInt(7) != 0 ? i7 : 0;
                    C3423 c3423 = new C3423();
                    c3423.f12732 = new HashMap();
                    String string = cursor2.getString(i7);
                    if (string == null) {
                        C1260.m3813("Null transportName");
                        return null;
                    }
                    c3423.f12727 = string;
                    c3423.f12726 = Long.valueOf(cursor2.getLong(i6));
                    c3423.f12725 = Long.valueOf(cursor2.getLong(3));
                    if (i11 != 0) {
                        String string2 = cursor2.getString(4);
                        c3423.f12724 = new C4621(string2 == null ? C3089.f11776 : new C1928(string2), cursor2.getBlob(5));
                        c3089 = c30893;
                    } else {
                        String string3 = cursor2.getString(4);
                        C1928 c1928 = string3 == null ? C3089.f11776 : new C1928(string3);
                        Cursor cursorQuery = c30893.m7421().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 i12 = 0;
                            int length2 = 0;
                            while (i12 < arrayList2.size()) {
                                byte[] bArr4 = (byte[]) arrayList2.get(i12);
                                C3089 c30894 = c30893;
                                cursor = cursorQuery;
                                try {
                                    System.arraycopy(bArr4, 0, bArr3, length2, bArr4.length);
                                    length2 += bArr4.length;
                                    i12++;
                                    cursorQuery = cursor;
                                    c30893 = c30894;
                                } catch (Throwable th) {
                                    th = th;
                                    cursor.close();
                                    throw th;
                                }
                            }
                            c3089 = c30893;
                            cursorQuery.close();
                            c3423.f12724 = new C4621(c1928, bArr3);
                        } catch (Throwable th2) {
                            th = th2;
                            cursor = cursorQuery;
                        }
                    }
                    if (!cursor2.isNull(6)) {
                        c3423.f12729 = Integer.valueOf(cursor2.getInt(6));
                    }
                    if (!cursor2.isNull(8)) {
                        c3423.f12730 = Integer.valueOf(cursor2.getInt(8));
                    }
                    if (!cursor2.isNull(9)) {
                        c3423.f12733 = cursor2.getString(9);
                    }
                    if (!cursor2.isNull(10)) {
                        c3423.f12728 = cursor2.getBlob(10);
                    }
                    if (!cursor2.isNull(11)) {
                        c3423.f12731 = cursor2.getBlob(11);
                    }
                    arrayList.add(new C1921(j, c54562, c3423.m7789()));
                    c30893 = c3089;
                    i6 = 2;
                    i7 = 1;
                }
                return null;
            default:
                C3089 c30895 = (C3089) obj4;
                HashMap map = (HashMap) obj3;
                ᵮᵥٴ r13 = (ᵮᵥٴ) obj2;
                ArrayList arrayList3 = (ArrayList) r13.ᵢˇᵭ;
                Cursor cursor3 = (Cursor) obj;
                c30895.getClass();
                while (cursor3.moveToNext()) {
                    String string4 = cursor3.getString(i8);
                    int i13 = cursor3.getInt(1);
                    EnumC2677 enumC26773 = EnumC2677.f10386;
                    if (i13 != 0) {
                        if (i13 == 1) {
                            enumC26773 = EnumC2677.f10389;
                        } else if (i13 == 2) {
                            enumC2677 = enumC26772;
                        } else if (i13 == i5) {
                            enumC26773 = EnumC2677.f10387;
                        } else if (i13 == i4) {
                            enumC26773 = EnumC2677.f10385;
                        } else if (i13 == i3) {
                            enumC26773 = EnumC2677.f10382;
                        } else if (i13 == i2) {
                            enumC26773 = EnumC2677.f10384;
                        } else {
                            ᵺᵣᵬ.ᵹᵝᵶ("SQLiteEventStore", "%n is not valid. No matched LogEventDropped-Reason found. Treated it as REASON_UNKNOWN", Integer.valueOf(i13));
                        }
                        enumC2677 = enumC26773;
                    } else {
                        enumC2677 = enumC26773;
                    }
                    long j2 = cursor3.getLong(2);
                    if (!map.containsKey(string4)) {
                        map.put(string4, new ArrayList());
                    }
                    ((List) map.get(string4)).add(new C4529(j2, enumC2677));
                    i2 = 6;
                    i3 = 5;
                    i4 = 4;
                    i5 = 3;
                    i8 = 0;
                }
                for (Map.Entry entry2 : map.entrySet()) {
                    int i14 = C4256.f15940;
                    new ArrayList();
                    arrayList3.add(new C4256((String) entry2.getKey(), DesugarCollections.unmodifiableList((List) entry2.getValue())));
                }
                long jM1696 = c30895.f11777.m1696();
                SQLiteDatabase sQLiteDatabaseM7421 = c30895.m7421();
                sQLiteDatabaseM7421.beginTransaction();
                try {
                    Cursor cursorRawQuery = sQLiteDatabaseM7421.rawQuery("SELECT last_metrics_upload_ms FROM global_log_event_state LIMIT 1", new String[0]);
                    try {
                        cursorRawQuery.moveToNext();
                        C1023 c1023 = new C1023(cursorRawQuery.getLong(0), jM1696);
                        cursorRawQuery.close();
                        sQLiteDatabaseM7421.setTransactionSuccessful();
                        sQLiteDatabaseM7421.endTransaction();
                        r13.ᵿʗᵝʽ = c1023;
                        r13.ᵾᵬ = new C3111(new C2328(c30895.m7421().compileStatement("PRAGMA page_size").simpleQueryForLong() * c30895.m7421().compileStatement("PRAGMA page_count").simpleQueryForLong(), C5121.f19078.f19081));
                        r13.ᵤᵙᵷ = (String) c30895.f11778.get();
                        return new C2143((C1023) r13.ᵿʗᵝʽ, DesugarCollections.unmodifiableList(arrayList3), (C3111) r13.ᵾᵬ, (String) r13.ᵤᵙᵷ);
                    } catch (Throwable th3) {
                        cursorRawQuery.close();
                        throw th3;
                    }
                } catch (Throwable th4) {
                    sQLiteDatabaseM7421.endTransaction();
                    throw th4;
                }
        }
    }

    @Override // defpackage.InterfaceC3610
    /* renamed from: ᵝᵵᵺ */
    public Object mo1888(C3921 c3921) {
        C2275 c2275;
        int i = this.f3781;
        URL url = null;
        Object obj = this.f3782;
        Object obj2 = this.f3783;
        Object obj3 = this.f3780;
        switch (i) {
            case 0:
                C4556 c4556 = (C4556) obj3;
                C3921 c39212 = (C3921) obj2;
                C3921 c39213 = (C3921) obj;
                if (!c39212.m8709()) {
                    return ʼٴ.ᵡˎ(new FirebaseRemoteConfigClientException("Firebase Installations failed to get installation auth token for config update listener connection.", c39212.m8707()));
                }
                if (!c39213.m8709()) {
                    return ʼٴ.ᵡˎ(new FirebaseRemoteConfigClientException("Firebase Installations failed to get installation ID for config update listener connection.", c39213.m8707()));
                }
                try {
                    try {
                        url = new URL(c4556.m9678(c4556.f16939));
                    } catch (MalformedURLException unused) {
                    }
                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                    c4556.m9676(httpURLConnection, (String) c39213.m8706(), ((C4444) c39212.m8706()).f16596);
                    return ʼٴ.ˑᵥˁ(httpURLConnection);
                } catch (IOException e) {
                    return ʼٴ.ᵡˎ(new FirebaseRemoteConfigClientException("Failed to open HTTP stream connection", e));
                }
            case 1:
                C1353 c1353 = (C1353) obj3;
                AtomicBoolean atomicBoolean = (AtomicBoolean) obj2;
                ˊᵘʛ r2 = (ˊᵘʛ) obj;
                if (c3921.m8709()) {
                    c1353.m4193(c3921.m8706());
                } else if (c3921.m8707() != null) {
                    c1353.m4192(c3921.m8707());
                } else if (atomicBoolean.getAndSet(true)) {
                    ((C3921) ((C2954) r2.ᵢˇᵭ).f11438).m8705(null);
                }
                return ʼٴ.ˑᵥˁ((Object) null);
            default:
                C4291 c4291 = (C4291) obj3;
                C3921 c39214 = (C3921) obj2;
                C3921 c39215 = (C3921) obj;
                if (!c39214.m8709() || c39214.m8706() == null) {
                    return ʼٴ.ˑᵥˁ(Boolean.FALSE);
                }
                C2275 c22752 = (C2275) c39214.m8706();
                return (c39215.m8709() && (c2275 = (C2275) c39215.m8706()) != null && c22752.f8894.equals(c2275.f8894)) ? ʼٴ.ˑᵥˁ(Boolean.FALSE) : c4291.f16102.m1829(c22752).m8718(c4291.f16099, new C1468(c4291));
        }
    }

    @Override // defpackage.InterfaceC1777
    /* renamed from: ᵥᵯˎ */
    public Object mo1598() {
        C0384 c0384 = (C0384) this.f3780;
        C5456 c5456 = (C5456) this.f3783;
        C3681 c3681 = (C3681) this.f3782;
        C3089 c3089 = c0384.f2219;
        c3089.getClass();
        EnumC2576 enumC2576 = c5456.f20489;
        if (Log.isLoggable(ᵺᵣᵬ.ᵳٴᵶ("SQLiteEventStore"), 3)) {
            new StringBuilder("Storing event with priority=").append(enumC2576);
        }
        ((Long) c3089.m7417(new C0817(c3089, c3681, c5456, 8))).getClass();
        c0384.f2217.ᵤᵼˑʠ(c5456, 1, false);
        return null;
    }

    @Override // defpackage.InterfaceC2070
    /* renamed from: ᵴᵳ, reason: contains not printable characters */
    public void mo2916(Object obj) {
        ᵮᵥٴ r0 = (ᵮᵥٴ) this.f3780;
        C3921 c3921 = (C3921) this.f3783;
        C0576 c0576 = (C0576) this.f3782;
        try {
            C2275 c2275 = (C2275) c3921.m8706();
            if (c2275 != null) {
                ((Executor) r0.ᵾᵬ).execute(new RunnableC5315(c0576, ((C5553) r0.ᵿʗᵝʽ).m11189(c2275), 1));
            }
        } catch (FirebaseRemoteConfigException unused) {
        }
    }

    @Override // defpackage.InterfaceC5560
    /* renamed from: ᵹᵝᵶ, reason: contains not printable characters */
    public C4471 mo2917(int i, C4010 c4010, int[] iArr) {
        C4966 c4966 = (C4966) this.f3780;
        String str = (String) this.f3783;
        String str2 = (String) this.f3782;
        C4704 c4704M6701 = AbstractC2692.m6701();
        for (int i2 = 0; i2 < c4010.f15077; i2++) {
            c4704M6701.m3197(new C3267(i, c4010, i2, c4966, iArr[i2], str, str2));
        }
        return c4704M6701.m9892();
    }
}
