package com.strmr.ps.data.repository;

import android.util.Log;
import com.strmr.ps.data.local.entity.TraktAccount;
import com.strmr.ps.data.model.ContentItem;
import com.strmr.ps.data.model.trakt.TraktScrobbleRequest;
import com.strmr.ps.data.model.trakt.TraktScrobbleResponse;
import com.strmr.ps.data.remote.api.TraktApiService;
import defpackage.ak1;
import defpackage.ck1;
import defpackage.cr4;
import defpackage.e6a;
import defpackage.gr3;
import defpackage.kq;
import defpackage.n41;
import defpackage.q38;
import defpackage.s38;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutorService;
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;
import retrofit2.HttpException;

/* JADX INFO: loaded from: classes3.dex */
@Metadata(d1 = {"\u0000\u0010\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\u0010\u0003\u001a\b\u0012\u0004\u0012\u00020\u00020\u0001*\u00020\u0000H\n¢\u0006\u0004\b\u0003\u0010\u0004"}, d2 = {"Lak1;", "Lq38;", "Le6a;", "<anonymous>", "(Lak1;)Lq38;"}, k = 3, mv = {2, 0, 0})
@DebugMetadata(c = "com.strmr.ps.data.repository.TraktScrobbleRepository$startWatching$2", f = "TraktScrobbleRepository.kt", i = {1}, l = {90, 112}, m = "invokeSuspend", n = {"sessionKey"}, s = {"L$0"})
public final class TraktScrobbleRepository$startWatching$2 extends SuspendLambda implements gr3<ak1, Continuation<? super q38<? extends e6a>>, Object> {
    final /* synthetic */ ContentItem $contentItem;
    final /* synthetic */ Integer $episode;
    final /* synthetic */ Integer $season;
    Object L$0;
    int label;
    final /* synthetic */ TraktScrobbleRepository this$0;

    /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
    public TraktScrobbleRepository$startWatching$2(ContentItem contentItem, TraktScrobbleRepository traktScrobbleRepository, Integer num, Integer num2, Continuation<? super TraktScrobbleRepository$startWatching$2> continuation) {
        super(2, continuation);
        this.$contentItem = contentItem;
        this.this$0 = traktScrobbleRepository;
        this.$season = num;
        this.$episode = num2;
    }

    @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl
    public final Continuation<e6a> create(Object obj, Continuation<?> continuation) {
        return new TraktScrobbleRepository$startWatching$2(this.$contentItem, this.this$0, this.$season, this.$episode, continuation);
    }

    @Override // defpackage.gr3
    public final Object invoke(ak1 ak1Var, Continuation<? super q38<e6a>> continuation) {
        return ((TraktScrobbleRepository$startWatching$2) create(ak1Var, continuation)).invokeSuspend(e6a.a);
    }

