package com.strmr.ps.data.subtitle;

import android.content.Context;
import android.util.Log;
import androidx.credentials.CredentialOption;
import androidx.credentials.exceptions.publickeycredential.DomExceptionUtils;
import androidx.media3.container.NalUnitUtil;
import androidx.media3.exoplayer.ExoPlayer;
import androidx.media3.exoplayer.upstream.CmcdData;
import androidx.media3.extractor.text.ttml.TtmlNode;
import com.strmr.ps.data.remote.api.OpenSubtitlesApiService;
import com.strmr.ps.data.remote.api.OpenSubtitlesAttributes;
import com.strmr.ps.data.remote.api.OpenSubtitlesDownloadRequest;
import com.strmr.ps.data.remote.api.OpenSubtitlesDownloadResponse;
import com.strmr.ps.data.remote.api.OpenSubtitlesFile;
import com.strmr.ps.data.remote.api.OpenSubtitlesSubtitle;
import com.strmr.ps.data.remote.api.OpenSubtitlesUploader;
import dagger.hilt.android.qualifiers.ApplicationContext;
import defpackage.ah;
import defpackage.bz0;
import defpackage.ck1;
import defpackage.cr4;
import defpackage.ez2;
import defpackage.h32;
import defpackage.j65;
import defpackage.kp6;
import defpackage.m02;
import defpackage.m12;
import defpackage.n41;
import defpackage.n89;
import defpackage.pw7;
import defpackage.q07;
import defpackage.q38;
import defpackage.s38;
import defpackage.sb9;
import defpackage.sc2;
import defpackage.v65;
import defpackage.vo0;
import defpackage.vx8;
import defpackage.wj0;
import defpackage.wo5;
import defpackage.y41;
import defpackage.yw7;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Locale;
import javax.inject.Inject;
import javax.inject.Singleton;
import kotlin.Metadata;
import kotlin.coroutines.jvm.internal.ContinuationImpl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import retrofit2.HttpException;

