package F2;

import A1.G;
import A1.y;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import d5.AbstractC0844a;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.Executor;
import n4.u0;
import q1.C1450g;
import w2.C1699b;
import x2.C1711a;
import x2.C1712b;
import y2.H;
import y2.J;
import y2.n;
import y2.o;
import y2.p;
import y2.q;
import y2.r;
import y2.s;
import y2.t;
import y2.v;
import y2.z;
import z2.m;

/* JADX INFO: loaded from: classes.dex */
public final class i {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final Context f2464a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final A2.f f2465b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final G2.d f2466c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final c f2467d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final Executor f2468e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final H2.c f2469f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final I2.a f2470g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final I2.a f2471h;
    public final G2.c i;

    public i(Context context, A2.f fVar, G2.d dVar, c cVar, Executor executor, H2.c cVar2, I2.a aVar, I2.a aVar2, G2.c cVar3) {
        this.f2464a = context;
        this.f2465b = fVar;
        this.f2466c = dVar;
        this.f2467d = cVar;
        this.f2468e = executor;
        this.f2469f = cVar2;
        this.f2470g = aVar;
        this.f2471h = aVar2;
        this.i = cVar3;
    }

    /* JADX WARN: Removed duplicated region for block: B:145:0x0452  */
    /* JADX WARN: Removed duplicated region for block: B:180:0x0439 A[SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void a(z2.i iVar, int i) {
        A2.h hVar;
        A2.a aVar;
        String str;
        int i5;
        H0.g gVarB;
        int i8;
        String str2;
        Integer numValueOf;
        String str3;
        A2.h hVar2;
        s4.b bVar;
        ArrayList arrayList;
        int i9;
        int i10;
        final i iVar2 = this;
        final z2.i iVar3 = iVar;
        final int i11 = 0;
        int i12 = 3;
        final int i13 = 1;
        A2.h hVarA = iVar2.f2465b.a(iVar3.f19260a);
        long jMax = 0;
        while (true) {
            H2.b bVar2 = new H2.b(iVar2) { // from class: F2.f

                /* JADX INFO: renamed from: r, reason: collision with root package name */
                public final /* synthetic */ i f2454r;

                {
                    this.f2454r = iVar2;
                }

