package com.strmr.ps.ui.player;

import android.net.Uri;
import android.util.Log;
import android.widget.Toast;
import androidx.media3.common.C;
import androidx.media3.common.MediaItem;
import androidx.media3.common.MimeTypes;
import androidx.media3.datasource.DefaultDataSource;
import androidx.media3.exoplayer.ExoPlayer;
import androidx.media3.exoplayer.source.MediaSource;
import androidx.media3.exoplayer.source.MergingMediaSource;
import androidx.media3.exoplayer.source.SingleSampleMediaSource;
import com.strmr.ps.data.subtitle.SubtitleManager;
import com.strmr.ps.data.subtitle.t;
import defpackage.ak1;
import defpackage.bz0;
import defpackage.ck1;
import defpackage.cr4;
import defpackage.dha;
import defpackage.e6a;
import defpackage.ez2;
import defpackage.gr3;
import defpackage.n41;
import defpackage.n89;
import defpackage.q07;
import defpackage.q38;
import defpackage.s38;
import defpackage.sb9;
import defpackage.y41;
import java.io.File;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import kotlin.Metadata;
import kotlin.coroutines.Continuation;
import kotlin.coroutines.jvm.internal.DebugMetadata;
import kotlin.coroutines.jvm.internal.SuspendLambda;

/* JADX INFO: loaded from: classes3.dex */
@Metadata(d1 = {"\u0000\f\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\u0010\u0002\u001a\u00020\u0001*\u00020\u0000H\n¢\u0006\u0004\b\u0002\u0010\u0003"}, d2 = {"Lak1;", "Le6a;", "<anonymous>", "(Lak1;)V"}, k = 3, mv = {2, 0, 0})
@DebugMetadata(c = "com.strmr.ps.ui.player.VideoPlayerActivity$applyExternalSubtitle$1", f = "VideoPlayerActivity.kt", i = {1}, l = {5733, 5736}, m = "invokeSuspend", n = {"downloadedFile"}, s = {"L$6"})
final class m extends SuspendLambda implements gr3<ak1, Continuation<? super e6a>, Object> {
    public Object a;
    public VideoPlayerActivity b;
    public sb9 c;
    public DefaultDataSource.Factory d;
    public MediaSource e;
    public ExoPlayer f;
    public File i;
    public int j;
    public final /* synthetic */ VideoPlayerActivity k;
    public final /* synthetic */ sb9 l;
    public final /* synthetic */ t.b.a m;
    public final /* synthetic */ DefaultDataSource.Factory n;
    public final /* synthetic */ MediaSource o;
    public final /* synthetic */ ExoPlayer p;

    /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
    public m(VideoPlayerActivity videoPlayerActivity, sb9 sb9Var, t.b.a aVar, DefaultDataSource.Factory factory, MediaSource mediaSource, ExoPlayer exoPlayer, Continuation continuation) {
        super(2, continuation);
        this.k = videoPlayerActivity;
        this.l = sb9Var;
        this.m = aVar;
        this.n = factory;
        this.o = mediaSource;
        this.p = exoPlayer;
    }

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

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