/* JADX INFO: loaded from: classes3.dex */
@Singleton
@Metadata(d1 = {"\u0000\u001c\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0005\b\u0007\u0018\u00002\u00020\u0001:\u0001\nB#\b\u0007\u0012\u0006\u0010\u0003\u001a\u00020\u0002\u0012\u0006\u0010\u0005\u001a\u00020\u0004\u0012\b\b\u0001\u0010\u0007\u001a\u00020\u0006¢\u0006\u0004\b\b\u0010\t¨\u0006\u000b"}, d2 = {"Lcom/strmr/ps/data/subtitle/OpenSubtitlesProvider;", "", "Lcom/strmr/ps/data/remote/api/OpenSubtitlesApiService;", "apiService", "Lokhttp3/OkHttpClient;", "okHttpClient", "Landroid/content/Context;", "context", "<init>", "(Lcom/strmr/ps/data/remote/api/OpenSubtitlesApiService;Lokhttp3/OkHttpClient;Landroid/content/Context;)V", CmcdData.OBJECT_TYPE_AUDIO_ONLY, "app_sideloadRelease"}, k = 1, mv = {2, 0, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
@vx8
public final class OpenSubtitlesProvider {
    public static final Object e = wo5.j(new q07("en", "English"), new q07("es", "Spanish"), new q07("fr", "French"), new q07("de", "German"), new q07("it", "Italian"), new q07("pt", "Portuguese"), new q07("nl", "Dutch"), new q07("pl", "Polish"), new q07("ru", "Russian"), new q07("ja", "Japanese"), new q07("ko", "Korean"), new q07("zh", "Chinese"), new q07("ar", "Arabic"), new q07("he", "Hebrew"), new q07("tr", "Turkish"), new q07("sv", "Swedish"), new q07("no", "Norwegian"), new q07("da", "Danish"), new q07("fi", "Finnish"), new q07("cs", "Czech"), new q07("hu", "Hungarian"), new q07("ro", "Romanian"), new q07("el", "Greek"), new q07("th", "Thai"), new q07("vi", "Vietnamese"), new q07(TtmlNode.ATTR_ID, "Indonesian"), new q07("ms", "Malay"));
    public final OpenSubtitlesApiService a;
    public final OkHttpClient b;
    public final Context c;
    public final j65 d;

    @Metadata(d1 = {"\u0000 \n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\u0010\u000e\n\u0002\b\u0003\n\u0002\u0010\b\n\u0002\b\u0002\n\u0002\u0010$\n\u0002\b\u0003\b\u0086\u0003\u0018\u00002\u00020\u0001R\u0014\u0010\u0003\u001a\u00020\u00028\u0002X\u0082T¢\u0006\u0006\n\u0004\b\u0003\u0010\u0004R\u0014\u0010\u0005\u001a\u00020\u00028\u0002X\u0082T¢\u0006\u0006\n\u0004\b\u0005\u0010\u0004R\u0014\u0010\u0007\u001a\u00020\u00068\u0002X\u0082T¢\u0006\u0006\n\u0004\b\u0007\u0010\bR \u0010\n\u001a\u000e\u0012\u0004\u0012\u00020\u0002\u0012\u0004\u0012\u00020\u00020\t8\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b\n\u0010\u000b¨\u0006\f"}, d2 = {"Lcom/strmr/ps/data/subtitle/OpenSubtitlesProvider$a;", "", "", "TAG", "Ljava/lang/String;", "SUBTITLE_CACHE_DIR", "", "MAX_RESULTS", "I", "", "LANGUAGE_NAMES", "Ljava/util/Map;", "app_sideloadRelease"}, k = 1, mv = {2, 0, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
    public static final class a {
    }

    @Inject
    public OpenSubtitlesProvider(@kp6 OpenSubtitlesApiService openSubtitlesApiService, @kp6 OkHttpClient okHttpClient, @ApplicationContext @kp6 Context context) {
        cr4.g(openSubtitlesApiService, "apiService");
        cr4.g(okHttpClient, "okHttpClient");
        cr4.g(context, "context");
        this.a = openSubtitlesApiService;
        this.b = okHttpClient;
        this.c = context;
        this.d = v65.a(new ah(this, 25));
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:36:0x00ba  */
    /* JADX WARN: Removed duplicated region for block: B:39:0x00c8  */
    /* JADX WARN: Removed duplicated region for block: B:40:0x00cb A[Catch: Exception -> 0x0065, HttpException -> 0x006a, TryCatch #6 {HttpException -> 0x006a, Exception -> 0x0065, blocks: (B:37:0x00c0, B:40:0x00cb, B:41:0x00d2, B:18:0x0060), top: B:81:0x0060 }] */
    /* JADX WARN: Removed duplicated region for block: B:55:0x00f7  */
    /* JADX WARN: Removed duplicated region for block: B:59:0x0130  */
    /* JADX WARN: Removed duplicated region for block: B:68:0x018d  */
    /* JADX WARN: Removed duplicated region for block: B:70:0x0194  */
    /* JADX WARN: Removed duplicated region for block: B:71:0x019b  */
    /* JADX WARN: Removed duplicated region for block: B:75:0x01a2  */
    /* JADX WARN: Removed duplicated region for block: B:7:0x0017  */
    /* JADX WARN: Removed duplicated region for block: B:86:? A[RETURN, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:87:? A[RETURN, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:90:? A[RETURN, SYNTHETIC] */
    /* JADX WARN: Type inference failed for: r11v10 */
    /* JADX WARN: Type inference failed for: r11v12, types: [java.lang.Exception] */
    /* JADX WARN: Type inference failed for: r11v14 */
    /* JADX WARN: Type inference failed for: r11v15 */
    /* JADX WARN: Type inference failed for: r11v3, types: [java.lang.Exception, java.lang.Throwable, retrofit2.HttpException] */
    /* JADX WARN: Type inference failed for: r11v8 */
    /* JADX WARN: Type inference failed for: r6v0 */
    /* JADX WARN: Type inference failed for: r6v1, types: [java.lang.Throwable] */
    /* JADX WARN: Type inference failed for: r6v3 */
    /* JADX WARN: Unsupported multi-entry loop pattern (BACK_EDGE: B:56:0x0128 -> B:58:0x012b). Please report as a decompilation issue!!! */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static final Object a(OpenSubtitlesProvider openSubtitlesProvider, int i, int i2, ContinuationImpl continuationImpl) throws Exception {
        d dVar;
        OpenSubtitlesProvider openSubtitlesProvider2;
        int i3;
        int i4;
        d dVar2;
        int i5;
        int i6;
        OpenSubtitlesProvider openSubtitlesProvider3;
        int i7;
        int i8;
        int i9;
        Exception e2;
        OpenSubtitlesProvider openSubtitlesProvider4;
        HttpException e3;
        ?? r11;
        long j;
        String link;
        d dVar3;
        int i10;
        if (continuationImpl instanceof d) {
            dVar = (d) continuationImpl;
            int i11 = dVar.j;
            if ((i11 & Integer.MIN_VALUE) != 0) {
                dVar.j = i11 - Integer.MIN_VALUE;
                openSubtitlesProvider2 = openSubtitlesProvider;
            } else {
                openSubtitlesProvider2 = openSubtitlesProvider;
                dVar = new d(openSubtitlesProvider2, continuationImpl);
            }
        }
        Object downloadLink$default = dVar.f;
        ck1 ck1Var = ck1.a;
        int i12 = dVar.j;
        ?? r6 = 0;
        if (i12 == 0) {
            s38.b(downloadLink$default);
            i3 = i2;
            if (1 <= i3) {
                i4 = i;
                dVar2 = dVar;
                i5 = 1;
                i6 = i3;
                OpenSubtitlesApiService openSubtitlesApiService = openSubtitlesProvider2.a;
                i10 = i5;
                OpenSubtitlesDownloadRequest openSubtitlesDownloadRequest = new OpenSubtitlesDownloadRequest(i4, "srt", null, null, null, null, null, 124, null);
                dVar2.a = openSubtitlesProvider2;
                dVar2.b = null;
                dVar2.c = i4;
                dVar2.d = i6;
                dVar2.e = i10;
                dVar2.j = 1;
                dVar3 = dVar2;
                downloadLink$default = OpenSubtitlesApiService.DefaultImpls.getDownloadLink$default(openSubtitlesApiService, "nASXPgmRph0A2K6guV1WMAmXvludLeAQ", null, null, openSubtitlesDownloadRequest, dVar3, 6, null);
                if (downloadLink$default != ck1Var) {
                }
            }
            if (r6 == 0) {
            }
        } else if (i12 == 1) {
            i9 = dVar.e;
            i8 = dVar.d;
            i7 = dVar.c;
            openSubtitlesProvider4 = dVar.a;
            try {
                s38.b(downloadLink$default);
            } catch (HttpException e4) {
                e3 = e4;
                openSubtitlesProvider3 = openSubtitlesProvider4;
                r11 = e3;
                if ((r11.code() == 503 && r11.code() != 429) || i9 >= i8) {
                    throw r11;
                }
                j = ((long) i9) * ExoPlayer.DEFAULT_DETACH_SURFACE_TIMEOUT_MS;
                Log.w("OpenSubtitlesProvider", "Got " + r11.code() + " error, retrying in " + j + "ms (attempt " + i9 + DomExceptionUtils.SEPARATOR + i8 + ")");
                dVar.a = openSubtitlesProvider3;
                dVar.b = r11;
                dVar.c = i7;
                dVar.d = i8;
                dVar.e = i9;
                dVar.j = 2;
                if (h32.a(j, dVar) != ck1Var) {
                    return ck1Var;
                }
                int i13 = i8;
                dVar2 = dVar;
                i6 = i13;
                if (i9 != i6) {
                }
            } catch (Exception e5) {
                e2 = e5;
                openSubtitlesProvider3 = openSubtitlesProvider4;
                Exception exc = e2;
                if (i9 < i8) {
                    throw exc;
                }
                long j2 = ((long) i9) * 1000;
                Log.w("OpenSubtitlesProvider", "Download error, retrying in " + j2 + "ms: " + exc.getMessage());
                dVar.a = openSubtitlesProvider3;
                dVar.b = exc;
                dVar.c = i7;
                dVar.d = i8;
                dVar.e = i9;
                dVar.j = 3;
                r11 = exc;
                if (h32.a(j2, dVar) == ck1Var) {
                    return ck1Var;
                }
                int i14 = i8;
                dVar2 = dVar;
                i6 = i14;
                if (i9 != i6) {
                }
            }
            link = ((OpenSubtitlesDownloadResponse) downloadLink$default).getLink();
            if (link == null) {
            }
        } else {
            if (i12 != 2) {
                if (i12 != 3) {
                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                }
                i9 = dVar.e;
                i8 = dVar.d;
                i7 = dVar.c;
                Exception exc2 = dVar.b;
                openSubtitlesProvider3 = dVar.a;
                s38.b(downloadLink$default);
                r11 = exc2;
                int i142 = i8;
                dVar2 = dVar;
                i6 = i142;
                if (i9 != i6) {
                    i3 = i6;
                    r6 = r11;
                    if (r6 == 0) {
                        throw r6;
                    }
                    throw new IllegalStateException(n41.i(i3, "Failed to get download link after ", " attempts"));
                }
                int i15 = i9 + 1;
                i4 = i7;
                openSubtitlesProvider2 = openSubtitlesProvider3;
                i5 = i15;
                try {
                } catch (HttpException e6) {
                    e3 = e6;
                    dVar3 = dVar2;
                    i10 = i5;
                } catch (Exception e7) {
                    e2 = e7;
                    dVar3 = dVar2;
                    i10 = i5;
                }
                OpenSubtitlesApiService openSubtitlesApiService2 = openSubtitlesProvider2.a;
                i10 = i5;
                try {
                } catch (HttpException e8) {
                    e3 = e8;
                    dVar3 = dVar2;
                } catch (Exception e9) {
                    e2 = e9;
                    dVar3 = dVar2;
                }
                OpenSubtitlesDownloadRequest openSubtitlesDownloadRequest2 = new OpenSubtitlesDownloadRequest(i4, "srt", null, null, null, null, null, 124, null);
                dVar2.a = openSubtitlesProvider2;
                dVar2.b = null;
                dVar2.c = i4;
                dVar2.d = i6;
                dVar2.e = i10;
                dVar2.j = 1;
                dVar3 = dVar2;
                try {
                } catch (HttpException e10) {
                    e3 = e10;
                    i8 = i6;
                    openSubtitlesProvider3 = openSubtitlesProvider2;
                    i9 = i10;
                    i7 = i4;
                    dVar = dVar3;
                    r11 = e3;
                    if (r11.code() == 503) {
                    }
                    j = ((long) i9) * ExoPlayer.DEFAULT_DETACH_SURFACE_TIMEOUT_MS;
                    Log.w("OpenSubtitlesProvider", "Got " + r11.code() + " error, retrying in " + j + "ms (attempt " + i9 + DomExceptionUtils.SEPARATOR + i8 + ")");
                    dVar.a = openSubtitlesProvider3;
                    dVar.b = r11;
                    dVar.c = i7;
                    dVar.d = i8;
                    dVar.e = i9;
                    dVar.j = 2;
                    if (h32.a(j, dVar) != ck1Var) {
                    }
                } catch (Exception e11) {
                    e2 = e11;
                    i8 = i6;
                    openSubtitlesProvider3 = openSubtitlesProvider2;
                    i9 = i10;
                    i7 = i4;
                    dVar = dVar3;
                    Exception exc3 = e2;
                    if (i9 < i8) {
                    }
                }
                downloadLink$default = OpenSubtitlesApiService.DefaultImpls.getDownloadLink$default(openSubtitlesApiService2, "nASXPgmRph0A2K6guV1WMAmXvludLeAQ", null, null, openSubtitlesDownloadRequest2, dVar3, 6, null);
                if (downloadLink$default != ck1Var) {
                    return ck1Var;
                }
                i8 = i6;
                i7 = i4;
                dVar = dVar3;
                openSubtitlesProvider4 = openSubtitlesProvider2;
                i9 = i10;
                link = ((OpenSubtitlesDownloadResponse) downloadLink$default).getLink();
                if (link == null) {
                    return link;
                }
                throw new IllegalStateException("No download link in response");
            }
            i9 = dVar.e;
            int i16 = dVar.d;
            i7 = dVar.c;
            r11 = dVar.b;
            openSubtitlesProvider3 = dVar.a;
            s38.b(downloadLink$default);
            dVar2 = dVar;
            i6 = i16;
            if (i9 != i6) {
            }
        }
    }

    /* JADX WARN: Type inference failed for: r9v1, types: [java.lang.Object, java.util.Map] */
    public static final sb9 b(OpenSubtitlesProvider openSubtitlesProvider, OpenSubtitlesSubtitle openSubtitlesSubtitle) {
        List<OpenSubtitlesFile> files;
        OpenSubtitlesFile openSubtitlesFile;
        Integer file_id;
        openSubtitlesProvider.getClass();
        OpenSubtitlesAttributes attributes = openSubtitlesSubtitle.getAttributes();
        if (attributes == null || (files = attributes.getFiles()) == null || (openSubtitlesFile = (OpenSubtitlesFile) y41.ab(files)) == null || (file_id = openSubtitlesFile.getFile_id()) == null) {
            return null;
        }
        String strValueOf = String.valueOf(file_id.intValue());
        String file_name = openSubtitlesFile.getFile_name();
        if (file_name == null && (file_name = attributes.getRelease()) == null) {
            file_name = "Unknown";
        }
        String str = file_name;
        String language = attributes.getLanguage();
        if (language == null) {
            language = "en";
        }
        Locale locale = Locale.ROOT;
        String lowerCase = language.toLowerCase(locale);
        cr4.f(lowerCase, "toLowerCase(...)");
        String upperCase = (String) e.get(lowerCase);
        if (upperCase == null) {
            upperCase = language.toUpperCase(locale);
            cr4.f(upperCase, "toUpperCase(...)");
        }
        String str2 = upperCase;
        String language2 = attributes.getLanguage();
        String str3 = language2 == null ? "en" : language2;
        String url = attributes.getUrl();
        if (url == null) {
            url = "";
        }
        String str4 = url;
        Float ratings = attributes.getRatings();
        Integer download_count = attributes.getDownload_count();
        Boolean moviehash_match = attributes.getMoviehash_match();
        Boolean bool = Boolean.TRUE;
        boolean zB = cr4.b(moviehash_match, bool);
        boolean zB2 = cr4.b(attributes.getAi_translated(), bool);
        boolean zB3 = cr4.b(attributes.getMachine_translated(), bool);
        Float fps = attributes.getFps();
        boolean zB4 = cr4.b(attributes.getHearing_impaired(), bool);
        boolean zB5 = cr4.b(attributes.getForeign_parts_only(), bool);
        String upload_date = attributes.getUpload_date();
        OpenSubtitlesUploader uploader = attributes.getUploader();
        return new sb9(strValueOf, str, str2, str3, str4, ratings, download_count, zB, zB2, zB3, "srt", fps, zB4, zB5, upload_date, uploader != null ? uploader.getName() : null);
    }

    public static boolean f(File file) {
        if (!file.exists()) {
            return false;
        }
        if (file.length() < 50) {
            Log.w("OpenSubtitlesProvider", "Subtitle file too small: " + file.length() + " bytes");
            return false;
        }
        try {
            String strBb = n89.bb(CredentialOption.PRIORITY_OIDC_OR_SIMILAR, ez2.b(file, bz0.a));
            boolean z = true;
            boolean z2 = n89.l(strBb, "-->", false) || new pw7(".*\\d+\\s*\\n\\d{2}:\\d{2}:\\d{2}.*", yw7.c).d(strBb);
            boolean z3 = n89.l(strBb, "WEBVTT", false) || n89.l(strBb, "-->", false);
            boolean z4 = n89.l(strBb, "[Script Info]", false) || n89.l(strBb, "Dialogue:", false);
            boolean z5 = n89.l(strBb, "<tt", false) || n89.l(strBb, "<p begin=", false);
            if (!z2 && !z3 && !z4 && !z5) {
                z = false;
            }
            if (!z) {
                Log.w("OpenSubtitlesProvider", "Subtitle file doesn't look like valid subtitle content");
            }
            return z;
        } catch (Exception e2) {
            Log.e("OpenSubtitlesProvider", "Error validating subtitle file", e2);
            return false;
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:7:0x0013  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object c(sb9 sb9Var, ContinuationImpl continuationImpl) {
        com.strmr.ps.data.subtitle.a aVar;
        if (continuationImpl instanceof com.strmr.ps.data.subtitle.a) {
            aVar = (com.strmr.ps.data.subtitle.a) continuationImpl;
            int i = aVar.c;
            if ((i & Integer.MIN_VALUE) != 0) {
                aVar.c = i - Integer.MIN_VALUE;
            } else {
                aVar = new com.strmr.ps.data.subtitle.a(this, continuationImpl);
            }
        }
        Object objF = aVar.a;
        ck1 ck1Var = ck1.a;
        int i2 = aVar.c;
        if (i2 == 0) {
            s38.b(objF);
            m12 m12Var = sc2.a;
            m02 m02Var = m02.c;
            b bVar = new b(this, sb9Var, null);
            aVar.c = 1;
            objF = wj0.f(m02Var, bVar, aVar);
            if (objF == ck1Var) {
                return ck1Var;
            }
        } else {
            if (i2 != 1) {
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
            }
            s38.b(objF);
        }
        return ((q38) objF).a;
    }

    /* JADX WARN: Removed duplicated region for block: B:20:0x0062 A[Catch: all -> 0x0096, TryCatch #1 {all -> 0x0096, blocks: (B:18:0x005c, B:20:0x0062, B:22:0x0068, B:27:0x0079, B:47:0x00a3, B:48:0x00a6, B:49:0x00a7, B:50:0x00ae, B:51:0x00af, B:52:0x00c9, B:45:0x00a1, B:24:0x006e, B:26:0x0076, B:43:0x009d, B:44:0x00a0), top: B:73:0x005c, outer: #2, inners: #0, #3 }] */
    /* JADX WARN: Removed duplicated region for block: B:51:0x00af A[Catch: all -> 0x0096, TryCatch #1 {all -> 0x0096, blocks: (B:18:0x005c, B:20:0x0062, B:22:0x0068, B:27:0x0079, B:47:0x00a3, B:48:0x00a6, B:49:0x00a7, B:50:0x00ae, B:51:0x00af, B:52:0x00c9, B:45:0x00a1, B:24:0x006e, B:26:0x0076, B:43:0x009d, B:44:0x00a0), top: B:73:0x005c, outer: #2, inners: #0, #3 }] */
    /* JADX WARN: Removed duplicated region for block: B:58:0x00d2  */
    /* JADX WARN: Removed duplicated region for block: B:64:0x012b  */
    /* JADX WARN: Removed duplicated region for block: B:65:0x012f  */
    /* JADX WARN: Removed duplicated region for block: B:69:0x0135  */
    /* JADX WARN: Removed duplicated region for block: B:7:0x0013  */
    /* JADX WARN: Removed duplicated region for block: B:82:? A[RETURN, SYNTHETIC] */
    /* JADX WARN: Unsupported multi-entry loop pattern (BACK_EDGE: B:57:0x00d0 -> B:63:0x0129). Please report as a decompilation issue!!! */
    /* JADX WARN: Unsupported multi-entry loop pattern (BACK_EDGE: B:61:0x011a -> B:62:0x0120). Please report as a decompilation issue!!! */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object d(String str, File file, int i, ContinuationImpl continuationImpl) throws Exception {
        c cVar;
        Exception e2;
        OpenSubtitlesProvider openSubtitlesProvider;
        int i2;
        File file2;
        int i3;
        Response responseExecute;
        InputStream inputStreamByteStream;
        if (continuationImpl instanceof c) {
            cVar = (c) continuationImpl;
            int i4 = cVar.k;
            if ((i4 & Integer.MIN_VALUE) != 0) {
                cVar.k = i4 - Integer.MIN_VALUE;
            } else {
                cVar = new c(this, continuationImpl);
            }
        }
        Object obj = cVar.i;
        ck1 ck1Var = ck1.a;
        int i5 = cVar.k;
        if (i5 == 0) {
            s38.b(obj);
            if (1 <= i) {
                openSubtitlesProvider = this;
                i2 = i;
                file2 = file;
                i3 = 1;
                responseExecute = openSubtitlesProvider.b.newCall(new Request.Builder().url(str).build()).execute();
                if (responseExecute.isSuccessful()) {
                }
                e2 = e;
                if (i3 < i2) {
                }
                if (i3 != i2) {
                }
            } else {
                e2 = null;
                if (e2 == null) {
                }
            }
        } else {
            if (i5 != 1) {
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
            }
            int i6 = cVar.f;
            int i7 = cVar.e;
            Exception exc = cVar.d;
            File file3 = cVar.c;
            String str2 = cVar.b;
            openSubtitlesProvider = cVar.a;
            s38.b(obj);
            file3.delete();
            File file4 = file3;
            e2 = exc;
            file2 = file4;
            i2 = i7;
            i3 = i6;
            str = str2;
            if (i3 != i2) {
                i3++;
                try {
                } catch (Exception e3) {
                    e2 = e3;
                    if (i3 < i2) {
                    }
                }
                responseExecute = openSubtitlesProvider.b.newCall(new Request.Builder().url(str).build()).execute();
                try {
                    if (responseExecute.isSuccessful()) {
                        throw new IOException("Download failed with code " + responseExecute.code());
                    }
                    ResponseBody responseBodyBody = responseExecute.body();
                    if (responseBodyBody == null || (inputStreamByteStream = responseBodyBody.byteStream()) == null) {
                        throw new IOException("Empty response body");
                    }
                    try {
                        FileOutputStream fileOutputStream = new FileOutputStream(file2);
                        try {
                            vo0.a(inputStreamByteStream, fileOutputStream);
                            fileOutputStream.close();
                            inputStreamByteStream.close();
                            responseExecute.close();
                            if (f(file2)) {
                                file2.getAbsolutePath();
                                return file2;
                            }
                            file2.delete();
                            throw new IOException("Downloaded file is invalid or empty");
                        } finally {
                        }
                    } finally {
                    }
                } finally {
                }
                e2 = e3;
                if (i3 < i2) {
                    long j = ((long) i3) * 1000;
                    Log.w("OpenSubtitlesProvider", "Download failed, retrying in " + j + "ms (attempt " + i3 + DomExceptionUtils.SEPARATOR + i2 + "): " + e2.getMessage());
                    cVar.a = openSubtitlesProvider;
                    cVar.b = str;
                    cVar.c = file2;
                    cVar.d = e2;
                    cVar.e = i2;
                    cVar.f = i3;
                    cVar.k = 1;
                    if (h32.a(j, cVar) == ck1Var) {
                        return ck1Var;
                    }
                    file3 = file2;
                    exc = e2;
                    str2 = str;
                    i6 = i3;
                    i7 = i2;
                    file3.delete();
                    File file42 = file3;
                    e2 = exc;
                    file2 = file42;
                    i2 = i7;
                    i3 = i6;
                    str = str2;
                }
                if (i3 != i2) {
                    i = i2;
                    if (e2 == null) {
                        throw e2;
                    }
                    throw new IOException(n41.i(i, "Download failed after ", " attempts"));
                }
            }
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:7:0x0013  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object e(u uVar, ContinuationImpl continuationImpl) {
        e eVar;
        if (continuationImpl instanceof e) {
            eVar = (e) continuationImpl;
            int i = eVar.c;
            if ((i & Integer.MIN_VALUE) != 0) {
                eVar.c = i - Integer.MIN_VALUE;
            } else {
                eVar = new e(this, continuationImpl);
            }
        }
        Object objF = eVar.a;
        ck1 ck1Var = ck1.a;
        int i2 = eVar.c;
        if (i2 == 0) {
            s38.b(objF);
            m12 m12Var = sc2.a;
            m02 m02Var = m02.c;
            f fVar = new f(this, uVar, null);
            eVar.c = 1;
            objF = wj0.f(m02Var, fVar, eVar);
            if (objF == ck1Var) {
                return ck1Var;
            }
        } else {
            if (i2 != 1) {
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
            }
            s38.b(objF);
        }
        return ((q38) objF).a;
    }
}
