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;

/* JADX INFO: renamed from: ᵬʛᵤᵻ, reason: contains not printable characters */
/* JADX INFO: loaded from: classes3.dex */
public final /* synthetic */ class C4021 implements InterfaceC5156, InterfaceC1633, InterfaceC2902, InterfaceC3498, InterfaceC3151, InterfaceC4590 {

    /* JADX INFO: renamed from: ˌᴵ, reason: contains not printable characters */
    public final /* synthetic */ int f15472;

    /* JADX INFO: renamed from: ᵔᵖᵹᵫ, reason: contains not printable characters */
    public final /* synthetic */ Object f15473;

    /* JADX INFO: renamed from: ᵜᵪʾ, reason: contains not printable characters */
    public final /* synthetic */ Object f15474;

    /* JADX INFO: renamed from: ᵨᵼⁱ, reason: contains not printable characters */
    public final /* synthetic */ Object f15475;

    public /* synthetic */ C4021(Object obj, Object obj2, Object obj3, int i) {
        this.f15472 = i;
        this.f15474 = obj;
        this.f15473 = obj2;
        this.f15475 = obj3;
    }

    @Override // defpackage.InterfaceC3498
    public void accept(Object obj) {
        C1404 c1404 = (C1404) this.f15474;
        ((InterfaceC0559) obj).mo2144(c1404.f5808, (C4100) this.f15473, (C0990) this.f15475);
    }

    @Override // defpackage.InterfaceC4590
    public Object apply(Object obj) throws Throwable {
        long jInsert;
        Cursor cursor;
        C0520 c0520;
        EnumC1197 enumC1197;
        int i = this.f15472;
        int i2 = 6;
        int i3 = 5;
        int i4 = 4;
        int i5 = 3;
        EnumC1197 enumC11972 = EnumC1197.f5096;
        int i6 = 2;
        int i7 = 1;
        Object obj2 = this.f15475;
        Object obj3 = this.f15473;
        Object obj4 = this.f15474;
        int i8 = 0;
        switch (i) {
            case C5224.BYTES_FIELD_NUMBER /* 8 */:
                C0520 c05202 = (C0520) obj4;
                C4589 c4589 = (C4589) obj3;
                C5491 c5491 = c4589.f17467;
                String str = c4589.f17470;
                C3147 c3147 = (C3147) obj2;
                SQLiteDatabase sQLiteDatabase = (SQLiteDatabase) obj;
                long jSimpleQueryForLong = c05202.m2015().compileStatement("PRAGMA page_size").simpleQueryForLong() * c05202.m2015().compileStatement("PRAGMA page_count").simpleQueryForLong();
                C4104 c4104 = c05202.f2694;
                if (jSimpleQueryForLong >= c4104.f15722) {
                    c05202.m2011(1L, enumC11972, str);
                    return -1L;
                }
                Long lM2009 = C0520.m2009(sQLiteDatabase, c3147);
                if (lM2009 != null) {
                    jInsert = lM2009.longValue();
                } else {
                    ContentValues contentValues = new ContentValues();
                    contentValues.put("backend_name", c3147.f11994);
                    contentValues.put("priority", Integer.valueOf(AbstractC4151.m9068(c3147.f11993)));
                    contentValues.put("next_request_ms", (Integer) 0);
                    byte[] bArr = c3147.f11992;
                    if (bArr != null) {
                        contentValues.put("extras", Base64.encodeToString(bArr, 0));
                    }
                    jInsert = sQLiteDatabase.insert("transport_contexts", null, contentValues);
                }
                int i9 = c4104.f15721;
                byte[] bArr2 = c5491.f20753;
                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(c4589.f17472));
                contentValues2.put("uptime_ms", Long.valueOf(c4589.f17468));
                contentValues2.put("payload_encoding", c5491.f20754.f1569);
                contentValues2.put("code", c4589.f17465);
                contentValues2.put("num_attempts", (Integer) 0);
                contentValues2.put("inline", Boolean.valueOf(z));
                contentValues2.put("payload", z ? bArr2 : new byte[0]);
                contentValues2.put("product_id", c4589.f17471);
                contentValues2.put("pseudonymous_id", c4589.f17466);
                contentValues2.put("experiment_ids_clear_blob", c4589.f17473);
                contentValues2.put("experiment_ids_encrypted_blob", c4589.f17469);
                long jInsert2 = sQLiteDatabase.insert("events", null, contentValues2);
                if (!z) {
                    int iCeil = (int) Math.ceil(((double) bArr2.length) / ((double) 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(c4589.f17474).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:
                C0520 c05203 = (C0520) obj4;
                ArrayList arrayList = (ArrayList) obj3;
                C3147 c31472 = (C3147) obj2;
                Cursor cursor2 = (Cursor) obj;
                while (cursor2.moveToNext()) {
                    long j = cursor2.getLong(0);
                    int i11 = cursor2.getInt(7) != 0 ? i7 : 0;
                    C4735 c4735 = new C4735();
                    c4735.f17999 = new HashMap();
                    String string = cursor2.getString(i7);
                    if (string == null) {
                        C4935.m10233("Null transportName");
                        return null;
                    }
                    c4735.f18000 = string;
                    c4735.f18007 = Long.valueOf(cursor2.getLong(i6));
                    c4735.f18004 = Long.valueOf(cursor2.getLong(3));
                    if (i11 != 0) {
                        String string2 = cursor2.getString(4);
                        c4735.f18001 = new C5491(string2 == null ? C0520.f2690 : new C0160(string2), cursor2.getBlob(5));
                        c0520 = c05203;
                    } else {
                        String string3 = cursor2.getString(4);
                        C0160 c0160 = string3 == null ? C0520.f2690 : new C0160(string3);
                        Cursor cursorQuery = c05203.m2015().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;
                                break;
                            }
                            byte[] bArr3 = new byte[length];
                            int i12 = 0;
                            int length2 = 0;
                            while (i12 < arrayList2.size()) {
                                byte[] bArr4 = (byte[]) arrayList2.get(i12);
                                C0520 c05204 = c05203;
                                cursor = cursorQuery;
                                try {
                                    System.arraycopy(bArr4, 0, bArr3, length2, bArr4.length);
                                    length2 += bArr4.length;
                                    i12++;
                                    cursorQuery = cursor;
                                    c05203 = c05204;
                                } catch (Throwable th) {
                                    th = th;
                                    cursor.close();
                                    throw th;
                                }
                            }
                            c0520 = c05203;
                            cursorQuery.close();
                            c4735.f18001 = new C5491(c0160, bArr3);
                        } catch (Throwable th2) {
                            th = th2;
                            cursor = cursorQuery;
                        }
                    }
                    if (!cursor2.isNull(6)) {
                        c4735.f18003 = Integer.valueOf(cursor2.getInt(6));
                    }
                    if (!cursor2.isNull(8)) {
                        c4735.f17998 = Integer.valueOf(cursor2.getInt(8));
                    }
                    if (!cursor2.isNull(9)) {
                        c4735.f18005 = cursor2.getString(9);
                    }
                    if (!cursor2.isNull(10)) {
                        c4735.f18006 = cursor2.getBlob(10);
                    }
                    if (!cursor2.isNull(11)) {
                        c4735.f18002 = cursor2.getBlob(11);
                    }
                    arrayList.add(new C0279(j, c31472, c4735.m9896()));
                    c05203 = c0520;
                    i6 = 2;
                    i7 = 1;
                }
                return null;
            default:
                C0520 c05205 = (C0520) obj4;
                HashMap map = (HashMap) obj3;
                ᵥᵵᵶᵩ r13 = (ᵥᵵᵶᵩ) obj2;
                ArrayList arrayList3 = (ArrayList) r13.ᵜᵪʾ;
                Cursor cursor3 = (Cursor) obj;
                c05205.getClass();
                while (cursor3.moveToNext()) {
                    String string4 = cursor3.getString(i8);
                    int i13 = cursor3.getInt(1);
                    EnumC1197 enumC11973 = EnumC1197.f5095;
                    if (i13 != 0) {
                        if (i13 == 1) {
                            enumC11973 = EnumC1197.f5094;
                        } else if (i13 == 2) {
                            enumC1197 = enumC11972;
                        } else if (i13 == i5) {
                            enumC11973 = EnumC1197.f5097;
                        } else if (i13 == i4) {
                            enumC11973 = EnumC1197.f5098;
                        } else if (i13 == i3) {
                            enumC11973 = EnumC1197.f5092;
                        } else if (i13 == i2) {
                            enumC11973 = EnumC1197.f5093;
                        } else {
                            ᵸˊʾᵜ.ᵶᵥᵝ("SQLiteEventStore", "%n is not valid. No matched LogEventDropped-Reason found. Treated it as REASON_UNKNOWN", Integer.valueOf(i13));
                        }
                        enumC1197 = enumC11973;
                    } else {
                        enumC1197 = enumC11973;
                    }
                    long j2 = cursor3.getLong(2);
                    if (!map.containsKey(string4)) {
                        map.put(string4, new ArrayList());
                    }
                    ((List) map.get(string4)).add(new C3657(j2, enumC1197));
                    i2 = 6;
                    i3 = 5;
                    i4 = 4;
                    i5 = 3;
                    i8 = 0;
                }
                for (Map.Entry entry2 : map.entrySet()) {
                    int i14 = C2034.f8085;
                    new ArrayList();
                    arrayList3.add(new C2034((String) entry2.getKey(), DesugarCollections.unmodifiableList((List) entry2.getValue())));
                }
                long jM9768 = c05205.f2693.m9768();
                SQLiteDatabase sQLiteDatabaseM2015 = c05205.m2015();
                sQLiteDatabaseM2015.beginTransaction();
                try {
                    Cursor cursorRawQuery = sQLiteDatabaseM2015.rawQuery("SELECT last_metrics_upload_ms FROM global_log_event_state LIMIT 1", new String[0]);
                    try {
                        cursorRawQuery.moveToNext();
                        C2450 c2450 = new C2450(cursorRawQuery.getLong(0), jM9768);
                        cursorRawQuery.close();
                        sQLiteDatabaseM2015.setTransactionSuccessful();
                        sQLiteDatabaseM2015.endTransaction();
                        r13.ᵔᵖᵹᵫ = c2450;
                        r13.ᵨᵼⁱ = new C0720(new C2425(c05205.m2015().compileStatement("PRAGMA page_size").simpleQueryForLong() * c05205.m2015().compileStatement("PRAGMA page_count").simpleQueryForLong(), C4104.f15718.f15722));
                        r13.ᵯᵷʽ = (String) c05205.f2695.get();
                        return new C3343((C2450) r13.ᵔᵖᵹᵫ, DesugarCollections.unmodifiableList(arrayList3), (C0720) r13.ᵨᵼⁱ, (String) r13.ᵯᵷʽ);
                    } catch (Throwable th3) {
                        cursorRawQuery.close();
                        throw th3;
                    }
                } catch (Throwable th4) {
                    sQLiteDatabaseM2015.endTransaction();
                    throw th4;
                }
        }
    }

    @Override // defpackage.InterfaceC5156
    /* JADX INFO: renamed from: ˊᵫᵗ */
    public Object mo2607(C1300 c1300) {
        C5235 c5235;
        int i = this.f15472;
        URL url = null;
        Object obj = this.f15475;
        Object obj2 = this.f15473;
        Object obj3 = this.f15474;
        switch (i) {
            case 0:
                C4379 c4379 = (C4379) obj3;
                C1300 c13002 = (C1300) obj2;
                C1300 c13003 = (C1300) obj;
                if (!c13002.m3829()) {
                    return ᵥˈᵛ.ˌᵨʠᵫ(new FirebaseRemoteConfigClientException("Firebase Installations failed to get installation auth token for config update listener connection.", c13002.m3830()));
                }
                if (!c13003.m3829()) {
                    return ᵥˈᵛ.ˌᵨʠᵫ(new FirebaseRemoteConfigClientException("Firebase Installations failed to get installation ID for config update listener connection.", c13003.m3830()));
                }
                try {
                    try {
                        url = new URL(c4379.m9390(c4379.f16622));
                    } catch (MalformedURLException unused) {
                    }
                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                    c4379.m9393(httpURLConnection, (String) c13003.m3826(), ((C3494) c13002.m3826()).f13442);
                    return ᵥˈᵛ.ᵸˆˁ(httpURLConnection);
                } catch (IOException e) {
                    return ᵥˈᵛ.ˌᵨʠᵫ(new FirebaseRemoteConfigClientException("Failed to open HTTP stream connection", e));
                }
            case 1:
                C0589 c0589 = (C0589) obj3;
                AtomicBoolean atomicBoolean = (AtomicBoolean) obj2;
                ᵧᵙᵤ r2 = (ᵧᵙᵤ) obj;
                if (c1300.m3829()) {
                    c0589.m2219(c1300.m3826());
                } else if (c1300.m3830() != null) {
                    c0589.m2220(c1300.m3830());
                } else if (atomicBoolean.getAndSet(true)) {
                    ((C1300) ((ᵷˇ) r2.ᵜᵪʾ).ˌᴵ).m3823(null);
                }
                return ᵥˈᵛ.ᵸˆˁ((Object) null);
            default:
                C1053 c1053 = (C1053) obj3;
                C1300 c13004 = (C1300) obj2;
                C1300 c13005 = (C1300) obj;
                if (!c13004.m3829() || c13004.m3826() == null) {
                    return ᵥˈᵛ.ᵸˆˁ(Boolean.FALSE);
                }
                C5235 c52352 = (C5235) c13004.m3826();
                return (c13005.m3829() && (c5235 = (C5235) c13005.m3826()) != null && c52352.f19737.equals(c5235.f19737)) ? ᵥˈᵛ.ᵸˆˁ(Boolean.FALSE) : c1053.f4540.m2562(c52352).m3828(c1053.f4533, new C2458(c1053));
        }
    }

    @Override // defpackage.InterfaceC3151
    /* JADX INFO: renamed from: ːˏᵟᵯ */
    public void mo1710(Object obj) {
        ᵥᵵᵶᵩ r0 = (ᵥᵵᵶᵩ) this.f15474;
        C1300 c1300 = (C1300) this.f15473;
        C5246 c5246 = (C5246) this.f15475;
        try {
            C5235 c5235 = (C5235) c1300.m3826();
            if (c5235 != null) {
                ((Executor) r0.ᵨᵼⁱ).execute(new RunnableC0492(c5246, ((C1709) r0.ᵔᵖᵹᵫ).m4729(c5235), 1));
            }
        } catch (FirebaseRemoteConfigException unused) {
        }
    }

    @Override // defpackage.InterfaceC1633
    /* JADX INFO: renamed from: ᵲᵶᵳˈ */
    public Object mo2135() {
        C3804 c3804 = (C3804) this.f15474;
        C3147 c3147 = (C3147) this.f15473;
        C4589 c4589 = (C4589) this.f15475;
        C0520 c0520 = c3804.f14435;
        c0520.getClass();
        EnumC3785 enumC3785 = c3147.f11993;
        if (Log.isLoggable(ᵸˊʾᵜ.ˉᵲ("SQLiteEventStore"), 3)) {
            new StringBuilder("Storing event with priority=").append(enumC3785);
        }
        ((Long) c0520.m2013(new C4021(c0520, c4589, c3147, 8))).getClass();
        c3804.f14434.ᵗᵭ(c3147, 1, false);
        return null;
    }

    @Override // defpackage.InterfaceC2902
    /* JADX INFO: renamed from: ᵻʽᵯ */
    public C2598 mo6785(int i, C1882 c1882, int[] iArr) {
        C2600 c2600 = (C2600) this.f15474;
        String str = (String) this.f15473;
        String str2 = (String) this.f15475;
        C3975 c3975M9561 = AbstractC4509.m9561();
        for (int i2 = 0; i2 < c1882.f7462; i2++) {
            c3975M9561.m7406(new C3527(i, c1882, i2, c2600, iArr[i2], str, str2));
        }
        return c3975M9561.m8718();
    }
}