    /* JADX WARN: Code restructure failed: missing block: B:34:0x012e, code lost:
    
        if (r3 == r4) goto L35;
     */
    /* JADX WARN: Removed duplicated region for block: B:51:0x01d9  */
    @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object invokeSuspend(Object obj) {
        Object objRefreshTokenIfNeeded;
        String strGenerateSessionKey;
        Object objStartScrobble;
        String str;
        Exception e;
        boolean z;
        String str2;
        Integer num;
        ck1 ck1Var = ck1.a;
        int i = this.label;
        e6a e6aVar = e6a.a;
        try {
            if (i == 0) {
                s38.b(obj);
                if (this.$contentItem.getItemType() != ContentItem.ItemType.TV_SHOW || this.this$0.isValidEpisodeInfo(this.$season, this.$episode)) {
                    TraktAccountRepository traktAccountRepository = this.this$0.accountRepository;
                    this.label = 1;
                    objRefreshTokenIfNeeded = traktAccountRepository.refreshTokenIfNeeded(this);
                    if (objRefreshTokenIfNeeded != ck1Var) {
                    }
                    return ck1Var;
                }
                Log.e("TraktScrobble", "REJECTED scrobble for TV show '" + this.$contentItem.getTitle() + "' - missing valid episode info (season=" + this.$season + ", episode=" + this.$episode + "). Without episode info, the ENTIRE SHOW would be marked watched!");
                return new q38(s38.a(new IllegalArgumentException("TV show scrobble requires valid season and episode")));
            }
            if (i != 1) {
                if (i != 2) {
                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                }
                str = (String) this.L$0;
                try {
                    s38.b(obj);
                    strGenerateSessionKey = str;
                    objStartScrobble = obj;
                    TraktScrobbleResponse traktScrobbleResponse = (TraktScrobbleResponse) objStartScrobble;
                    ExecutorService executorService = kq.a;
                    String title = this.$contentItem.getTitle();
                    Integer num2 = this.$season;
                    if (num2 == null || (num = this.$episode) == null) {
                        str2 = "";
                    } else {
                        str2 = " S" + num2 + "E" + num;
                    }
                    kq.h("TraktScrobble", "Started watching: " + title + str2 + " (tmdb=" + this.$contentItem.getTmdbId() + ", imdb=" + this.$contentItem.getImdbId() + ") — trakt responded: action=" + traktScrobbleResponse.getAction() + ", id=" + traktScrobbleResponse.getId());
                    str = strGenerateSessionKey;
                    z = true;
                } catch (Exception e2) {
                    e = e2;
                    if ((e instanceof HttpException) || ((HttpException) e).code() != 422) {
                        Boxing.boxInt(Log.e("TraktScrobble", "Failed to start scrobble for " + this.$contentItem.getTitle(), e));
                    } else {
                        ExecutorService executorService2 = kq.a;
                        String title2 = this.$contentItem.getTitle();
                        int tmdbId = this.$contentItem.getTmdbId();
                        String imdbId = this.$contentItem.getImdbId();
                        StringBuilder sbV = n41.v("Start scrobble returned 422 (content may not be found on Trakt): ", tmdbId, title2, " (tmdb=", ", imdb=");
                        sbV.append(imdbId);
                        sbV.append(")");
                        kq.h("TraktScrobble", sbV.toString());
                    }
                    z = false;
                }
                if (z) {
                    this.this$0.currentSessionKey = str;
                    this.this$0.lastUpdateTime = System.currentTimeMillis();
                }
                return new q38(e6aVar);
            }
            s38.b(obj);
            objRefreshTokenIfNeeded = obj;
            TraktAccount traktAccount = (TraktAccount) objRefreshTokenIfNeeded;
            if (traktAccount == null) {
                ExecutorService executorService3 = kq.a;
                kq.h("TraktScrobble", "Not authenticated - skipping scrobble for " + this.$contentItem.getTitle());
                return new q38(e6aVar);
            }
            strGenerateSessionKey = this.this$0.generateSessionKey(this.$contentItem, this.$season, this.$episode);
            if (!cr4.b(strGenerateSessionKey, this.this$0.currentSessionKey)) {
                Log.w("TraktScrobble", n41.o("New scrobble session: ", strGenerateSessionKey, " (previous: ", this.this$0.currentSessionKey, ")"));
                this.this$0.resetState();
            }
            TraktScrobbleRequest traktScrobbleRequestBuildScrobbleRequest = this.this$0.buildScrobbleRequest(this.$contentItem, this.$season, this.$episode, 0.0d);
            try {
                TraktApiService traktApiService = this.this$0.traktApiService;
                String str3 = "Bearer " + traktAccount.getAccessToken();
                this.L$0 = strGenerateSessionKey;
                this.label = 2;
                objStartScrobble = traktApiService.startScrobble(str3, traktScrobbleRequestBuildScrobbleRequest, this);
            } catch (Exception e3) {
                e = e3;
                str = strGenerateSessionKey;
                if (e instanceof HttpException) {
                    Boxing.boxInt(Log.e("TraktScrobble", "Failed to start scrobble for " + this.$contentItem.getTitle(), e));
                    z = false;
                }
            }
        } catch (CancellationException e4) {
            throw e4;
        }
    }
}