                @Override // H2.b
                public final Object g() {
                    Boolean bool;
                    switch (i11) {
                        case u0.f15495q /* 0 */:
                            z2.i iVar4 = iVar3;
                            G2.h hVar3 = (G2.h) this.f2454r.f2466c;
                            SQLiteDatabase sQLiteDatabaseA = hVar3.a();
                            sQLiteDatabaseA.beginTransaction();
                            try {
                                Long lB = G2.h.b(sQLiteDatabaseA, iVar4);
                                if (lB == null) {
                                    bool = Boolean.FALSE;
                                } else {
                                    Cursor cursorRawQuery = hVar3.a().rawQuery("SELECT 1 FROM events WHERE context_id = ? LIMIT 1", new String[]{lB.toString()});
                                    try {
                                        Boolean boolValueOf = Boolean.valueOf(cursorRawQuery.moveToNext());
                                        cursorRawQuery.close();
                                        bool = boolValueOf;
                                    } catch (Throwable th) {
                                        cursorRawQuery.close();
                                        throw th;
                                    }
                                }
                                sQLiteDatabaseA.setTransactionSuccessful();
                                return bool;
                            } finally {
                                sQLiteDatabaseA.endTransaction();
                            }
                        default:
                            G2.h hVar4 = (G2.h) this.f2454r.f2466c;
                            hVar4.getClass();
                            return (Iterable) hVar4.g(new C0.b(hVar4, 4, iVar3));
                    }
                }
            };
            G2.h hVar3 = (G2.h) iVar2.f2469f;
            if (!((Boolean) hVar3.n(bVar2)).booleanValue()) {
                hVar3.n(new h(iVar2, iVar3, jMax));
                return;
            }
            Iterable iterable = (Iterable) hVar3.n(new H2.b(iVar2) { // from class: F2.f

                /* JADX INFO: renamed from: r, reason: collision with root package name */
                public final /* synthetic */ i f2454r;

                {
                    this.f2454r = iVar2;
                }

                @Override // H2.b
                public final Object g() {
                    Boolean bool;
                    switch (i13) {
                        case u0.f15495q /* 0 */:
                            z2.i iVar4 = iVar3;
                            G2.h hVar32 = (G2.h) this.f2454r.f2466c;
                            SQLiteDatabase sQLiteDatabaseA = hVar32.a();
                            sQLiteDatabaseA.beginTransaction();
                            try {
                                Long lB = G2.h.b(sQLiteDatabaseA, iVar4);
                                if (lB == null) {
                                    bool = Boolean.FALSE;
                                } else {
                                    Cursor cursorRawQuery = hVar32.a().rawQuery("SELECT 1 FROM events WHERE context_id = ? LIMIT 1", new String[]{lB.toString()});
                                    try {
                                        Boolean boolValueOf = Boolean.valueOf(cursorRawQuery.moveToNext());
                                        cursorRawQuery.close();
                                        bool = boolValueOf;
                                    } catch (Throwable th) {
                                        cursorRawQuery.close();
                                        throw th;
                                    }
                                }
                                sQLiteDatabaseA.setTransactionSuccessful();
                                return bool;
                            } finally {
                                sQLiteDatabaseA.endTransaction();
                            }
                        default:
                            G2.h hVar4 = (G2.h) this.f2454r.f2466c;
                            hVar4.getClass();
                            return (Iterable) hVar4.g(new C0.b(hVar4, 4, iVar3));
                    }
                }
            });
            if (!iterable.iterator().hasNext()) {
                return;
            }
            byte[] bArr = iVar3.f19261b;
            if (hVarA == null) {
                android.support.v4.media.session.a.e("Uploader", "Unknown backend for %s, deleting event batch for it...", iVar3);
                aVar = new A2.a(-1L, i12);
                hVar = hVarA;
            } else {
                ArrayList arrayList2 = new ArrayList();
                Iterator it = iterable.iterator();
                while (it.hasNext()) {
                    arrayList2.add(((G2.b) it.next()).f2936c);
                    i13 = i13;
                }
                int i14 = i13;
                String str4 = "proto";
                if ((bArr != null ? i14 : i11) != 0) {
                    G2.c cVar = iVar2.i;
                    Objects.requireNonNull(cVar);
                    C2.b bVar3 = (C2.b) hVar3.n(new G(12, cVar));
                    h6.c cVar2 = new h6.c();
                    cVar2.f13437f = new HashMap();
                    cVar2.f13435d = Long.valueOf(iVar2.f2470g.d());
                    cVar2.f13436e = Long.valueOf(iVar2.f2471h.d());
                    cVar2.f13432a = "GDT_CLIENT_METRICS";
                    C1699b c1699b = new C1699b("proto");
                    bVar3.getClass();
                    y yVar = m.f19272a;
                    yVar.getClass();
                    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                    try {
                        yVar.p(bVar3, byteArrayOutputStream);
                    } catch (IOException unused) {
                    }
                    cVar2.f13434c = new z2.k(c1699b, byteArrayOutputStream.toByteArray());
                    arrayList2.add(((C1712b) hVarA).a(cVar2.b()));
                }
                C1712b c1712b = (C1712b) hVarA;
                HashMap map = new HashMap();
                int size = arrayList2.size();
                int i15 = 0;
                while (i15 < size) {
                    Object obj = arrayList2.get(i15);
                    i15++;
                    z2.h hVar4 = (z2.h) obj;
                    String str5 = hVar4.f19251a;
                    if (map.containsKey(str5)) {
                        arrayList = arrayList2;
                        ((List) map.get(str5)).add(hVar4);
                    } else {
                        arrayList = arrayList2;
                        ArrayList arrayList3 = new ArrayList();
                        arrayList3.add(hVar4);
                        map.put(str5, arrayList3);
                    }
                    arrayList2 = arrayList;
                }
                ArrayList arrayList4 = new ArrayList();
                Iterator it2 = map.entrySet().iterator();
                while (it2.hasNext()) {
                    Map.Entry entry = (Map.Entry) it2.next();
                    z2.h hVar5 = (z2.h) ((List) entry.getValue()).get(0);
                    J j8 = J.f18890q;
                    long jD = c1712b.f18582f.d();
                    long jD2 = c1712b.f18581e.d();
                    n nVar = new n(new y2.l(Integer.valueOf(hVar5.b("sdk-version")), hVar5.a("model"), hVar5.a("hardware"), hVar5.a("device"), hVar5.a("product"), hVar5.a("os-uild"), hVar5.a("manufacturer"), hVar5.a("fingerprint"), hVar5.a("locale"), hVar5.a("country"), hVar5.a("mcc_mnc"), hVar5.a("application_build")));
                    try {
                        numValueOf = Integer.valueOf(Integer.parseInt((String) entry.getKey()));
                        str2 = null;
                    } catch (NumberFormatException unused2) {
                        str2 = (String) entry.getKey();
                        numValueOf = null;
                    }
                    ArrayList arrayList5 = new ArrayList();
                    Iterator it3 = ((List) entry.getValue()).iterator();
                    while (it3.hasNext()) {
                        z2.h hVar6 = (z2.h) it3.next();
                        z2.k kVar = hVar6.f19253c;
                        Iterator it4 = it2;
                        C1699b c1699b2 = kVar.f19269a;
                        Iterator it5 = it3;
                        boolean zEquals = c1699b2.equals(new C1699b(str4));
                        byte[] bArr2 = kVar.f19270b;
                        if (zEquals) {
                            bVar = new s4.b();
                            bVar.f16868e = bArr2;
                            str3 = str4;
                        } else {
                            str3 = str4;
                            if (c1699b2.equals(new C1699b("json"))) {
                                String str6 = new String(bArr2, Charset.forName("UTF-8"));
                                s4.b bVar4 = new s4.b();
                                bVar4.f16869f = str6;
                                bVar = bVar4;
                            } else {
                                hVar2 = hVarA;
                                String strN = android.support.v4.media.session.a.n("CctTransportBackend");
                                if (Log.isLoggable(strN, 5)) {
                                    Log.w(strN, "Received event of unsupported encoding " + c1699b2 + ". Skipping...");
                                }
                                hVarA = hVar2;
                                it3 = it5;
                                it2 = it4;
                                str4 = str3;
                            }
                        }
                        hVar2 = hVarA;
                        bVar.f16864a = Long.valueOf(hVar6.f19254d);
                        bVar.f16867d = Long.valueOf(hVar6.f19255e);
                        String str7 = (String) hVar6.f19256f.get("tz-offset");
                        bVar.f16870g = Long.valueOf(str7 == null ? 0L : Long.valueOf(str7).longValue());
                        bVar.f16871h = new v((H) H.f18888q.get(hVar6.b("net-type")), (y2.G) y2.G.f18886q.get(hVar6.b("mobile-subtype")));
                        Integer num = hVar6.f19252b;
                        if (num != null) {
                            bVar.f16865b = num;
                        }
                        Integer num2 = hVar6.f19257g;
                        if (num2 != null) {
                            r rVar = new r(new q(num2));
                            z zVar = z.f18976q;
                            bVar.f16866c = new o(rVar);
                        }
                        byte[] bArr3 = hVar6.f19259j;
                        byte[] bArr4 = hVar6.i;
                        if (bArr4 != null || bArr3 != null) {
                            if (bArr4 == null) {
                                bArr4 = null;
                            }
                            if (bArr3 == null) {
                                bArr3 = null;
                            }
                            bVar.i = new p(bArr4, bArr3);
                        }
                        String strL = ((Long) bVar.f16864a) == null ? " eventTimeMs" : "";
                        if (((Long) bVar.f16867d) == null) {
                            strL = strL.concat(" eventUptimeMs");
                        }
                        if (((Long) bVar.f16870g) == null) {
                            strL = AbstractC0844a.l(strL, " timezoneOffsetSeconds");
                        }
                        if (!strL.isEmpty()) {
                            throw new IllegalStateException("Missing required properties:".concat(strL));
                        }
                        arrayList5.add(new s(((Long) bVar.f16864a).longValue(), (Integer) bVar.f16865b, (o) bVar.f16866c, ((Long) bVar.f16867d).longValue(), (byte[]) bVar.f16868e, (String) bVar.f16869f, ((Long) bVar.f16870g).longValue(), (v) bVar.f16871h, (p) bVar.i));
                        hVarA = hVar2;
                        it3 = it5;
                        it2 = it4;
                        str4 = str3;
                    }
                    arrayList4.add(new t(jD, jD2, nVar, numValueOf, str2, arrayList5));
                    hVarA = hVarA;
                    it2 = it2;
                    str4 = str4;
                }
                hVar = hVarA;
                y2.m mVar = new y2.m(arrayList4);
                URL urlB = c1712b.f18580d;
                if (bArr != null) {
                    try {
                        C1711a c1711aA = C1711a.a(bArr);
                        str = c1711aA.f18576b;
                        if (str == null) {
                            str = null;
                        }
                        String str8 = c1711aA.f18575a;
                        if (str8 != null) {
                            urlB = C1712b.b(str8);
                        }
                    } catch (IllegalArgumentException unused3) {
                        aVar = new A2.a(-1L, 3);
                        i5 = 2;
                    }
                } else {
                    str = null;
                }
                int i16 = 3;
                try {
                    try {
                        com.google.android.material.datepicker.c cVar3 = new com.google.android.material.datepicker.c(urlB, mVar, str, 16);
                        C1450g c1450g = new C1450g(i16, c1712b);
                        int i17 = 5;
                        while (true) {
                            gVarB = c1450g.b(cVar3);
                            URL url = (URL) gVarB.f3051c;
                            if (url != null) {
                                android.support.v4.media.session.a.e("CctTransportBackend", "Following redirect to: %s", url);
                                cVar3 = new com.google.android.material.datepicker.c(url, (y2.m) cVar3.f10220s, (String) cVar3.f10221t, 16);
                            } else {
                                cVar3 = null;
                            }
                            if (cVar3 == null || i17 - 1 < (i8 = i14)) {
                                break;
                            } else {
                                i14 = i8;
                            }
                        }
                        int i18 = gVarB.f3049a;
                        if (i18 == 200) {
                            aVar = new A2.a(gVarB.f3050b, 1);
                        } else if (i18 >= 500 || i18 == 404) {
                            aVar = new A2.a(-1L, 2);
                        } else if (i18 == 400) {
                            try {
                                aVar = new A2.a(-1L, 4);
                            } catch (IOException e8) {
                                e = e8;
                                android.support.v4.media.session.a.g("CctTransportBackend", "Could not make request to the backend", e);
                                i5 = 2;
                                aVar = new A2.a(-1L, 2);
                            }
                        } else {
                            aVar = new A2.a(-1L, 3);
                        }
                        i5 = 2;
                    } catch (IOException e9) {
                        e = e9;
                    }
                } catch (IOException e10) {
                    e = e10;
                }
                i9 = aVar.f377a;
                if (i9 != i5) {
                    hVar3.n(new g(this, iterable, iVar, jMax));
                    this.f2467d.a(iVar, i + 1, true);
                    return;
                }
                hVar3.n(new C0.b(this, i5, iterable));
                if (i9 == 1) {
                    jMax = Math.max(jMax, aVar.f378b);
                    if (bArr != null) {
                        hVar3.n(new G(14, this));
                    }
                } else {
                    if (i9 == 4) {
                        HashMap map2 = new HashMap();
                        Iterator it6 = iterable.iterator();
                        while (it6.hasNext()) {
                            String str9 = ((G2.b) it6.next()).f2936c.f19251a;
                            if (map2.containsKey(str9)) {
                                map2.put(str9, Integer.valueOf(((Integer) map2.get(str9)).intValue() + 1));
                            } else {
                                map2.put(str9, 1);
                            }
                        }
                        i10 = 1;
                        i12 = 3;
                        hVar3.n(new C0.b(this, i12, map2));
                    }
                    iVar2 = this;
                    hVarA = hVar;
                    iVar3 = iVar;
                    i13 = i10;
                    i11 = 0;
                }
                i12 = 3;
                i10 = 1;
                iVar2 = this;
                hVarA = hVar;
                iVar3 = iVar;
                i13 = i10;
                i11 = 0;
            }
            i5 = 2;
            i9 = aVar.f377a;
            if (i9 != i5) {
            }
        }
    }
}
