package d3;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.zip.GZIPOutputStream;

/* JADX INFO: renamed from: d3.b0, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public final class RunnableC0770b0 implements Runnable {

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public final /* synthetic */ int f11892q = 0;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public final URL f11893r;

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public final byte[] f11894s;

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public final String f11895t;

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public final Object f11896u;

    /* JADX INFO: renamed from: v, reason: collision with root package name */
    public final Map f11897v;

    /* JADX INFO: renamed from: w, reason: collision with root package name */
    public final /* synthetic */ O3.e f11898w;

    public RunnableC0770b0(C0773c0 c0773c0, String str, URL url, byte[] bArr, Map map, InterfaceC0767a0 interfaceC0767a0) {
        this.f11898w = c0773c0;
        P2.w.c(str);
        P2.w.f(url);
        this.f11893r = url;
        this.f11894s = bArr;
        this.f11896u = interfaceC0767a0;
        this.f11895t = str;
        this.f11897v = map;
    }

    public void a(int i, IOException iOException, byte[] bArr, Map map) {
        C0815q0 c0815q0 = ((C0817r0) ((C0774c1) this.f11898w).f5146q).f12151z;
        C0817r0.k(c0815q0);
        c0815q0.O(new RunnableC0771b1(this, i, iOException, bArr, map));
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Not initialized variable reg: 13, insn: 0x0287: MOVE (r11 I:??[OBJECT, ARRAY]) = (r13 I:??[OBJECT, ARRAY]) (LINE:648), block:B:141:0x0285 */
    /* JADX WARN: Not initialized variable reg: 13, insn: 0x028a: MOVE (r12 I:??[OBJECT, ARRAY]) = (r13 I:??[OBJECT, ARRAY]) (LINE:651), block:B:142:0x0289 */
    /* JADX WARN: Removed duplicated region for block: B:163:0x02c5  */
    /* JADX WARN: Removed duplicated region for block: B:173:0x02fa  */
    /* JADX WARN: Removed duplicated region for block: B:177:0x02b0 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:179:0x0141 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:189:0x02e5 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:191:0x0163 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:80:0x0156  */
    /* JADX WARN: Removed duplicated region for block: B:90:0x0178  */
    /* JADX WARN: Type inference failed for: r14v0, types: [d3.b0] */
    /* JADX WARN: Type inference failed for: r8v36 */
    /* JADX WARN: Type inference failed for: r8v47 */
    /* JADX WARN: Type inference failed for: r8v48 */
    /* JADX WARN: Type inference failed for: r9v21, types: [java.util.Map] */
    /* JADX WARN: Type inference failed for: r9v22, types: [java.util.Map] */
    /* JADX WARN: Type inference failed for: r9v27 */
    /* JADX WARN: Type inference failed for: r9v28 */
    /* JADX WARN: Type inference failed for: r9v29 */
    /* JADX WARN: Type inference failed for: r9v30 */
    /* JADX WARN: Type inference failed for: r9v36 */
    /* JADX WARN: Type inference failed for: r9v37 */
    @Override // java.lang.Runnable
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void run() throws Throwable {
        int i;
        HttpURLConnection httpURLConnection;
        Map map;
        IOException iOException;
        int i5;
        Map map2;
        Throwable th;
        int responseCode;
        Map map3;
        Map map4;
        InputStream inputStream;
        ByteArrayOutputStream byteArrayOutputStream;
        int responseCode2;
        HttpURLConnection httpURLConnection2;
        OutputStream outputStream;
        OutputStream outputStream2;
        OutputStream outputStream3;
        OutputStream outputStream4;
        OutputStream outputStream5;
        Throwable th2;
        OutputStream outputStream6;
        IOException iOException2;
        ?? r9;
        OutputStream outputStream7;
        ?? r92;
        OutputStream outputStream8;
        ?? r8;
        InputStream inputStream2;
        boolean zHasNext;
        switch (this.f11892q) {
            case n4.u0.f15495q /* 0 */:
                String str = this.f11895t;
                C0773c0 c0773c0 = (C0773c0) this.f11898w;
                C0817r0 c0817r0 = (C0817r0) c0773c0.f5146q;
                C0817r0 c0817r02 = (C0817r0) c0773c0.f5146q;
                C0815q0 c0815q0 = c0817r0.f12151z;
                C0817r0.k(c0815q0);
                c0815q0.I();
                OutputStream outputStream9 = null;
                try {
                    URLConnection uRLConnectionOpenConnection = this.f11893r.openConnection();
                    if (!(uRLConnectionOpenConnection instanceof HttpURLConnection)) {
                        throw new IOException("Failed to obtain HTTP connection");
                    }
                    httpURLConnection = (HttpURLConnection) uRLConnectionOpenConnection;
                    httpURLConnection.setDefaultUseCaches(false);
                    c0817r02.getClass();
                    httpURLConnection.setConnectTimeout(60000);
                    httpURLConnection.setReadTimeout(61000);
                    httpURLConnection.setInstanceFollowRedirects(false);
                    httpURLConnection.setDoInput(true);
                    try {
                        Map map5 = this.f11897v;
                        if (map5 != null) {
                            for (Map.Entry entry : map5.entrySet()) {
                                httpURLConnection.addRequestProperty((String) entry.getKey(), (String) entry.getValue());
                            }
                        }
                        byte[] bArr = this.f11894s;
                        if (bArr != null) {
                            C0773c0 c0773c02 = c0773c0.f11473r.f11687w;
                            Q1.L(c0773c02);
                            byte[] bArrJ = c0773c02.J(bArr);
                            Z z8 = c0817r02.f12150y;
                            C0817r0.k(z8);
                            X x8 = z8.D;
                            int length = bArrJ.length;
                            x8.c(Integer.valueOf(length), "Uploading data. size");
                            httpURLConnection.setDoOutput(true);
                            httpURLConnection.addRequestProperty("Content-Encoding", "gzip");
                            httpURLConnection.setFixedLengthStreamingMode(length);
                            httpURLConnection.connect();
                            OutputStream outputStream10 = httpURLConnection.getOutputStream();
                            try {
                                outputStream10.write(bArrJ);
                                outputStream10.close();
                            } catch (IOException e8) {
                                iOException = e8;
                                i5 = 0;
                                map2 = null;
                                outputStream9 = outputStream10;
                                if (outputStream9 != null) {
                                    try {
                                        outputStream9.close();
                                    } catch (IOException e9) {
                                        Z z9 = c0817r02.f12150y;
                                        C0817r0.k(z9);
                                        z9.f11875v.d("Error closing HTTP compressed POST connection output stream. appId", Z.M(str), e9);
                                    }
                                    break;
                                }
                                if (httpURLConnection != null) {
                                    httpURLConnection.disconnect();
                                }
                                C0815q0 c0815q02 = c0817r02.f12151z;
                                C0817r0.k(c0815q02);
                                c0815q02.O(new W(this.f11895t, (InterfaceC0767a0) this.f11896u, i5, iOException, (byte[]) null, map2));
                                return;
                            } catch (Throwable th3) {
                                th = th3;
                                i = 0;
                                map = null;
                                outputStream9 = outputStream10;
                                th = th;
                                if (outputStream9 != null) {
                                    try {
                                        outputStream9.close();
                                    } catch (IOException e10) {
                                        Z z10 = c0817r02.f12150y;
                                        C0817r0.k(z10);
                                        z10.f11875v.d("Error closing HTTP compressed POST connection output stream. appId", Z.M(str), e10);
                                    }
                                    break;
                                }
                                if (httpURLConnection != null) {
                                    httpURLConnection.disconnect();
                                }
                                C0815q0 c0815q03 = c0817r02.f12151z;
                                C0817r0.k(c0815q03);
                                c0815q03.O(new W(this.f11895t, (InterfaceC0767a0) this.f11896u, i, (IOException) null, (byte[]) null, map));
                                throw th;
                            }
                        }
                        responseCode = httpURLConnection.getResponseCode();
                    } catch (IOException e11) {
                        iOException = e11;
                        i5 = 0;
                        map2 = null;
                    } catch (Throwable th4) {
                        th = th4;
                        i = 0;
                        map = null;
                    }
                    try {
                        try {
                            Map<String, List<String>> headerFields = httpURLConnection.getHeaderFields();
                            try {
                                byteArrayOutputStream = new ByteArrayOutputStream();
                                inputStream = httpURLConnection.getInputStream();
                            } catch (Throwable th5) {
                                th = th5;
                                inputStream = null;
                            }
                            try {
                                byte[] bArr2 = new byte[1024];
                                while (true) {
                                    int i8 = inputStream.read(bArr2);
                                    if (i8 <= 0) {
                                        byte[] byteArray = byteArrayOutputStream.toByteArray();
                                        inputStream.close();
                                        httpURLConnection.disconnect();
                                        C0815q0 c0815q04 = c0817r02.f12151z;
                                        C0817r0.k(c0815q04);
                                        c0815q04.O(new W(this.f11895t, (InterfaceC0767a0) this.f11896u, responseCode, (IOException) null, byteArray, headerFields));
                                        return;
                                    }
                                    byteArrayOutputStream.write(bArr2, 0, i8);
                                }
                            } catch (Throwable th6) {
                                th = th6;
                                if (inputStream != null) {
                                    inputStream.close();
                                }
                                throw th;
                            }
                        } catch (IOException e12) {
                            e = e12;
                            i5 = responseCode;
                            map2 = map4;
                            iOException = e;
                            if (outputStream9 != null) {
                            }
                            if (httpURLConnection != null) {
                            }
                            C0815q0 c0815q022 = c0817r02.f12151z;
                            C0817r0.k(c0815q022);
                            c0815q022.O(new W(this.f11895t, (InterfaceC0767a0) this.f11896u, i5, iOException, (byte[]) null, map2));
                            return;
                        } catch (Throwable th7) {
                            th = th7;
                            i = responseCode;
                            map = map3;
                            if (outputStream9 != null) {
                            }
                            if (httpURLConnection != null) {
                            }
                            C0815q0 c0815q032 = c0817r02.f12151z;
                            C0817r0.k(c0815q032);
                            c0815q032.O(new W(this.f11895t, (InterfaceC0767a0) this.f11896u, i, (IOException) null, (byte[]) null, map));
                            throw th;
                        }
                    } catch (IOException e13) {
                        e = e13;
                        map2 = null;
                        i5 = responseCode;
                        iOException = e;
                        if (outputStream9 != null) {
                        }
                        if (httpURLConnection != null) {
                        }
                        C0815q0 c0815q0222 = c0817r02.f12151z;
                        C0817r0.k(c0815q0222);
                        c0815q0222.O(new W(this.f11895t, (InterfaceC0767a0) this.f11896u, i5, iOException, (byte[]) null, map2));
                        return;
                    } catch (Throwable th8) {
                        th = th8;
                        map = null;
                        i = responseCode;
                        if (outputStream9 != null) {
                        }
                        if (httpURLConnection != null) {
                        }
                        C0815q0 c0815q0322 = c0817r02.f12151z;
                        C0817r0.k(c0815q0322);
                        c0815q0322.O(new W(this.f11895t, (InterfaceC0767a0) this.f11896u, i, (IOException) null, (byte[]) null, map));
                        throw th;
                    }
                } catch (IOException e14) {
                    iOException = e14;
                    i5 = 0;
                    httpURLConnection = null;
                    map2 = null;
                } catch (Throwable th9) {
                    th = th9;
                    i = 0;
                    httpURLConnection = null;
                    map = null;
                }
                break;
            default:
                String str2 = this.f11895t;
                C0774c1 c0774c1 = (C0774c1) this.f11898w;
                C0817r0 c0817r03 = (C0817r0) c0774c1.f5146q;
                C0817r0 c0817r04 = (C0817r0) c0774c1.f5146q;
                C0815q0 c0815q05 = c0817r03.f12151z;
                C0817r0.k(c0815q05);
                c0815q05.I();
                try {
                    URLConnection uRLConnectionOpenConnection2 = this.f11893r.openConnection();
                    if (!(uRLConnectionOpenConnection2 instanceof HttpURLConnection)) {
                        throw new IOException("Failed to obtain HTTP connection");
                    }
                    httpURLConnection2 = (HttpURLConnection) uRLConnectionOpenConnection2;
                    httpURLConnection2.setDefaultUseCaches(false);
                    c0817r04.getClass();
                    httpURLConnection2.setConnectTimeout(60000);
                    httpURLConnection2.setReadTimeout(61000);
                    httpURLConnection2.setInstanceFollowRedirects(false);
                    httpURLConnection2.setDoInput(true);
                    try {
                        try {
                            HashMap map6 = (HashMap) this.f11897v;
                            if (map6 != null) {
                                Iterator it = map6.entrySet().iterator();
                                while (true) {
                                    zHasNext = it.hasNext();
                                    if (zHasNext) {
                                        Map.Entry entry2 = (Map.Entry) it.next();
                                        httpURLConnection2.addRequestProperty((String) entry2.getKey(), (String) entry2.getValue());
                                    }
                                }
                            }
                            byte[] bArr3 = this.f11894s;
                            r8 = zHasNext;
                            if (bArr3 != null) {
                                try {
                                    ByteArrayOutputStream byteArrayOutputStream2 = new ByteArrayOutputStream();
                                    GZIPOutputStream gZIPOutputStream = new GZIPOutputStream(byteArrayOutputStream2);
                                    gZIPOutputStream.write(bArr3);
                                    gZIPOutputStream.close();
                                    byteArrayOutputStream2.close();
                                    byte[] byteArray2 = byteArrayOutputStream2.toByteArray();
                                    Z z11 = c0817r04.f12150y;
                                    C0817r0.k(z11);
                                    X x9 = z11.D;
                                    int length2 = byteArray2.length;
                                    x9.c(Integer.valueOf(length2), "Uploading data. size");
                                    httpURLConnection2.setDoOutput(true);
                                    httpURLConnection2.addRequestProperty("Content-Encoding", "gzip");
                                    httpURLConnection2.setFixedLengthStreamingMode(length2);
                                    httpURLConnection2.connect();
                                    OutputStream outputStream11 = httpURLConnection2.getOutputStream();
                                    try {
                                        outputStream11.write(byteArray2);
                                        outputStream11.close();
                                        r8 = outputStream11;
                                    } catch (IOException e15) {
                                        e = e15;
                                        responseCode2 = 0;
                                        outputStream4 = null;
                                        outputStream6 = outputStream11;
                                        iOException2 = e;
                                        outputStream8 = outputStream6;
                                        r92 = outputStream4;
                                        if (outputStream8 != null) {
                                        }
                                        if (httpURLConnection2 != null) {
                                        }
                                        a(responseCode2, iOException2, null, r92);
                                        return;
                                    } catch (Throwable th10) {
                                        th = th10;
                                        responseCode2 = 0;
                                        outputStream3 = null;
                                        outputStream5 = outputStream11;
                                        th2 = th;
                                        outputStream7 = outputStream5;
                                        r9 = outputStream3;
                                        if (outputStream7 != null) {
                                        }
                                        if (httpURLConnection2 != null) {
                                        }
                                        a(responseCode2, null, null, r9);
                                        throw th2;
                                    }
                                } catch (IOException e16) {
                                    Z z12 = c0817r04.f12150y;
                                    C0817r0.k(z12);
                                    z12.f11875v.c(e16, "Failed to gzip post request content");
                                    throw e16;
                                }
                            }
                            responseCode2 = httpURLConnection2.getResponseCode();
                        } catch (IOException e17) {
                            e = e17;
                            responseCode2 = 0;
                            outputStream2 = null;
                            outputStream4 = outputStream2;
                            outputStream6 = outputStream2;
                            iOException2 = e;
                            outputStream8 = outputStream6;
                            r92 = outputStream4;
                            if (outputStream8 != null) {
                                try {
                                    outputStream8.close();
                                } catch (IOException e18) {
                                    Z z13 = c0817r04.f12150y;
                                    C0817r0.k(z13);
                                    z13.f11875v.d("Error closing HTTP compressed POST connection output stream. appId", Z.M(str2), e18);
                                }
                                break;
                            }
                            if (httpURLConnection2 != null) {
                                httpURLConnection2.disconnect();
                            }
                            a(responseCode2, iOException2, null, r92);
                            return;
                        }
                    } catch (Throwable th11) {
                        th = th11;
                        responseCode2 = 0;
                        outputStream = null;
                        outputStream3 = outputStream;
                        outputStream5 = outputStream;
                        th2 = th;
                        outputStream7 = outputStream5;
                        r9 = outputStream3;
                        if (outputStream7 != null) {
                            try {
                                outputStream7.close();
                            } catch (IOException e19) {
                                Z z14 = c0817r04.f12150y;
                                C0817r0.k(z14);
                                z14.f11875v.d("Error closing HTTP compressed POST connection output stream. appId", Z.M(str2), e19);
                            }
                            break;
                        }
                        if (httpURLConnection2 != null) {
                            httpURLConnection2.disconnect();
                        }
                        a(responseCode2, null, null, r9);
                        throw th2;
                    }
                    try {
                        try {
                            Map<String, List<String>> headerFields2 = httpURLConnection2.getHeaderFields();
                            try {
                                ByteArrayOutputStream byteArrayOutputStream3 = new ByteArrayOutputStream();
                                inputStream2 = httpURLConnection2.getInputStream();
                                try {
                                    byte[] bArr4 = new byte[1024];
                                    while (true) {
                                        int i9 = inputStream2.read(bArr4);
                                        if (i9 <= 0) {
                                            byte[] byteArray3 = byteArrayOutputStream3.toByteArray();
                                            inputStream2.close();
                                            httpURLConnection2.disconnect();
                                            a(responseCode2, null, byteArray3, headerFields2);
                                            return;
                                        }
                                        byteArrayOutputStream3.write(bArr4, 0, i9);
                                    }
                                } catch (Throwable th12) {
                                    th = th12;
                                    if (inputStream2 != null) {
                                        inputStream2.close();
                                    }
                                    throw th;
                                }
                            } catch (Throwable th13) {
                                th = th13;
                                inputStream2 = null;
                            }
                        } catch (IOException e20) {
                            iOException2 = e20;
                            r92 = r8;
                            outputStream8 = null;
                            if (outputStream8 != null) {
                            }
                            if (httpURLConnection2 != null) {
                            }
                            a(responseCode2, iOException2, null, r92);
                            return;
                        } catch (Throwable th14) {
                            th2 = th14;
                            r9 = r8;
                            outputStream7 = null;
                            if (outputStream7 != null) {
                            }
                            if (httpURLConnection2 != null) {
                            }
                            a(responseCode2, null, null, r9);
                            throw th2;
                        }
                    } catch (IOException e21) {
                        iOException2 = e21;
                        outputStream8 = null;
                        r92 = 0;
                        if (outputStream8 != null) {
                        }
                        if (httpURLConnection2 != null) {
                        }
                        a(responseCode2, iOException2, null, r92);
                        return;
                    } catch (Throwable th15) {
                        th2 = th15;
                        outputStream7 = null;
                        r9 = 0;
                        if (outputStream7 != null) {
                        }
                        if (httpURLConnection2 != null) {
                        }
                        a(responseCode2, null, null, r9);
                        throw th2;
                    }
                } catch (IOException e22) {
                    e = e22;
                    responseCode2 = 0;
                    httpURLConnection2 = null;
                    outputStream2 = null;
                } catch (Throwable th16) {
                    th = th16;
                    responseCode2 = 0;
                    httpURLConnection2 = null;
                    outputStream = null;
                }
                break;
        }
    }

    public RunnableC0770b0(C0774c1 c0774c1, String str, URL url, byte[] bArr, HashMap map, InterfaceC0768a1 interfaceC0768a1) {
        this.f11898w = c0774c1;
        P2.w.c(str);
        this.f11893r = url;
        this.f11894s = bArr;
        this.f11896u = interfaceC0768a1;
        this.f11895t = str;
        this.f11897v = map;
    }
}
