package com.strmr.ps.update;

import android.util.Log;
import androidx.media3.exoplayer.rtsp.RtspHeaders;
import com.strmr.ps.data.model.UpdateInfo;
import com.strmr.ps.update.a;
import defpackage.ck1;
import defpackage.cr4;
import defpackage.dw7;
import defpackage.e6a;
import defpackage.f21;
import defpackage.gr3;
import defpackage.l43;
import defpackage.n41;
import defpackage.n89;
import defpackage.s38;
import defpackage.vx8;
import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import kotlin.Metadata;
import kotlin.coroutines.Continuation;
import kotlin.coroutines.jvm.internal.DebugMetadata;
import kotlin.coroutines.jvm.internal.SuspendLambda;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

/* JADX INFO: loaded from: classes3.dex */
@Metadata(d1 = {"\u0000\u0010\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\u0010\u0003\u001a\u00020\u0002*\b\u0012\u0004\u0012\u00020\u00010\u0000H\n¢\u0006\u0004\b\u0003\u0010\u0004"}, d2 = {"Ll43;", "Lcom/strmr/ps/update/a;", "Le6a;", "<anonymous>", "(Ll43;)V"}, k = 3, mv = {2, 0, 0})
@DebugMetadata(c = "com.strmr.ps.update.UpdateDownloadManager$downloadUpdate$1", f = "UpdateDownloadManager.kt", i = {0, 1, 1, 1, 1, 1, 1, 1, 1, 2}, l = {46, 96, 102, 106}, m = "invokeSuspend", n = {"$this$flow", "$this$flow", "apkFile", "downloadedBytes", "inputStream", "outputStream", "buffer", "bytesRead", "totalBytes", "$this$flow"}, s = {"L$0", "L$0", "L$1", "L$2", "L$4", "L$6", "L$7", "L$8", "J$0", "L$0"})
@vx8
final class b extends SuspendLambda implements gr3<l43<? super a>, Continuation<? super e6a>, Object> {
    public File a;
    public dw7.g b;
    public Closeable c;
    public InputStream d;
    public Closeable e;
    public FileOutputStream f;
    public byte[] i;
    public dw7.f j;
    public long k;
    public int l;
    public /* synthetic */ Object m;
    public final /* synthetic */ UpdateDownloadManager n;
    public final /* synthetic */ UpdateInfo o;

    /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
    public b(UpdateDownloadManager updateDownloadManager, UpdateInfo updateInfo, Continuation continuation) {
        super(2, continuation);
        this.n = updateDownloadManager;
        this.o = updateInfo;
    }

    @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl
    public final Continuation create(Object obj, Continuation continuation) {
        b bVar = new b(this.n, this.o, continuation);
        bVar.m = obj;
        return bVar;
    }

    @Override // defpackage.gr3
    public final Object invoke(Object obj, Object obj2) {
        return ((b) create((l43) obj, (Continuation) obj2)).invokeSuspend(e6a.a);
    }

