package com.strmr.ps.ui.mobile.details;

import android.util.Log;
import com.strmr.ps.data.local.entity.PlaybackProgress;
import com.strmr.ps.data.model.tmdb.TMDBEpisode;
import com.strmr.ps.data.model.tmdb.TMDBSeasonDetails;
import com.strmr.ps.data.model.trakt.TraktShowProgress;
import com.strmr.ps.data.remote.RateLimiter;
import com.strmr.ps.data.remote.api.TMDBApiService;
import com.strmr.ps.data.repository.PlaybackProgressRepository;
import com.strmr.ps.data.repository.TraktSyncRepository;
import com.strmr.ps.ui.mobile.details.MobileDetailsViewModel;
import defpackage.ak1;
import defpackage.ck1;
import defpackage.cr4;
import defpackage.e6a;
import defpackage.fp2;
import defpackage.gr3;
import defpackage.h26;
import defpackage.ik2;
import defpackage.m12;
import defpackage.pk2;
import defpackage.qm5;
import defpackage.s38;
import defpackage.sc2;
import defpackage.sm5;
import defpackage.vx8;
import defpackage.y41;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CancellationException;
import kotlin.Metadata;
import kotlin.coroutines.Continuation;
import kotlin.coroutines.jvm.internal.Boxing;
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.mobile.details.MobileDetailsViewModel$loadEpisodesForSeason$2", f = "MobileDetailsViewModel.kt", i = {2, 3, 3, 4, 4, 4}, l = {608, 609, 615, 630, 634, 672, 683}, m = "invokeSuspend", n = {"seasonDetails", "seasonDetails", "progressMap", "seasonDetails", "progressMap", "watchedEpisodes"}, s = {"L$0", "L$0", "L$1", "L$0", "L$1", "L$2"})
@vx8
final class u extends SuspendLambda implements gr3<ak1, Continuation<? super e6a>, Object> {
    public TMDBSeasonDetails a;
    public Map b;
    public Set c;
    public int d;
    public final /* synthetic */ MobileDetailsViewModel e;
    public final /* synthetic */ int f;
    public final /* synthetic */ int i;

    /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
    public u(int i, int i2, MobileDetailsViewModel mobileDetailsViewModel, Continuation continuation) {
        super(2, continuation);
        this.e = mobileDetailsViewModel;
        this.f = i;
        this.i = i2;
    }

    @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl
    public final Continuation create(Object obj, Continuation continuation) {
        return new u(this.f, this.i, this.e, continuation);
    }

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

