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.upstream.CmcdData;
import com.strmr.ps.data.remote.api.StremioSubtitle;
import com.strmr.ps.data.remote.api.StremioSubtitleService;
import dagger.hilt.android.qualifiers.ApplicationContext;
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.q38;
import defpackage.s38;
import defpackage.sb9;
import defpackage.sc2;
import defpackage.v65;
import defpackage.vo0;
import defpackage.vx8;
import defpackage.wg8;
import defpackage.wj0;
import defpackage.yw7;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLDecoder;
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;

/* 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/StremioSubtitleProvider;", "", "Lcom/strmr/ps/data/remote/api/StremioSubtitleService;", "apiService", "Lokhttp3/OkHttpClient;", "okHttpClient", "Landroid/content/Context;", "context", "<init>", "(Lcom/strmr/ps/data/remote/api/StremioSubtitleService;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 StremioSubtitleProvider {
    public final StremioSubtitleService a;
    public final OkHttpClient b;
    public final Context c;
    public final j65 d;

    @Metadata(d1 = {"\u0000\u0018\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\u0010\u000e\n\u0002\b\u0003\n\u0002\u0010\b\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\b¨\u0006\t"}, d2 = {"Lcom/strmr/ps/data/subtitle/StremioSubtitleProvider$a;", "", "", "TAG", "Ljava/lang/String;", "SUBTITLE_CACHE_DIR", "", "MAX_RESULTS", "I", "app_sideloadRelease"}, k = 1, mv = {2, 0, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
    public static final class a {
    }

    @Inject
    public StremioSubtitleProvider(@kp6 StremioSubtitleService stremioSubtitleService, @kp6 OkHttpClient okHttpClient, @ApplicationContext @kp6 Context context) {
        cr4.g(stremioSubtitleService, "apiService");
        cr4.g(okHttpClient, "okHttpClient");
        cr4.g(context, "context");
        this.a = stremioSubtitleService;
        this.b = okHttpClient;
        this.c = context;
        this.d = v65.a(new wg8(this, 3));
    }

    public static final sb9 a(StremioSubtitleProvider stremioSubtitleProvider, StremioSubtitle stremioSubtitle) {
        String lowerCase;
        stremioSubtitleProvider.getClass();
        String url = stremioSubtitle.getUrl();
        if (url == null) {
            return null;
        }
        String id = stremioSubtitle.getId();
        if (id == null) {
            id = String.valueOf(url.hashCode());
        }
        String lowerCase2 = "srt";
        if (!n89.l(url, ".srt", true)) {
            if (n89.l(url, ".vtt", true)) {
                lowerCase2 = "vtt";
            } else if (n89.l(url, ".ass", true)) {
                lowerCase2 = "ass";
            } else if (n89.l(url, ".ssa", true)) {
                lowerCase2 = "ssa";
            } else {
                String subFormat = stremioSubtitle.getSubFormat();
                if (subFormat != null) {
                    lowerCase2 = subFormat.toLowerCase(Locale.ROOT);
                    cr4.f(lowerCase2, "toLowerCase(...)");
                }
            }
        }
        String str = lowerCase2;
        String subFileName = stremioSubtitle.getSubFileName();
        if (subFileName != null) {
            lowerCase = subFileName.toLowerCase(Locale.ROOT);
            cr4.f(lowerCase, "toLowerCase(...)");
        } else {
            lowerCase = "";
        }
        boolean z = n89.l(lowerCase, "forced", false) || n89.l(lowerCase, "foreign", false) || n89.l(lowerCase, "non-english", false) || n89.l(lowerCase, "non english", false) || (!n89.l(lowerCase, "sdh", false) && n89.l(lowerCase, "forced", false));
        boolean z2 = n89.l(lowerCase, ".hi.", false) || n89.l(lowerCase, "[hi]", false) || n89.l(lowerCase, "(hi)", false) || n89.l(lowerCase, "sdh", false) || n89.l(lowerCase, "hearing", false);
        String subFileName2 = stremioSubtitle.getSubFileName();
        if (subFileName2 == null) {
            String url2 = stremioSubtitle.getUrl();
            if (url2 == null) {
                url2 = "";
            }
            String strAz = n89.az(n89.ax(url2, DomExceptionUtils.SEPARATOR), "?");
            subFileName2 = (!n89.l(strAz, ".", false) || strAz.length() <= 4) ? n41.n("Subtitle #", id) : URLDecoder.decode(strAz, "UTF-8");
        }
        cr4.d(subFileName2);
        Integer numBe = null;
        String displayLabel = stremioSubtitle.getDisplayLabel();
        String lang = stremioSubtitle.getLang();
        if (lang == null) {
            lang = "en";
        }
        String subRating = stremioSubtitle.getSubRating();
        Float fBd = subRating != null ? n89.bd(subRating) : null;
        String subDownloadsCnt = stremioSubtitle.getSubDownloadsCnt();
        if (subDownloadsCnt != null) {
            numBe = n89.be(n89.an(subDownloadsCnt, ",", ""));
        }
        return new sb9(id, subFileName2, displayLabel, lang, url, fBd, numBe, false, false, false, str, null, z2, z, null, null);
    }

    public static boolean e(File file) {
        if (!file.exists()) {
            return false;
        }
        if (file.length() < 50) {
            Log.w("StremioSubtitleProvider", "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("StremioSubtitleProvider", "Subtitle file doesn't look like valid subtitle content");
            }
            return z;
        } catch (Exception e) {
            Log.e("StremioSubtitleProvider", "Error validating subtitle file", e);
            return false;
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:7:0x0013  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object b(sb9 sb9Var, ContinuationImpl continuationImpl) {
        g gVar;
        if (continuationImpl instanceof g) {
            gVar = (g) continuationImpl;
            int i = gVar.c;
            if ((i & Integer.MIN_VALUE) != 0) {
                gVar.c = i - Integer.MIN_VALUE;
            } else {
                gVar = new g(this, continuationImpl);
            }
        }
        Object objF = gVar.a;
        ck1 ck1Var = ck1.a;
        int i2 = gVar.c;
        if (i2 == 0) {
            s38.b(objF);
            m12 m12Var = sc2.a;
            m02 m02Var = m02.c;
            h hVar = new h(this, sb9Var, null);
            gVar.c = 1;
            objF = wj0.f(m02Var, hVar, gVar);
            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 c(String str, File file, int i, ContinuationImpl continuationImpl) throws Exception {
        i iVar;
        Exception e;
        StremioSubtitleProvider stremioSubtitleProvider;
        int i2;
        File file2;
        int i3;
        Response responseExecute;
        InputStream inputStreamByteStream;
        if (continuationImpl instanceof i) {
            iVar = (i) continuationImpl;
            int i4 = iVar.k;
            if ((i4 & Integer.MIN_VALUE) != 0) {
                iVar.k = i4 - Integer.MIN_VALUE;
            } else {
                iVar = new i(this, continuationImpl);
            }
        }
        Object obj = iVar.i;
        ck1 ck1Var = ck1.a;
        int i5 = iVar.k;
        if (i5 == 0) {
            s38.b(obj);
            if (1 <= i) {
                stremioSubtitleProvider = this;
                i2 = i;
                file2 = file;
                i3 = 1;
                responseExecute = stremioSubtitleProvider.b.newCall(new Request.Builder().url(str).build()).execute();
                if (responseExecute.isSuccessful()) {
                }
                e = e;
                if (i3 < i2) {
                }
                if (i3 != i2) {
                }
            } else {
                e = null;
                if (e == null) {
                }
            }
        } else {
            if (i5 != 1) {
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
            }
            int i6 = iVar.f;
            int i7 = iVar.e;
            Exception exc = iVar.d;
            File file3 = iVar.c;
            String str2 = iVar.b;
            stremioSubtitleProvider = iVar.a;
            s38.b(obj);
            file3.delete();
            File file4 = file3;
            e = exc;
            file2 = file4;
            i2 = i7;
            i3 = i6;
            str = str2;
            if (i3 != i2) {
                i3++;
                try {
                } catch (Exception e2) {
                    e = e2;
                    if (i3 < i2) {
                    }
                }
                responseExecute = stremioSubtitleProvider.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 (e(file2)) {
                                file2.getAbsolutePath();
                                return file2;
                            }
                            file2.delete();
                            throw new IOException("Downloaded file is invalid or empty");
                        } finally {
                        }
                    } finally {
                    }
                } finally {
                }
                e = e2;
                if (i3 < i2) {
                    long j = ((long) i3) * 1000;
                    Log.w("StremioSubtitleProvider", "Download failed, retrying in " + j + "ms (attempt " + i3 + DomExceptionUtils.SEPARATOR + i2 + "): " + e.getMessage());
                    iVar.a = stremioSubtitleProvider;
                    iVar.b = str;
                    iVar.c = file2;
                    iVar.d = e;
                    iVar.e = i2;
                    iVar.f = i3;
                    iVar.k = 1;
                    if (h32.a(j, iVar) == ck1Var) {
                        return ck1Var;
                    }
                    file3 = file2;
                    exc = e;
                    str2 = str;
                    i6 = i3;
                    i7 = i2;
                    file3.delete();
                    File file42 = file3;
                    e = exc;
                    file2 = file42;
                    i2 = i7;
                    i3 = i6;
                    str = str2;
                }
                if (i3 != i2) {
                    i = i2;
                    if (e == null) {
                        throw e;
                    }
                    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 d(u uVar, ContinuationImpl continuationImpl) {
        j jVar;
        if (continuationImpl instanceof j) {
            jVar = (j) continuationImpl;
            int i = jVar.c;
            if ((i & Integer.MIN_VALUE) != 0) {
                jVar.c = i - Integer.MIN_VALUE;
            } else {
                jVar = new j(this, continuationImpl);
            }
        }
        Object objF = jVar.a;
        ck1 ck1Var = ck1.a;
        int i2 = jVar.c;
        if (i2 == 0) {
            s38.b(objF);
            m12 m12Var = sc2.a;
            m02 m02Var = m02.c;
            k kVar = new k(uVar, this, null);
            jVar.c = 1;
            objF = wj0.f(m02Var, kVar, jVar);
            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;
    }
}