    /* JADX WARN: Can't wrap try/catch for region: R(11:0|2|(1:(1:(3:(1:(1:8)(2:9|10))(3:11|131|12)|114|115)(9:16|139|17|18|75|126|53|(15:55|56|122|57|58|(4:60|137|61|62)(1:66)|63|67|68|120|69|70|141|71|(5:74|75|126|53|(4:84|116|85|86)(0)))(0)|113))(1:21))(1:22)|128|25|(1:27)|30|(6:32|(3:34|(2:36|144)(1:145)|37)|143|38|(2:41|39)|146)|42|(2:44|(2:46|(11:48|133|49|50|118|51|52|126|53|(0)(0)|113)(2:105|106))(2:107|108))(2:109|110)|(5:(0)|(0)|(1:130)|(1:136)|(1:125))) */
    /* JADX WARN: Code restructure failed: missing block: B:112:0x0275, code lost:
    
        if (r2.emit(r4, r19) == r3) goto L113;
     */
    /* JADX WARN: Code restructure failed: missing block: B:23:0x0077, code lost:
    
        if (r4.emit(com.strmr.ps.update.a.d.a, r19) == r3) goto L113;
     */
    /* JADX WARN: Code restructure failed: missing block: B:28:0x0092, code lost:
    
        r0 = e;
     */
    /* JADX WARN: Code restructure failed: missing block: B:29:0x0093, code lost:
    
        r2 = r4;
     */
    /* JADX WARN: Code restructure failed: missing block: B:87:0x01fc, code lost:
    
        if (r4.emit(r0, r19) == r3) goto L113;
     */
    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:55:0x015c A[Catch: all -> 0x01cb, TRY_LEAVE, TryCatch #5 {all -> 0x01cb, blocks: (B:53:0x0153, B:55:0x015c), top: B:126:0x0153 }] */
    /* JADX WARN: Removed duplicated region for block: B:84:0x01d2  */
    /* JADX WARN: Type inference failed for: r10v11 */
    /* JADX WARN: Type inference failed for: r10v12 */
    /* JADX WARN: Type inference failed for: r10v14 */
    /* JADX WARN: Type inference failed for: r10v15 */
    /* JADX WARN: Type inference failed for: r10v2 */
    /* JADX WARN: Type inference failed for: r10v7, types: [java.io.InputStream] */
    /* JADX WARN: Type inference failed for: r9v16, types: [java.io.InputStream] */
    /* JADX WARN: Unsupported multi-entry loop pattern (BACK_EDGE: B:74:0x01b2 -> B:75:0x01b9). Please report as a decompilation issue!!! */
    @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object invokeSuspend(Object obj) {
        l43 l43Var;
        File file;
        Closeable closeableByteStream;
        Throwable th;
        FileOutputStream fileOutputStream;
        Throwable th2;
        l43 l43Var2;
        Closeable closeable;
        dw7.g gVar;
        byte[] bArr;
        dw7.f fVar;
        Closeable closeable2;
        long j;
        ?? r10;
        int i;
        Closeable closeable3;
        long j2;
        Closeable closeable4;
        Closeable closeable5;
        int i2;
        dw7.g gVar2;
        UpdateInfo updateInfo = this.o;
        UpdateDownloadManager updateDownloadManager = this.n;
        ck1 ck1Var = ck1.a;
        int i3 = this.l;
        int i4 = 0;
        if (i3 == 0) {
            s38.b(obj);
            l43Var = (l43) this.m;
            this.m = l43Var;
            this.l = 1;
        } else if (i3 == 1) {
            l43Var = (l43) this.m;
            s38.b(obj);
        } else {
            if (i3 != 2) {
                if (i3 == 3) {
                    l43 l43Var3 = (l43) this.m;
                    try {
                        s38.b(obj);
                    } catch (Exception e) {
                        e = e;
                        Log.e("UpdateDownloadManager", "Download failed", e);
                        a.c cVar = new a.c(n41.n("Download failed: ", e.getMessage()), e);
                        this.m = null;
                        this.a = null;
                        this.b = null;
                        this.c = null;
                        this.d = null;
                        this.e = null;
                        this.f = null;
                        this.i = null;
                        this.j = null;
                        this.l = 4;
                    }
                } else {
                    if (i3 != 4) {
                        throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                    }
                    s38.b(obj);
                }
                return e6a.a;
            }
            long j3 = this.k;
            fVar = this.j;
            bArr = this.i;
            FileOutputStream fileOutputStream2 = this.f;
            closeable = this.e;
            InputStream inputStream = this.d;
            closeableByteStream = this.c;
            dw7.g gVar3 = this.b;
            File file2 = this.a;
            l43Var2 = (l43) this.m;
            try {
                s38.b(obj);
                fileOutputStream = fileOutputStream2;
                l43Var = l43Var2;
                file = file2;
                ?? r102 = inputStream;
                char c = 2;
                closeable2 = closeableByteStream;
                long j4 = j3;
                gVar = gVar3;
                i4 = 0;
                j = j4;
                r10 = r102;
                try {
                    i = r10.read(bArr);
                    fVar.a = i;
                    if (i != -1) {
                        try {
                            try {
                                try {
                                    fileOutputStream.write(bArr, i4, i);
                                    FileOutputStream fileOutputStream3 = fileOutputStream;
                                    gVar.a = j2;
                                    if (j > 0) {
                                        try {
                                            i2 = (int) ((((long) 100) * j2) / j);
                                        } catch (Throwable th3) {
                                            th2 = th3;
                                            l43Var2 = l43Var;
                                            closeableByteStream = closeable2;
                                            closeable = closeable3;
                                            try {
                                                throw th2;
                                            } catch (Throwable th4) {
                                                try {
                                                    f21.a(closeable, th2);
                                                    throw th4;
                                                } catch (Throwable th5) {
                                                    th = th5;
                                                    l43Var = l43Var2;
                                                    try {
                                                        throw th;
                                                    } catch (Throwable th6) {
                                                        f21.a(closeableByteStream, th);
                                                        throw th6;
                                                    }
                                                }
                                            }
                                        }
                                    } else {
                                        i2 = -1;
                                    }
                                    ?? r9 = r10;
                                    a.b bVar = new a.b(i2, j2, j);
                                    this.m = l43Var;
                                    this.a = file;
                                    this.b = gVar2;
                                    this.c = closeable4;
                                    this.d = r9;
                                    this.e = closeable5;
                                    this.f = fileOutputStream3;
                                    this.i = bArr;
                                    this.j = fVar;
                                    this.k = j;
                                    c = 2;
                                    this.l = 2;
                                    if (l43Var.emit(bVar, this) != ck1Var) {
                                        closeable2 = closeable4;
                                        fileOutputStream = fileOutputStream3;
                                        r102 = r9;
                                        j4 = j;
                                        gVar3 = gVar2;
                                        closeable = closeable5;
                                        gVar = gVar3;
                                        i4 = 0;
                                        j = j4;
                                        r10 = r102;
                                        i = r10.read(bArr);
                                        fVar.a = i;
                                        if (i != -1) {
                                            Closeable closeable6 = closeable2;
                                            try {
                                                f21.a(closeable, null);
                                                f21.a(closeable6, null);
                                                file.getAbsolutePath();
                                                a.C0126a c0126a = new a.C0126a(file);
                                                this.m = l43Var;
                                                this.a = null;
                                                this.b = null;
                                                this.c = null;
                                                this.d = null;
                                                this.e = null;
                                                this.f = null;
                                                this.i = null;
                                                this.j = null;
                                                this.l = 3;
                                            } catch (Throwable th7) {
                                                th = th7;
                                                closeableByteStream = closeable6;
                                                throw th;
                                            }
                                        }
                                    }
                                } catch (Throwable th8) {
                                    th = th8;
                                    closeable5 = closeable3;
                                    th2 = th;
                                    l43Var2 = l43Var;
                                    closeableByteStream = closeable4;
                                    closeable = closeable5;
                                    throw th2;
                                }
                                gVar2 = gVar;
                                closeable4 = closeable2;
                            } catch (Throwable th9) {
                                th = th9;
                                closeable4 = closeable2;
                            }
                            closeable3 = closeable;
                            j2 = gVar.a + ((long) fVar.a);
                        } catch (Throwable th10) {
                            th = th10;
                            th2 = th;
                            l43Var2 = l43Var;
                            closeableByteStream = closeable4;
                            closeable = closeable5;
                            throw th2;
                        }
                        closeable5 = closeable3;
                    }
                    return ck1Var;
                } catch (Throwable th11) {
                    th2 = th11;
                    l43Var2 = l43Var;
                    closeableByteStream = closeable2;
                }
            } catch (Throwable th12) {
                th2 = th12;
                throw th2;
            }
        }
        File file3 = new File(updateDownloadManager.a.getExternalFilesDir(null), "updates");
        if (!file3.exists()) {
            file3.mkdirs();
        }
        File[] fileArrListFiles = file3.listFiles();
        if (fileArrListFiles != null) {
            ArrayList arrayList = new ArrayList();
            for (File file4 : fileArrListFiles) {
                cr4.d(file4);
                String name = file4.getName();
                cr4.f(name, "getName(...)");
                if (n89.aw('.', name, "").equals("apk")) {
                    arrayList.add(file4);
                }
            }
            Iterator it = arrayList.iterator();
            while (it.hasNext()) {
                ((File) it.next()).delete();
            }
        }
        file = new File(file3, "STRMR-" + updateInfo.getVersion() + ".apk");
        String apkUrl = updateInfo.getApkUrl();
        if (apkUrl == null) {
            throw new IllegalArgumentException("No APK URL provided for update");
        }
        file.getAbsolutePath();
        Response responseExecute = updateDownloadManager.b.newCall(new Request.Builder().url(apkUrl).header(RtspHeaders.USER_AGENT, "STRMR-App/1.1.72").build()).execute();
        if (!responseExecute.isSuccessful()) {
            throw new Exception("Download failed: HTTP " + responseExecute.code());
        }
        ResponseBody responseBodyBody = responseExecute.body();
        if (responseBodyBody == null) {
            throw new Exception("Empty response body");
        }
        long jContentLength = responseBodyBody.getContentLength();
        dw7.g gVar4 = new dw7.g();
        closeableByteStream = responseBodyBody.byteStream();
        try {
            fileOutputStream = new FileOutputStream(file);
            try {
                gVar = gVar4;
                bArr = new byte[8192];
                fVar = new dw7.f();
                closeable2 = closeableByteStream;
                j = jContentLength;
                r10 = closeable2;
                closeable = fileOutputStream;
                i = r10.read(bArr);
                fVar.a = i;
                if (i != -1) {
                }
                return ck1Var;
            } catch (Throwable th13) {
                th2 = th13;
                l43Var2 = l43Var;
                closeable = fileOutputStream;
                throw th2;
            }
        } catch (Throwable th14) {
            th = th14;
            throw th;
        }
    }
}
