package d3;

import V2.C0636a3;
import V2.M3;
import V2.Q2;
import V2.n3;
import V2.u3;
import android.content.Context;
import android.media.MediaCodec;
import android.net.Uri;
import android.os.Handler;
import androidx.compose.runtime.MutableState;
import androidx.compose.runtime.State;
import androidx.media3.common.Format;
import androidx.media3.common.MediaItem;
import androidx.media3.common.PlaybackException;
import androidx.media3.common.Player;
import androidx.media3.common.TrackGroup;
import androidx.media3.common.Tracks;
import androidx.media3.exoplayer.ExoPlaybackException;
import androidx.media3.exoplayer.ExoPlayer;
import com.google.firebase.crashlytics.FirebaseCrashlytics;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import m3.AbstractC2467b;

/* JADX INFO: renamed from: d3.a1, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public final class C1870a1 implements Player.Listener {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final /* synthetic */ ExoPlayer f15519a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final /* synthetic */ Context f15520b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final /* synthetic */ kotlin.jvm.internal.y f15521c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final /* synthetic */ Handler f15522d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final /* synthetic */ AtomicBoolean f15523e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final /* synthetic */ Set f15524f;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public final /* synthetic */ AtomicReference f15525j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public final /* synthetic */ String f15526k;
    public final /* synthetic */ MutableState l;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public final /* synthetic */ AtomicBoolean f15527m;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public final /* synthetic */ String f15528n;

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    public final /* synthetic */ MutableState f15529o;

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    public final /* synthetic */ EnumC1892i f15530p;

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public final /* synthetic */ boolean f15531q;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public final /* synthetic */ State f15532r;

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public final /* synthetic */ State f15533s;

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public final /* synthetic */ State f15534t;

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public final /* synthetic */ MutableState f15535u;

    /* JADX INFO: renamed from: v, reason: collision with root package name */
    public final /* synthetic */ MutableState f15536v;

    public C1870a1(ExoPlayer exoPlayer, Context context, kotlin.jvm.internal.y yVar, Handler handler, AtomicBoolean atomicBoolean, Set set, AtomicReference atomicReference, String str, MutableState mutableState, AtomicBoolean atomicBoolean2, String str2, MutableState mutableState2, EnumC1892i enumC1892i, boolean z6, State state, State state2, State state3, MutableState mutableState3, MutableState mutableState4) {
        this.f15519a = exoPlayer;
        this.f15520b = context;
        this.f15521c = yVar;
        this.f15522d = handler;
        this.f15523e = atomicBoolean;
        this.f15524f = set;
        this.f15525j = atomicReference;
        this.f15526k = str;
        this.l = mutableState;
        this.f15527m = atomicBoolean2;
        this.f15528n = str2;
        this.f15529o = mutableState2;
        this.f15530p = enumC1892i;
        this.f15531q = z6;
        this.f15532r = state;
        this.f15533s = state2;
        this.f15534t = state3;
        this.f15535u = mutableState3;
        this.f15536v = mutableState4;
    }

    @Override // androidx.media3.common.Player.Listener
    public final void onPlayWhenReadyChanged(boolean z6, int i6) {
        v2.D(this.f15520b, this.f15521c, 128, this.f15519a);
    }

    @Override // androidx.media3.common.Player.Listener
    public final void onPlaybackStateChanged(int i6) {
        v2.D(this.f15520b, this.f15521c, 128, this.f15519a);
    }

    /* JADX WARN: Code restructure failed: missing block: B:73:0x011e, code lost:
    
        if (S4.p.D0(r5, "MediaCodecVideoDecoderException", true) == false) goto L95;
     */
    /* JADX WARN: Code restructure failed: missing block: B:93:0x0167, code lost:
    
        if (S4.p.D0(r2, "MediaCodecVideoRenderer", true) != false) goto L50;
     */
    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:174:0x0354  */
    /* JADX WARN: Removed duplicated region for block: B:177:0x035e  */
    /* JADX WARN: Removed duplicated region for block: B:194:0x03c2  */
    /* JADX WARN: Removed duplicated region for block: B:238:0x0462  */
    /* JADX WARN: Removed duplicated region for block: B:301:0x0374 A[SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:51:0x00cf  */
    @Override // androidx.media3.common.Player.Listener
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void onPlayerError(PlaybackException error) {
        long j4;
        long j6;
        boolean z6;
        Throwable cause;
        boolean z7;
        boolean zD0;
        AtomicBoolean atomicBoolean;
        Object next;
        boolean z8;
        String str;
        J4.a aVar;
        Iterator it;
        Object next2;
        int i6;
        Object next3;
        Iterator it2;
        kotlin.jvm.internal.o.h(error, "error");
        long jCurrentTimeMillis = System.currentTimeMillis();
        MutableState mutableState = this.l;
        Long l = (Long) mutableState.getValue();
        final Handler handler = this.f15522d;
        final ExoPlayer exoPlayer = this.f15519a;
        final kotlin.jvm.internal.y yVar = this.f15521c;
        final Context context = this.f15520b;
        final AtomicBoolean atomicBoolean2 = this.f15527m;
        if (l == null || jCurrentTimeMillis - l.longValue() >= 60000) {
            boolean z9 = error instanceof ExoPlaybackException;
            ExoPlaybackException exoPlaybackException = z9 ? (ExoPlaybackException) error : null;
            j4 = 0;
            if (exoPlaybackException != null && exoPlaybackException.type == 0) {
                StringBuilder sb = new StringBuilder();
                String message = error.getMessage();
                sb.append(message == null ? "" : message);
                sb.append('\n');
                Throwable cause2 = error.getCause();
                String message2 = cause2 != null ? cause2.getMessage() : null;
                if (message2 == null) {
                    message2 = "";
                }
                sb.append(message2);
                String string = sb.toString();
                boolean z10 = S4.p.D0(string, "Network stalled", true) || S4.p.D0(string, "CodecId is missing in TrackEntry", true) || S4.p.D0(string, "No valid varint length mask found", true) || ((cause = error.getCause()) != null && S4.p.D0(cause.getClass().getName(), "UnexpectedLoaderException", true));
                Throwable cause3 = error;
                while (true) {
                    if (cause3 == null) {
                        z6 = false;
                        break;
                    } else {
                        if (cause3 instanceof OutOfMemoryError) {
                            z6 = true;
                            break;
                        }
                        cause3 = cause3.getCause();
                    }
                }
                if (z10 || z6) {
                    j6 = jCurrentTimeMillis;
                } else {
                    ExoPlaybackException exoPlaybackException2 = z9 ? (ExoPlaybackException) error : null;
                    if (exoPlaybackException2 != null && exoPlaybackException2.type == 1) {
                        Format format = exoPlaybackException2.rendererFormat;
                        if (format == null) {
                            StringBuilder sb2 = new StringBuilder();
                            String message3 = error.getMessage();
                            if (message3 == null) {
                                message3 = "";
                            }
                            sb2.append(message3);
                            sb2.append('\n');
                            Throwable cause4 = error.getCause();
                            String message4 = cause4 != null ? cause4.getMessage() : null;
                            if (message4 == null) {
                                message4 = "";
                            }
                            sb2.append(message4);
                            String string2 = sb2.toString();
                            if (!S4.p.D0(string2, "MediaCodecVideoRenderer", true)) {
                            }
                            j6 = jCurrentTimeMillis;
                        } else {
                            String str2 = format.sampleMimeType;
                            if (str2 != null) {
                                boolean z11 = true;
                                if (S4.w.A0(str2, "video/", false)) {
                                    Throwable cause5 = error;
                                    while (true) {
                                        if (cause5 != null) {
                                            String name = cause5.getClass().getName();
                                            j6 = jCurrentTimeMillis;
                                            if (S4.p.D0(name, "MediaCodecVideoDecoderException", z11) || S4.p.D0(name, "CodecException", z11) || (cause5 instanceof MediaCodec.CodecException)) {
                                                break;
                                            }
                                            cause5 = cause5.getCause();
                                            jCurrentTimeMillis = j6;
                                            z11 = true;
                                        } else {
                                            j6 = jCurrentTimeMillis;
                                            String message5 = error.getMessage();
                                            if (message5 == null) {
                                                message5 = "";
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            long currentPosition = exoPlayer.getCurrentPosition();
            final long j7 = currentPosition < 0 ? 0L : currentPosition;
            final boolean z12 = !exoPlayer.getPlayWhenReady();
            mutableState.setValue(Long.valueOf(j6));
            final Set set = this.f15524f;
            final String str3 = this.f15528n;
            final MutableState mutableState2 = this.f15529o;
            handler.post(new Runnable() { // from class: d3.Y0
                @Override // java.lang.Runnable
                public final void run() {
                    if (atomicBoolean2.get()) {
                        return;
                    }
                    set.clear();
                    v2.x(mutableState2, false);
                    MediaItem mediaItemFromUri = MediaItem.fromUri(Uri.parse(str3));
                    ExoPlayer exoPlayer2 = exoPlayer;
                    exoPlayer2.setMediaItem(mediaItemFromUri);
                    exoPlayer2.prepare();
                    long j8 = j7;
                    if (j8 > 0) {
                        exoPlayer2.seekTo(j8);
                    }
                    exoPlayer2.setPlayWhenReady(!z12);
                    v2.D(context, yVar, 128, exoPlayer2);
                }
            });
            return;
        }
        j4 = 0;
        if (v2.t0(error)) {
            StringBuilder sb3 = new StringBuilder();
            Throwable cause6 = error;
            for (int i7 = 0; cause6 != null && i7 < 16; i7++) {
                String message6 = cause6.getMessage();
                if (message6 == null) {
                    message6 = "";
                }
                sb3.append(message6);
                sb3.append('\n');
                cause6 = cause6.getCause();
            }
            z7 = true;
            zD0 = S4.p.D0(sb3.toString(), "format_supported=YES", true);
        } else {
            zD0 = false;
            z7 = true;
        }
        final State state = this.f15534t;
        final State state2 = this.f15532r;
        final String str4 = this.f15526k;
        if (zD0) {
            final AtomicBoolean atomicBoolean3 = this.f15523e;
            if (!atomicBoolean3.getAndSet(z7)) {
                final long currentPosition2 = exoPlayer.getCurrentPosition();
                if (currentPosition2 < j4) {
                    currentPosition2 = j4;
                }
                final boolean playWhenReady = z7 ^ exoPlayer.getPlayWhenReady();
                final EnumC1892i enumC1892i = this.f15530p;
                final MutableState mutableState3 = this.f15535u;
                final MutableState mutableState4 = this.f15536v;
                final boolean z13 = this.f15531q;
                final String str5 = this.f15528n;
                final MutableState mutableState5 = this.f15529o;
                final Set set2 = this.f15524f;
                final AtomicReference atomicReference = this.f15525j;
                final MutableState mutableState6 = this.l;
                final State state3 = this.f15533s;
                handler.post(new Runnable() { // from class: d3.Z0
                    @Override // java.lang.Runnable
                    public final void run() {
                        AtomicBoolean atomicBoolean4 = atomicBoolean2;
                        if (atomicBoolean4.get()) {
                            return;
                        }
                        MutableState mutableState7 = mutableState5;
                        v2.x(mutableState7, false);
                        MutableState mutableState8 = mutableState3;
                        ExoPlayer exoPlayer2 = (ExoPlayer) mutableState8.getValue();
                        ExoPlayer exoPlayer3 = exoPlayer;
                        if (exoPlayer2 == exoPlayer3) {
                            mutableState8.setValue(null);
                        }
                        v2.E0(exoPlayer3);
                        Context context2 = context;
                        EnumC1892i enumC1892i2 = enumC1892i;
                        boolean z14 = z13;
                        ExoPlayer exoPlayerA = C1930v.a(context2, enumC1892i2, z14, true);
                        kotlin.jvm.internal.y yVar2 = yVar;
                        Handler handler2 = handler;
                        AtomicBoolean atomicBoolean5 = atomicBoolean3;
                        String str6 = str4;
                        State state4 = state2;
                        State state5 = state;
                        Set set3 = set2;
                        AtomicReference atomicReference2 = atomicReference;
                        MutableState mutableState9 = mutableState6;
                        String str7 = str5;
                        State state6 = state3;
                        MutableState mutableState10 = mutableState4;
                        exoPlayerA.addListener(new C1870a1(exoPlayerA, context2, yVar2, handler2, atomicBoolean5, set3, atomicReference2, str6, mutableState9, atomicBoolean4, str7, mutableState7, enumC1892i2, z14, state4, state6, state5, mutableState8, mutableState10));
                        exoPlayerA.addAnalyticsListener(new C1876c1(handler2, atomicBoolean4, mutableState10));
                        exoPlayerA.setRepeatMode(0);
                        exoPlayerA.setMediaItem(MediaItem.fromUri(Uri.parse(str7)));
                        exoPlayerA.prepare();
                        long j8 = currentPosition2;
                        if (j8 > 0) {
                            exoPlayerA.seekTo(j8);
                        }
                        exoPlayerA.setPlayWhenReady(!playWhenReady);
                        mutableState8.setValue(exoPlayerA);
                        v2.D(context2, yVar2, 128, exoPlayerA);
                    }
                });
                return;
            }
        }
        AtomicBoolean atomicBoolean4 = atomicBoolean2;
        if (v2.t0(error)) {
            Tracks currentTracks = exoPlayer.getCurrentTracks();
            kotlin.jvm.internal.o.g(currentTracks, "getCurrentTracks(...)");
            ArrayList arrayList = new ArrayList();
            F3.O groups = currentTracks.getGroups();
            kotlin.jvm.internal.o.g(groups, "getGroups(...)");
            Iterator it3 = groups.iterator();
            while (it3.hasNext()) {
                Tracks.Group group = (Tracks.Group) it3.next();
                if (group.getType() == 1) {
                    TrackGroup mediaTrackGroup = group.getMediaTrackGroup();
                    kotlin.jvm.internal.o.g(mediaTrackGroup, "getMediaTrackGroup(...)");
                    int i8 = group.length;
                    int i9 = 0;
                    while (i9 < i8) {
                        if (group.isTrackSupported(i9)) {
                            Format trackFormat = group.getTrackFormat(i9);
                            it2 = it3;
                            kotlin.jvm.internal.o.g(trackFormat, "getTrackFormat(...)");
                            arrayList.add(new C(mediaTrackGroup, i9, v2.l0(trackFormat), ""));
                        } else {
                            it2 = it3;
                        }
                        i9++;
                        it3 = it2;
                    }
                }
                it3 = it3;
            }
            if (arrayList.size() > 1) {
                M3 m32 = ((n3) ((Y4.c0) u3.f8112c.f8871a).getValue()).f7977g;
                List listI0 = v2.i0(v2.z0(m32.f7448n), v2.z0(m32.f7449o), arrayList);
                int size = arrayList.size();
                int i10 = 0;
                while (true) {
                    if (i10 >= size) {
                        atomicBoolean = atomicBoolean4;
                        i10 = -1;
                        break;
                    }
                    C c6 = (C) arrayList.get(i10);
                    F3.O groups2 = exoPlayer.getCurrentTracks().getGroups();
                    kotlin.jvm.internal.o.g(groups2, "getGroups(...)");
                    Iterator<E> it4 = groups2.iterator();
                    while (true) {
                        if (!it4.hasNext()) {
                            i6 = size;
                            atomicBoolean = atomicBoolean4;
                            next3 = null;
                            break;
                        }
                        next3 = it4.next();
                        Tracks.Group group2 = (Tracks.Group) next3;
                        i6 = size;
                        atomicBoolean = atomicBoolean4;
                        if (group2.getType() == 1 && kotlin.jvm.internal.o.c(group2.getMediaTrackGroup(), c6.f15031a)) {
                            break;
                        }
                        size = i6;
                        atomicBoolean4 = atomicBoolean;
                    }
                    Tracks.Group group3 = (Tracks.Group) next3;
                    if (group3 != null && group3.isTrackSelected(c6.f15032b)) {
                        break;
                    }
                    i10++;
                    size = i6;
                    atomicBoolean4 = atomicBoolean;
                }
                Integer numValueOf = Integer.valueOf(i10);
                if (!(i10 >= 0)) {
                    numValueOf = null;
                }
                while (true) {
                    Set set3 = this.f15524f;
                    kotlin.jvm.internal.o.e(set3);
                    if (listI0.isEmpty()) {
                        numValueOf = null;
                    } else if (numValueOf == null || numValueOf.intValue() < 0) {
                        it = listI0.iterator();
                        while (true) {
                            if (!it.hasNext()) {
                                next2 = it.next();
                                if (!set3.contains(Integer.valueOf(((Number) next2).intValue()))) {
                                    break;
                                }
                            } else {
                                next2 = null;
                                break;
                            }
                        }
                        numValueOf = (Integer) next2;
                    } else {
                        set3.add(numValueOf);
                        int iIndexOf = listI0.indexOf(numValueOf);
                        if (iIndexOf >= 0) {
                            int size2 = listI0.size();
                            for (int i11 = iIndexOf + 1; i11 < size2; i11++) {
                                int iIntValue = ((Number) listI0.get(i11)).intValue();
                                if (!set3.contains(Integer.valueOf(iIntValue))) {
                                    numValueOf = Integer.valueOf(iIntValue);
                                    break;
                                }
                            }
                            it = listI0.iterator();
                            while (true) {
                                if (!it.hasNext()) {
                                }
                            }
                            numValueOf = (Integer) next2;
                        }
                    }
                    if (numValueOf == null) {
                        numValueOf = null;
                        break;
                    } else if (!v2.h0((C) arrayList.get(numValueOf.intValue()), ((n3) state2.getValue()).f7974d)) {
                        break;
                    } else {
                        set3.add(numValueOf);
                    }
                }
                if (numValueOf != null) {
                    long currentPosition3 = exoPlayer.getCurrentPosition();
                    if (currentPosition3 < j4) {
                        currentPosition3 = j4;
                    }
                    boolean playWhenReady2 = exoPlayer.getPlayWhenReady();
                    v2.k0(exoPlayer, arrayList, numValueOf.intValue(), 1);
                    exoPlayer.prepare();
                    exoPlayer.seekTo(currentPosition3);
                    exoPlayer.setPlayWhenReady(playWhenReady2);
                    v2.D(context, yVar, 128, exoPlayer);
                    return;
                }
            } else {
                atomicBoolean = atomicBoolean4;
            }
        }
        Iterator it5 = ((C0636a3) this.f15533s.getValue()).f7691j.iterator();
        while (true) {
            if (!it5.hasNext()) {
                next = null;
                break;
            }
            next = it5.next();
            N2.f fVar = (N2.f) next;
            String str6 = ((Q2) state.getValue()).f7497b;
            String string3 = str6 != null ? S4.p.o1(str6).toString() : null;
            if (string3 == null) {
                string3 = "";
            }
            String str7 = ((Q2) state.getValue()).f7498c;
            String string4 = str7 != null ? S4.p.o1(str7).toString() : null;
            if (string4 == null) {
                string4 = "";
            }
            if (!S4.p.Q0(string3) && !S4.p.Q0(string4) && kotlin.jvm.internal.o.c(fVar.f5143a, string3) && S4.w.t0(fVar.f5144b, string4, true)) {
                break;
            }
        }
        N2.f fVar2 = (N2.f) next;
        String strW0 = fVar2 != null ? v2.w0(fVar2) : null;
        long duration = exoPlayer.getDuration();
        if (duration == androidx.media3.common.C.TIME_UNSET || duration <= j4) {
            z8 = false;
        } else {
            long currentPosition4 = exoPlayer.getCurrentPosition();
            if (currentPosition4 < j4) {
                currentPosition4 = j4;
            }
            if (currentPosition4 >= duration - androidx.media3.common.C.DEFAULT_MAX_SEEK_TO_PREVIOUS_POSITION_MS) {
                z8 = true;
            }
        }
        MutableState mutableState7 = this.f15529o;
        if (z8 && (aVar = (J4.a) this.f15525j.get()) != null) {
            handler.post(new androidx.media3.exoplayer.source.h(atomicBoolean, exoPlayer, aVar, mutableState7, 3));
            v2.D(context, yVar, 128, exoPlayer);
            return;
        }
        if (!v2.r0(error) && !v2.q0(error) && !v2.s0(error)) {
            if (strW0 == null) {
                str = ((Q2) state.getValue()).f7501f;
                if (str == null) {
                    str = str4;
                }
            } else {
                str = strW0;
            }
            long currentPosition5 = exoPlayer.getCurrentPosition();
            if (currentPosition5 < j4) {
                currentPosition5 = j4;
            }
            String str8 = ((Q2) state.getValue()).f7497b;
            try {
                FirebaseCrashlytics firebaseCrashlytics = FirebaseCrashlytics.getInstance();
                kotlin.jvm.internal.o.g(firebaseCrashlytics, "getInstance(...)");
                firebaseCrashlytics.setCustomKey("player_error_screen", "VideoPlayerScreen");
                firebaseCrashlytics.setCustomKey("player_error_phase", "playback");
                firebaseCrashlytics.setCustomKey("player_error_file_name", str);
                firebaseCrashlytics.setCustomKey("player_error_position_ms", currentPosition5);
                firebaseCrashlytics.setCustomKey("player_error_link_hash", str8 == null ? "" : str8);
                firebaseCrashlytics.recordException(error);
            } catch (Throwable th) {
                AbstractC2467b.d(th);
            }
        }
        handler.post(new F1.r(mutableState7, 20));
        v2.D(context, yVar, 128, exoPlayer);
    }
}