    /* JADX WARN: Removed duplicated region for block: B:22:0x0094  */
    /* JADX WARN: Removed duplicated region for block: B:23:0x0096  */
    /* JADX WARN: Removed duplicated region for block: B:27:0x00b1 A[Catch: Exception -> 0x00c0, TryCatch #0 {Exception -> 0x00c0, blocks: (B:25:0x00a5, B:27:0x00b1, B:53:0x0110, B:30:0x00c2, B:32:0x00c8, B:34:0x00d2, B:37:0x00dd, B:38:0x00e2, B:40:0x00e8, B:42:0x00fc, B:45:0x0101, B:46:0x0105), top: B:100:0x00a5 }] */
    /* JADX WARN: Removed duplicated region for block: B:30:0x00c2 A[Catch: Exception -> 0x00c0, TryCatch #0 {Exception -> 0x00c0, blocks: (B:25:0x00a5, B:27:0x00b1, B:53:0x0110, B:30:0x00c2, B:32:0x00c8, B:34:0x00d2, B:37:0x00dd, B:38:0x00e2, B:40:0x00e8, B:42:0x00fc, B:45:0x0101, B:46:0x0105), top: B:100:0x00a5 }] */
    /* JADX WARN: Removed duplicated region for block: B:36:0x00db  */
    /* JADX WARN: Removed duplicated region for block: B:52:0x010f  */
    /* JADX WARN: Removed duplicated region for block: B:59:0x016c  */
    /* JADX WARN: Removed duplicated region for block: B:71:0x01bb  */
    /* JADX WARN: Removed duplicated region for block: B:72:0x01be  */
    /* JADX WARN: Removed duplicated region for block: B:75:0x0221  */
    /* JADX WARN: Removed duplicated region for block: B:76:0x0228  */
    /* JADX WARN: Removed duplicated region for block: B:80:0x0256  */
    /* JADX WARN: Removed duplicated region for block: B:90:0x0277 A[PHI: r10
      0x0277: PHI (r10v2 int) = (r10v4 int), (r10v5 int) binds: [B:89:0x0275, B:83:0x0264] A[DONT_GENERATE, DONT_INLINE]] */
    @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object invokeSuspend(Object obj) {
        Object objD;
        Object obj2;
        DefaultDataSource.Factory factory;
        MediaSource mediaSource;
        Object objC;
        File file;
        VideoPlayerActivity videoPlayerActivity;
        ExoPlayer exoPlayer;
        Throwable thA;
        boolean z;
        int i;
        String strN;
        q07 q07Var;
        boolean zR;
        String strB;
        int size;
        ck1 ck1Var = ck1.a;
        int i2 = this.j;
        sb9 sb9Var = this.l;
        VideoPlayerActivity videoPlayerActivity2 = this.k;
        if (i2 == 0) {
            s38.b(obj);
            Toast.makeText(videoPlayerActivity2, "Loading subtitle...", 0).show();
            SubtitleManager subtitleManagerAq = videoPlayerActivity2.aq();
            this.j = 1;
            objD = subtitleManagerAq.d(sb9Var, this.m, this);
            if (objD != ck1Var) {
            }
            return ck1Var;
        }
        if (i2 != 1) {
            if (i2 != 2) {
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
            }
            file = this.i;
            exoPlayer = this.f;
            MediaSource mediaSource2 = this.e;
            DefaultDataSource.Factory factory2 = this.d;
            sb9 sb9Var2 = this.c;
            videoPlayerActivity = this.b;
            obj2 = this.a;
            s38.b(obj);
            mediaSource = mediaSource2;
            factory = factory2;
            sb9Var = sb9Var2;
            objC = obj;
            File file2 = (File) objC;
            videoPlayerActivity.f0 = videoPlayerActivity.a0 == 0;
            videoPlayerActivity.aq();
            cr4.g(file, "file");
            try {
                strB = ez2.b(file, bz0.a);
            } catch (Exception e) {
                Log.e("StremioSubtitleProvider", "Error analyzing subtitle file", e);
                q07Var = new q07(0, Boolean.FALSE);
            }
            if (n89.l(strB, "-->", false)) {
                if (n89.l(strB, "Dialogue:", false)) {
                    List listAf = n89.af(strB);
                    if ((listAf instanceof Collection) && listAf.isEmpty()) {
                        size = 0;
                    } else {
                        Iterator it = listAf.iterator();
                        int i3 = 0;
                        while (it.hasNext()) {
                            if (n89.ar(n89.bg((String) it.next()).toString(), "Dialogue:", false) && (i3 = i3 + 1) < 0) {
                                y41.bl();
                                throw null;
                            }
                        }
                        size = i3;
                    }
                }
                thA = q38.a(obj2);
                if (thA != null) {
                    String message = thA.getMessage();
                    if (message != null) {
                        z = false;
                        i = 1;
                        if (n89.l(message, "503", false)) {
                            strN = "OpenSubtitles is temporarily unavailable. Please try again later.";
                            Toast.makeText(videoPlayerActivity2, strN, i).show();
                            Log.e("VideoPlayerActivity", "Failed to apply subtitle", thA);
                        }
                    } else {
                        z = false;
                        i = 1;
                    }
                    String message2 = thA.getMessage();
                    if (message2 == null || n89.l(message2, "Service", z) != i) {
                        String message3 = thA.getMessage();
                        strN = (message3 == null || n89.l(message3, "429", z) != i) ? n41.n("Failed to load subtitle: ", thA.getMessage()) : "Too many requests. Please wait a moment and try again.";
                    }
                    Toast.makeText(videoPlayerActivity2, strN, i).show();
                    Log.e("VideoPlayerActivity", "Failed to apply subtitle", thA);
                }
                return e6a.a;
            }
            size = n89.ao(strB, new String[]{"-->"}, 6).size() - 1;
            q07Var = new q07(Integer.valueOf(size), Boolean.valueOf(1 > size && size < 76));
            int iIntValue = ((Number) q07Var.a).intValue();
            boolean zBooleanValue = ((Boolean) q07Var.b).booleanValue();
            videoPlayerActivity.aq();
            String str = sb9Var.d;
            String str2 = sb9Var.c;
            cr4.g(file2, "subtitleFile");
            cr4.g(str2, "label");
            String name = file2.getName();
            cr4.f(name, "getName(...)");
            zR = n89.r(name, ".srt", true);
            String str3 = MimeTypes.APPLICATION_SUBRIP;
            if (!zR) {
                String name2 = file2.getName();
                cr4.f(name2, "getName(...)");
                if (!n89.r(name2, ".vtt", true)) {
                    String name3 = file2.getName();
                    cr4.f(name3, "getName(...)");
                    if (!n89.r(name3, ".ttml", true)) {
                        String name4 = file2.getName();
                        cr4.f(name4, "getName(...)");
                        if (n89.r(name4, ".xml", true)) {
                            str3 = MimeTypes.APPLICATION_TTML;
                        } else {
                            String name5 = file2.getName();
                            cr4.f(name5, "getName(...)");
                            if (!n89.r(name5, ".ass", true)) {
                                String name6 = file2.getName();
                                cr4.f(name6, "getName(...)");
                                if (n89.r(name6, ".ssa", true)) {
                                    str3 = MimeTypes.TEXT_SSA;
                                }
                            }
                        }
                    }
                    thA = q38.a(obj2);
                    if (thA != null) {
                    }
                    return e6a.a;
                }
                str3 = MimeTypes.TEXT_VTT;
            }
            MediaItem.SubtitleConfiguration subtitleConfigurationBuild = new MediaItem.SubtitleConfiguration.Builder(Uri.fromFile(file2)).setMimeType(str3).setLanguage(str).setLabel("[EXT] ".concat(str2)).setSelectionFlags(1).build();
            cr4.f(subtitleConfigurationBuild, "build(...)");
            VideoPlayerActivity videoPlayerActivity3 = videoPlayerActivity;
            SingleSampleMediaSource singleSampleMediaSourceCreateMediaSource = new SingleSampleMediaSource.Factory(factory).createMediaSource(subtitleConfigurationBuild, C.TIME_UNSET);
            cr4.f(singleSampleMediaSourceCreateMediaSource, "createMediaSource(...)");
            MergingMediaSource mergingMediaSource = new MergingMediaSource(mediaSource, singleSampleMediaSourceCreateMediaSource);
            videoPlayerActivity3.bq = mergingMediaSource;
            exoPlayer.setMediaSource((MediaSource) mergingMediaSource, false);
            exoPlayer.prepare();
            exoPlayer.addListener(new dha(exoPlayer));
            Toast.makeText(videoPlayerActivity3, !zBooleanValue ? n41.i(iIntValue, "Subtitle loaded (", " entries) - May be forced/foreign-only. Try another if dialogue is missing.") : n41.i(iIntValue, "Subtitle loaded (", " entries)"), zBooleanValue ? 1 : 0).show();
            StringBuilder sbV = n41.v("Applied external subtitle via MergingMediaSource: ", iIntValue, sb9Var.b, " (", " entries, likelyForced=");
            sbV.append(zBooleanValue);
            sbV.append(")");
            Log.w("VideoPlayerActivity", sbV.toString());
            thA = q38.a(obj2);
            if (thA != null) {
            }
            return e6a.a;
        }
        s38.b(obj);
        objD = ((q38) obj).a;
        obj2 = objD;
        if (!(obj2 instanceof q38.b)) {
            File file3 = (File) obj2;
            SubtitleManager subtitleManagerAq2 = videoPlayerActivity2.aq();
            long j = videoPlayerActivity2.a0;
            this.a = obj2;
            this.b = videoPlayerActivity2;
            this.c = sb9Var;
            factory = this.n;
            this.d = factory;
            mediaSource = this.o;
            this.e = mediaSource;
            ExoPlayer exoPlayer2 = this.p;
            this.f = exoPlayer2;
            this.i = file3;
            this.j = 2;
            objC = subtitleManagerAq2.c(file3, j, this);
            if (objC != ck1Var) {
                file = file3;
                videoPlayerActivity = videoPlayerActivity2;
                exoPlayer = exoPlayer2;
                File file22 = (File) objC;
                videoPlayerActivity.f0 = videoPlayerActivity.a0 == 0;
                videoPlayerActivity.aq();
                cr4.g(file, "file");
                strB = ez2.b(file, bz0.a);
                if (n89.l(strB, "-->", false)) {
                }
                if (1 > size) {
                    q07Var = new q07(Integer.valueOf(size), Boolean.valueOf(1 > size && size < 76));
                    int iIntValue2 = ((Number) q07Var.a).intValue();
                    boolean zBooleanValue2 = ((Boolean) q07Var.b).booleanValue();
                    videoPlayerActivity.aq();
                    String str4 = sb9Var.d;
                    String str22 = sb9Var.c;
                    cr4.g(file22, "subtitleFile");
                    cr4.g(str22, "label");
                    String name7 = file22.getName();
                    cr4.f(name7, "getName(...)");
                    zR = n89.r(name7, ".srt", true);
                    String str32 = MimeTypes.APPLICATION_SUBRIP;
                    if (!zR) {
                    }
                    MediaItem.SubtitleConfiguration subtitleConfigurationBuild2 = new MediaItem.SubtitleConfiguration.Builder(Uri.fromFile(file22)).setMimeType(str32).setLanguage(str4).setLabel("[EXT] ".concat(str22)).setSelectionFlags(1).build();
                    cr4.f(subtitleConfigurationBuild2, "build(...)");
                    VideoPlayerActivity videoPlayerActivity32 = videoPlayerActivity;
                    SingleSampleMediaSource singleSampleMediaSourceCreateMediaSource2 = new SingleSampleMediaSource.Factory(factory).createMediaSource(subtitleConfigurationBuild2, C.TIME_UNSET);
                    cr4.f(singleSampleMediaSourceCreateMediaSource2, "createMediaSource(...)");
                    MergingMediaSource mergingMediaSource2 = new MergingMediaSource(mediaSource, singleSampleMediaSourceCreateMediaSource2);
                    videoPlayerActivity32.bq = mergingMediaSource2;
                    exoPlayer.setMediaSource((MediaSource) mergingMediaSource2, false);
                    exoPlayer.prepare();
                    exoPlayer.addListener(new dha(exoPlayer));
                    Toast.makeText(videoPlayerActivity32, !zBooleanValue2 ? n41.i(iIntValue2, "Subtitle loaded (", " entries) - May be forced/foreign-only. Try another if dialogue is missing.") : n41.i(iIntValue2, "Subtitle loaded (", " entries)"), zBooleanValue2 ? 1 : 0).show();
                    StringBuilder sbV2 = n41.v("Applied external subtitle via MergingMediaSource: ", iIntValue2, sb9Var.b, " (", " entries, likelyForced=");
                    sbV2.append(zBooleanValue2);
                    sbV2.append(")");
                    Log.w("VideoPlayerActivity", sbV2.toString());
                }
            }
            return ck1Var;
        }
        thA = q38.a(obj2);
        if (thA != null) {
        }
        return e6a.a;
    }
}