    /* JADX WARN: Code restructure failed: missing block: B:107:0x025e, code lost:
    
        if (defpackage.wj0.f(r0, r6, r22) != r3) goto L112;
     */
    /* JADX WARN: Code restructure failed: missing block: B:110:0x028b, code lost:
    
        if (defpackage.wj0.f(r0, r4, r22) != r3) goto L112;
     */
    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:105:0x0245 A[Catch: Exception -> 0x0028, CancellationException -> 0x002b, TryCatch #2 {CancellationException -> 0x002b, Exception -> 0x0028, blocks: (B:7:0x0023, B:14:0x0036, B:56:0x0116, B:57:0x0120, B:59:0x0126, B:61:0x0142, B:63:0x0148, B:64:0x014e, B:66:0x0154, B:70:0x0180, B:72:0x0184, B:76:0x018e, B:78:0x0194, B:82:0x01a5, B:83:0x01a9, B:84:0x01b9, B:86:0x01bf, B:87:0x01db, B:89:0x01e1, B:92:0x0212, B:97:0x021f, B:99:0x0223, B:102:0x022d, B:104:0x023b, B:106:0x0247, B:105:0x0245, B:17:0x0044, B:47:0x00f5, B:49:0x00f9, B:52:0x0101, B:51:0x00ff, B:20:0x004d, B:34:0x008a, B:35:0x0097, B:37:0x009d, B:39:0x00a9, B:41:0x00af, B:43:0x00e4, B:21:0x0053, B:31:0x0079, B:22:0x0059, B:28:0x006c, B:25:0x0060), top: B:115:0x0013 }] */
    /* JADX WARN: Removed duplicated region for block: B:30:0x0077  */
    /* JADX WARN: Removed duplicated region for block: B:31:0x0079 A[Catch: Exception -> 0x0028, CancellationException -> 0x002b, PHI: r4
      0x0079: PHI (r4v8 java.lang.Object) = (r4v7 java.lang.Object), (r4v10 java.lang.Object) binds: [B:29:0x0075, B:21:0x0053] A[DONT_GENERATE, DONT_INLINE], TryCatch #2 {CancellationException -> 0x002b, Exception -> 0x0028, blocks: (B:7:0x0023, B:14:0x0036, B:56:0x0116, B:57:0x0120, B:59:0x0126, B:61:0x0142, B:63:0x0148, B:64:0x014e, B:66:0x0154, B:70:0x0180, B:72:0x0184, B:76:0x018e, B:78:0x0194, B:82:0x01a5, B:83:0x01a9, B:84:0x01b9, B:86:0x01bf, B:87:0x01db, B:89:0x01e1, B:92:0x0212, B:97:0x021f, B:99:0x0223, B:102:0x022d, B:104:0x023b, B:106:0x0247, B:105:0x0245, B:17:0x0044, B:47:0x00f5, B:49:0x00f9, B:52:0x0101, B:51:0x00ff, B:20:0x004d, B:34:0x008a, B:35:0x0097, B:37:0x009d, B:39:0x00a9, B:41:0x00af, B:43:0x00e4, B:21:0x0053, B:31:0x0079, B:22:0x0059, B:28:0x006c, B:25:0x0060), top: B:115:0x0013 }] */
    /* JADX WARN: Removed duplicated region for block: B:33:0x0088  */
    /* JADX WARN: Removed duplicated region for block: B:34:0x008a A[Catch: Exception -> 0x0028, CancellationException -> 0x002b, PHI: r4 r11
      0x008a: PHI (r4v11 com.strmr.ps.data.model.tmdb.TMDBSeasonDetails) = (r4v9 com.strmr.ps.data.model.tmdb.TMDBSeasonDetails), (r4v13 com.strmr.ps.data.model.tmdb.TMDBSeasonDetails) binds: [B:32:0x0086, B:20:0x004d] A[DONT_GENERATE, DONT_INLINE]
      0x008a: PHI (r11v3 java.lang.Object) = (r11v2 java.lang.Object), (r11v8 java.lang.Object) binds: [B:32:0x0086, B:20:0x004d] A[DONT_GENERATE, DONT_INLINE], TryCatch #2 {CancellationException -> 0x002b, Exception -> 0x0028, blocks: (B:7:0x0023, B:14:0x0036, B:56:0x0116, B:57:0x0120, B:59:0x0126, B:61:0x0142, B:63:0x0148, B:64:0x014e, B:66:0x0154, B:70:0x0180, B:72:0x0184, B:76:0x018e, B:78:0x0194, B:82:0x01a5, B:83:0x01a9, B:84:0x01b9, B:86:0x01bf, B:87:0x01db, B:89:0x01e1, B:92:0x0212, B:97:0x021f, B:99:0x0223, B:102:0x022d, B:104:0x023b, B:106:0x0247, B:105:0x0245, B:17:0x0044, B:47:0x00f5, B:49:0x00f9, B:52:0x0101, B:51:0x00ff, B:20:0x004d, B:34:0x008a, B:35:0x0097, B:37:0x009d, B:39:0x00a9, B:41:0x00af, B:43:0x00e4, B:21:0x0053, B:31:0x0079, B:22:0x0059, B:28:0x006c, B:25:0x0060), top: B:115:0x0013 }] */
    /* JADX WARN: Removed duplicated region for block: B:37:0x009d A[Catch: Exception -> 0x0028, CancellationException -> 0x002b, TryCatch #2 {CancellationException -> 0x002b, Exception -> 0x0028, blocks: (B:7:0x0023, B:14:0x0036, B:56:0x0116, B:57:0x0120, B:59:0x0126, B:61:0x0142, B:63:0x0148, B:64:0x014e, B:66:0x0154, B:70:0x0180, B:72:0x0184, B:76:0x018e, B:78:0x0194, B:82:0x01a5, B:83:0x01a9, B:84:0x01b9, B:86:0x01bf, B:87:0x01db, B:89:0x01e1, B:92:0x0212, B:97:0x021f, B:99:0x0223, B:102:0x022d, B:104:0x023b, B:106:0x0247, B:105:0x0245, B:17:0x0044, B:47:0x00f5, B:49:0x00f9, B:52:0x0101, B:51:0x00ff, B:20:0x004d, B:34:0x008a, B:35:0x0097, B:37:0x009d, B:39:0x00a9, B:41:0x00af, B:43:0x00e4, B:21:0x0053, B:31:0x0079, B:22:0x0059, B:28:0x006c, B:25:0x0060), top: B:115:0x0013 }] */
    /* JADX WARN: Removed duplicated region for block: B:45:0x00f1  */
    /* JADX WARN: Removed duplicated region for block: B:46:0x00f3  */
    /* JADX WARN: Removed duplicated region for block: B:54:0x0113  */
    /* JADX WARN: Removed duplicated region for block: B:55:0x0115  */
    /* JADX WARN: Removed duplicated region for block: B:59:0x0126 A[Catch: Exception -> 0x0028, CancellationException -> 0x002b, TryCatch #2 {CancellationException -> 0x002b, Exception -> 0x0028, blocks: (B:7:0x0023, B:14:0x0036, B:56:0x0116, B:57:0x0120, B:59:0x0126, B:61:0x0142, B:63:0x0148, B:64:0x014e, B:66:0x0154, B:70:0x0180, B:72:0x0184, B:76:0x018e, B:78:0x0194, B:82:0x01a5, B:83:0x01a9, B:84:0x01b9, B:86:0x01bf, B:87:0x01db, B:89:0x01e1, B:92:0x0212, B:97:0x021f, B:99:0x0223, B:102:0x022d, B:104:0x023b, B:106:0x0247, B:105:0x0245, B:17:0x0044, B:47:0x00f5, B:49:0x00f9, B:52:0x0101, B:51:0x00ff, B:20:0x004d, B:34:0x008a, B:35:0x0097, B:37:0x009d, B:39:0x00a9, B:41:0x00af, B:43:0x00e4, B:21:0x0053, B:31:0x0079, B:22:0x0059, B:28:0x006c, B:25:0x0060), top: B:115:0x0013 }] */
    /* JADX WARN: Removed duplicated region for block: B:73:0x0189  */
    /* JADX WARN: Removed duplicated region for block: B:86:0x01bf A[Catch: Exception -> 0x0028, CancellationException -> 0x002b, TryCatch #2 {CancellationException -> 0x002b, Exception -> 0x0028, blocks: (B:7:0x0023, B:14:0x0036, B:56:0x0116, B:57:0x0120, B:59:0x0126, B:61:0x0142, B:63:0x0148, B:64:0x014e, B:66:0x0154, B:70:0x0180, B:72:0x0184, B:76:0x018e, B:78:0x0194, B:82:0x01a5, B:83:0x01a9, B:84:0x01b9, B:86:0x01bf, B:87:0x01db, B:89:0x01e1, B:92:0x0212, B:97:0x021f, B:99:0x0223, B:102:0x022d, B:104:0x023b, B:106:0x0247, B:105:0x0245, B:17:0x0044, B:47:0x00f5, B:49:0x00f9, B:52:0x0101, B:51:0x00ff, B:20:0x004d, B:34:0x008a, B:35:0x0097, B:37:0x009d, B:39:0x00a9, B:41:0x00af, B:43:0x00e4, B:21:0x0053, B:31:0x0079, B:22:0x0059, B:28:0x006c, B:25:0x0060), top: B:115:0x0013 }] */
    /* JADX WARN: Type inference failed for: r9v3, types: [ik2] */
    /* JADX WARN: Type inference failed for: r9v4, types: [java.util.List] */
    /* JADX WARN: Type inference failed for: r9v6, types: [java.util.ArrayList] */
    @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object invokeSuspend(Object obj) {
        Object seasonDetails;
        TMDBSeasonDetails tMDBSeasonDetails;
        Object showProgress;
        Object showProgress2;
        TMDBSeasonDetails tMDBSeasonDetails2;
        Map map;
        TraktShowProgress traktShowProgress;
        Set<String> watchedEpisodeKeys;
        Object episodePlaybackProgress;
        TMDBSeasonDetails tMDBSeasonDetails3;
        List<TMDBEpisode> episodes;
        ?? arrayList;
        Set<String> set;
        String str;
        Long lBoxLong;
        Object next;
        MobileDetailsViewModel mobileDetailsViewModel = this.e;
        TraktSyncRepository traktSyncRepository = mobileDetailsViewModel.e;
        ck1 ck1Var = ck1.a;
        int i = this.d;
        int i2 = this.i;
        String str2 = "E";
        int i3 = this.f;
        try {
        } catch (CancellationException e) {
            throw e;
        } catch (Exception e2) {
            Log.e("MobileDetailsViewModel", "Error loading episodes for season " + i2, e2);
            m12 m12Var = sc2.a;
            qm5 qm5Var = sm5.a;
            t tVar = new t(mobileDetailsViewModel, null);
            this.a = null;
            this.b = null;
            this.c = null;
            this.d = 7;
        }
        switch (i) {
            case 0:
                s38.b(obj);
                RateLimiter rateLimiter = mobileDetailsViewModel.c;
                this.d = 1;
                if (rateLimiter.a(this) != ck1Var) {
                    TMDBApiService tMDBApiService = mobileDetailsViewModel.a;
                    this.d = 2;
                    seasonDetails = tMDBApiService.getSeasonDetails(i3, i2, this);
                    if (seasonDetails == ck1Var) {
                        tMDBSeasonDetails = (TMDBSeasonDetails) seasonDetails;
                        PlaybackProgressRepository playbackProgressRepository = mobileDetailsViewModel.d;
                        this.a = tMDBSeasonDetails;
                        this.d = 3;
                        showProgress = playbackProgressRepository.getShowProgress(i3, this);
                        if (showProgress == ck1Var) {
                            LinkedHashMap linkedHashMap = new LinkedHashMap();
                            for (PlaybackProgress playbackProgress : (List) showProgress) {
                                if (playbackProgress.getSeason() != null && playbackProgress.getEpisode() != null) {
                                    linkedHashMap.put("S" + playbackProgress.getSeason() + "E" + playbackProgress.getEpisode(), new MobileDetailsViewModel.d(playbackProgress.getPositionMs(), playbackProgress.getDurationMs(), playbackProgress.getPercent()));
                                }
                            }
                            this.a = tMDBSeasonDetails;
                            this.b = linkedHashMap;
                            this.d = 4;
                            showProgress2 = traktSyncRepository.getShowProgress(i3, this);
                            if (showProgress2 == ck1Var) {
                                tMDBSeasonDetails2 = tMDBSeasonDetails;
                                map = linkedHashMap;
                                traktShowProgress = (TraktShowProgress) showProgress2;
                                if (traktShowProgress != null || (watchedEpisodeKeys = traktShowProgress.getWatchedEpisodeKeys()) == null) {
                                    watchedEpisodeKeys = pk2.a;
                                }
                                this.a = tMDBSeasonDetails2;
                                this.b = map;
                                this.c = watchedEpisodeKeys;
                                this.d = 5;
                                episodePlaybackProgress = traktSyncRepository.getEpisodePlaybackProgress(i3, this);
                                if (episodePlaybackProgress == ck1Var) {
                                    tMDBSeasonDetails3 = tMDBSeasonDetails2;
                                    for (Map.Entry entry : ((Map) episodePlaybackProgress).entrySet()) {
                                        String str3 = (String) entry.getKey();
                                        float fFloatValue = ((Number) entry.getValue()).floatValue();
                                        if (!map.containsKey(str3)) {
                                            List<TMDBEpisode> episodes2 = tMDBSeasonDetails3.getEpisodes();
                                            if (episodes2 != null) {
                                                Iterator it = episodes2.iterator();
                                                while (true) {
                                                    if (it.hasNext()) {
                                                        next = it.next();
                                                        TMDBEpisode tMDBEpisode = (TMDBEpisode) next;
                                                        if (cr4.b("S" + tMDBEpisode.getSeasonNumber() + "E" + tMDBEpisode.getEpisodeNumber(), str3)) {
                                                        }
                                                    } else {
                                                        next = null;
                                                    }
                                                }
                                                TMDBEpisode tMDBEpisode2 = (TMDBEpisode) next;
                                                Integer runtime = tMDBEpisode2 != null ? tMDBEpisode2.getRuntime() : null;
                                                long jIntValue = (runtime == null || runtime.intValue() <= 0) ? 0L : ((long) (runtime.intValue() * 60)) * 1000;
                                                map.put(str3, new MobileDetailsViewModel.d(jIntValue > 0 ? (long) (jIntValue * fFloatValue) : 0L, jIntValue, fFloatValue));
                                            }
                                        }
                                    }
                                    episodes = tMDBSeasonDetails3.getEpisodes();
                                    if (episodes == null) {
                                        List<TMDBEpisode> listBj = y41.bj(new h26(), episodes);
                                        arrayList = new ArrayList(y41.p(listBj, 10));
                                        for (TMDBEpisode tMDBEpisode3 : listBj) {
                                            String str4 = "S" + tMDBEpisode3.getSeasonNumber() + str2 + tMDBEpisode3.getEpisodeNumber();
                                            MobileDetailsViewModel.d dVar = (MobileDetailsViewModel.d) map.get(str4);
                                            boolean z = watchedEpisodeKeys.contains(str4) || (dVar != null && dVar.c >= 0.9f);
                                            Float fBoxFloat = dVar != null ? Boxing.boxFloat(dVar.c) : null;
                                            if (dVar != null) {
                                                set = watchedEpisodeKeys;
                                                str = str2;
                                                lBoxLong = Boxing.boxLong(dVar.a);
                                            } else {
                                                set = watchedEpisodeKeys;
                                                str = str2;
                                                lBoxLong = null;
                                            }
                                            arrayList.add(new fp2(tMDBEpisode3, z, fBoxFloat, lBoxLong));
                                            str2 = str;
                                            watchedEpisodeKeys = set;
                                        }
                                    } else {
                                        arrayList = ik2.a;
                                    }
                                    m12 m12Var2 = sc2.a;
                                    qm5 qm5Var2 = sm5.a;
                                    s sVar = new s(mobileDetailsViewModel, arrayList, map, null);
                                    this.a = null;
                                    this.b = null;
                                    this.c = null;
                                    this.d = 6;
                                    break;
                                }
                            }
                        }
                    }
                }
                return ck1Var;
            case 1:
                s38.b(obj);
                TMDBApiService tMDBApiService2 = mobileDetailsViewModel.a;
                this.d = 2;
                seasonDetails = tMDBApiService2.getSeasonDetails(i3, i2, this);
                if (seasonDetails == ck1Var) {
                }
                return ck1Var;
            case 2:
                s38.b(obj);
                seasonDetails = obj;
                tMDBSeasonDetails = (TMDBSeasonDetails) seasonDetails;
                PlaybackProgressRepository playbackProgressRepository2 = mobileDetailsViewModel.d;
                this.a = tMDBSeasonDetails;
                this.d = 3;
                showProgress = playbackProgressRepository2.getShowProgress(i3, this);
                if (showProgress == ck1Var) {
                }
                return ck1Var;
            case 3:
                tMDBSeasonDetails = this.a;
                s38.b(obj);
                showProgress = obj;
                LinkedHashMap linkedHashMap2 = new LinkedHashMap();
                while (r11.hasNext()) {
                }
                this.a = tMDBSeasonDetails;
                this.b = linkedHashMap2;
                this.d = 4;
                showProgress2 = traktSyncRepository.getShowProgress(i3, this);
                if (showProgress2 == ck1Var) {
                }
                break;
            case 4:
                map = this.b;
                tMDBSeasonDetails2 = this.a;
                s38.b(obj);
                showProgress2 = obj;
                traktShowProgress = (TraktShowProgress) showProgress2;
                if (traktShowProgress != null) {
                }
                watchedEpisodeKeys = pk2.a;
                this.a = tMDBSeasonDetails2;
                this.b = map;
                this.c = watchedEpisodeKeys;
                this.d = 5;
                episodePlaybackProgress = traktSyncRepository.getEpisodePlaybackProgress(i3, this);
                if (episodePlaybackProgress == ck1Var) {
                }
                break;
            case 5:
                Set<String> set2 = this.c;
                map = this.b;
                tMDBSeasonDetails3 = this.a;
                s38.b(obj);
                watchedEpisodeKeys = set2;
                episodePlaybackProgress = obj;
                while (r0.hasNext()) {
                }
                episodes = tMDBSeasonDetails3.getEpisodes();
                if (episodes == null) {
                }
                m12 m12Var22 = sc2.a;
                qm5 qm5Var22 = sm5.a;
                s sVar2 = new s(mobileDetailsViewModel, arrayList, map, null);
                this.a = null;
                this.b = null;
                this.c = null;
                this.d = 6;
                break;
            case 6:
                s38.b(obj);
                return e6a.a;
            case 7:
                s38.b(obj);
                return e6a.a;
            default:
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
        }
    }
}
