package com.strmr.ps.data.repository;

import android.util.Log;
import androidx.media3.exoplayer.upstream.CmcdData;
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.remote.api.TraktApiService;
import defpackage.ak1;
import defpackage.ck1;
import defpackage.e6a;
import defpackage.gr3;
import defpackage.kq;
import defpackage.n89;
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 okhttp3.Headers;
import retrofit2.HttpException;
import retrofit2.Response;

/* JADX INFO: loaded from: classes3.dex */
@Metadata(d1 = {"\u0000\u0010\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0010\u000b\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;", "", "<anonymous>", "(Lak1;)Lq38;"}, k = 3, mv = {2, 0, 0})
@DebugMetadata(c = "com.strmr.ps.data.repository.TraktScrobbleRepository$updateProgress$2", f = "TraktScrobbleRepository.kt", i = {0, 1, 1, 2, 3, 3, 4, 4}, l = {186, 208, 217, 226, 234}, m = "invokeSuspend", n = {"now", "now", "progress", "now", "now", "progress", "now", "progress"}, s = {"J$0", "J$0", "D$0", "J$0", "J$0", "D$0", "J$0", "D$0"})
public final class TraktScrobbleRepository$updateProgress$2 extends SuspendLambda implements gr3<ak1, Continuation<? super q38<? extends Boolean>>, Object> {
    final /* synthetic */ ContentItem $contentItem;
    final /* synthetic */ long $currentPositionMs;
    final /* synthetic */ long $durationMs;
    final /* synthetic */ Integer $episode;
    final /* synthetic */ boolean $isPaused;
    final /* synthetic */ Integer $season;
    double D$0;
    long J$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$updateProgress$2(ContentItem contentItem, TraktScrobbleRepository traktScrobbleRepository, Integer num, Integer num2, boolean z, long j, long j2, Continuation<? super TraktScrobbleRepository$updateProgress$2> continuation) {
        super(2, continuation);
        this.$contentItem = contentItem;
        this.this$0 = traktScrobbleRepository;
        this.$season = num;
        this.$episode = num2;
        this.$isPaused = z;
        this.$currentPositionMs = j;
        this.$durationMs = j2;
    }

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

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

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:142:0x02e7  */
    /* JADX WARN: Removed duplicated region for block: B:143:0x02eb  */
    /* JADX WARN: Removed duplicated region for block: B:145:0x02ee  */
    /* JADX WARN: Removed duplicated region for block: B:148:0x02f9  */
    /* JADX WARN: Removed duplicated region for block: B:156:0x033a  */
    /* JADX WARN: Removed duplicated region for block: B:159:0x034c  */
    /* JADX WARN: Removed duplicated region for block: B:170:0x0395  */
    /* JADX WARN: Removed duplicated region for block: B:87:0x01c2  */
    /* JADX WARN: Removed duplicated region for block: B:92:0x01f3  */
    /* JADX WARN: Type inference failed for: r13v0 */
    /* JADX WARN: Type inference failed for: r13v1 */
    /* JADX WARN: Type inference failed for: r13v16 */
    /* JADX WARN: Type inference failed for: r13v17 */
    /* JADX WARN: Type inference failed for: r13v2 */
    /* JADX WARN: Type inference failed for: r13v20 */
    /* JADX WARN: Type inference failed for: r13v21 */
    /* JADX WARN: Type inference failed for: r13v22 */
    /* JADX WARN: Type inference failed for: r13v25 */
    /* JADX WARN: Type inference failed for: r13v26 */
    /* JADX WARN: Type inference failed for: r13v3 */
    /* JADX WARN: Type inference failed for: r13v32 */
    /* JADX WARN: Type inference failed for: r13v33 */
    /* JADX WARN: Type inference failed for: r13v35 */
    /* JADX WARN: Type inference failed for: r13v36 */
    /* JADX WARN: Type inference failed for: r13v38 */
    /* JADX WARN: Type inference failed for: r13v39 */
    /* JADX WARN: Type inference failed for: r13v4, types: [java.lang.String] */
    /* JADX WARN: Type inference failed for: r13v8, types: [java.lang.String] */
    @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object invokeSuspend(Object obj) {
        String str;
        ?? r13;
        Integer numBoxInt;
        Integer num;
        Integer num2;
        String str2;
        String str3;
        ?? r132;
        Integer num3;
        Headers headers;
        String str4;
        Long lBf;
        String str5;
        String str6;
        long jCurrentTimeMillis;
        Object objRefreshTokenIfNeeded;
        String str7;
        String str8;
        String str9;
        long j;
        double d;
        String str10;
        String str11;
        String str12;
        double d2;
        double d3;
        Integer num4;
        String str13;
        TraktScrobbleRepository traktScrobbleRepository;
        ContentItem contentItem;
        Integer num5;
        Integer num6;
        Integer num7;
        Integer num8;
        TraktApiService traktApiService;
        String str14;
        String str15 = "Auto-scrobbled (marked watched) at ";
        ck1 ck1Var = ck1.a;
        int i = this.label;
        String str16 = "Scrobble update returned 422 for ";
        String str17 = "";
        ?? r133 = 1;
        r133 = 1;
        String str18 = null;
        try {
            try {
                try {
                    try {
                    } catch (HttpException e) {
                        e = e;
                    }
                } catch (CancellationException e2) {
                    throw e2;
                }
            } catch (Exception e3) {
                e = e3;
            }
        } catch (HttpException e4) {
            e = e4;
        } catch (Exception e5) {
            e = e5;
            r13 = r133;
        }
        if (i == 0) {
            s38.b(obj);
            if (this.$contentItem.getItemType() == ContentItem.ItemType.TV_SHOW && !this.this$0.isValidEpisodeInfo(this.$season, this.$episode)) {
                Log.w("TraktScrobble", "Skipping progress update for TV show - invalid episode info");
                return new q38(Boxing.boxBoolean(false));
            }
            if (this.this$0.currentSessionKey == null) {
                return new q38(Boxing.boxBoolean(false));
            }
            jCurrentTimeMillis = System.currentTimeMillis();
            if (!this.$isPaused && jCurrentTimeMillis - this.this$0.lastUpdateTime < 30000) {
                return new q38(Boxing.boxBoolean(false));
            }
            if (jCurrentTimeMillis < this.this$0.rateLimitBackoffUntilMs) {
                long j2 = (this.this$0.rateLimitBackoffUntilMs - jCurrentTimeMillis) / ((long) 1000);
                return new q38(Boxing.boxBoolean(false));
            }
            TraktAccountRepository traktAccountRepository = this.this$0.accountRepository;
            this.J$0 = jCurrentTimeMillis;
            this.label = 1;
            objRefreshTokenIfNeeded = traktAccountRepository.refreshTokenIfNeeded(this);
            if (objRefreshTokenIfNeeded != ck1Var) {
            }
            return ck1Var;
        }
        if (i != 1) {
            try {
            } catch (HttpException e6) {
                e = e6;
                str6 = "TraktScrobble";
                str = "Scrobble update returned 422 for ";
                str5 = str6;
                str16 = " S";
                r132 = str5;
                if (e.code() == 429) {
                }
            } catch (Exception e7) {
                e = e7;
                str3 = "TraktScrobble";
                str18 = "Scrobble update returned 422 for ";
                str2 = str3;
                str16 = " S";
                r13 = str2;
                if (!(e instanceof HttpException)) {
                }
                if (httpException != null) {
                }
                if (numBoxInt != null) {
                    ExecutorService executorService = kq.a;
                    String title = this.$contentItem.getTitle();
                    num = this.$season;
                    if (num != null) {
                        str17 = str16 + num + "E" + num2;
                    }
                    kq.h(r13, str18 + title + str17);
                }
            }
            if (i == 2) {
                d3 = this.D$0;
                long j3 = this.J$0;
                s38.b(obj);
                str7 = "TraktScrobble";
                jCurrentTimeMillis = j3;
                this.this$0.hasScrobbled = true;
                ExecutorService executorService2 = kq.a;
                int i2 = (int) d3;
                String title2 = this.$contentItem.getTitle();
                num4 = this.$season;
                if (num4 != null || (num7 = this.$episode) == null) {
                    str13 = "";
                } else {
                    str13 = " S" + num4 + "E" + num7;
                }
                str15 = str7;
                kq.h(str15, "Auto-scrobbled (marked watched) at " + i2 + "%: " + title2 + str13);
                traktScrobbleRepository = this.this$0;
                contentItem = this.$contentItem;
                num5 = this.$season;
                num6 = this.$episode;
                this.J$0 = jCurrentTimeMillis;
                this.label = 3;
                if (traktScrobbleRepository.handleWatchedSideEffects(contentItem, num5, num6, this) == ck1Var) {
                    return ck1Var;
                }
                r133 = str15;
                str18 = "Scrobble update returned 422 for ";
                str16 = " S";
                this.this$0.lastUpdateTime = jCurrentTimeMillis;
                return new q38(Boxing.boxBoolean(true));
            }
            if (i == 3) {
                long j4 = this.J$0;
                s38.b(obj);
                jCurrentTimeMillis = j4;
                str15 = "TraktScrobble";
                r133 = str15;
                str18 = "Scrobble update returned 422 for ";
                str16 = " S";
                this.this$0.lastUpdateTime = jCurrentTimeMillis;
                return new q38(Boxing.boxBoolean(true));
            }
            if (i == 4) {
                d2 = this.D$0;
                j = this.J$0;
                s38.b(obj);
                str12 = "TraktScrobble";
                str11 = "Paused at ";
                str18 = "Scrobble update returned 422 for ";
                str16 = " S";
                Boxing.boxInt(Log.d(str12, str11 + ((int) d2) + "%"));
                str10 = str12;
                jCurrentTimeMillis = j;
                r133 = str10;
                this.this$0.lastUpdateTime = jCurrentTimeMillis;
                return new q38(Boxing.boxBoolean(true));
            }
            if (i != 5) {
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
            }
            d = this.D$0;
            j = this.J$0;
            s38.b(obj);
            str9 = "TraktScrobble";
            str8 = "Updated progress: ";
            str18 = "Scrobble update returned 422 for ";
            str16 = " S";
            Boxing.boxInt(Log.d(str9, str8 + ((int) d) + "%"));
            str10 = str9;
            jCurrentTimeMillis = j;
            r133 = str10;
            this.this$0.lastUpdateTime = jCurrentTimeMillis;
            return new q38(Boxing.boxBoolean(true));
        }
        jCurrentTimeMillis = this.J$0;
        s38.b(obj);
        objRefreshTokenIfNeeded = obj;
        TraktAccount traktAccount = (TraktAccount) objRefreshTokenIfNeeded;
        if (traktAccount == null) {
            return new q38(Boxing.boxBoolean(false));
        }
        str8 = "Updated progress: ";
        str11 = "Paused at ";
        str7 = "TraktScrobble";
        double dCalculateProgress = this.this$0.calculateProgress(this.$currentPositionMs, this.$durationMs);
        if (dCalculateProgress < 1.0d) {
            return new q38(Boxing.boxBoolean(false));
        }
        TraktScrobbleRequest traktScrobbleRequestBuildScrobbleRequest = this.this$0.buildScrobbleRequest(this.$contentItem, this.$season, this.$episode, dCalculateProgress);
        try {
            try {
                try {
                } catch (HttpException e8) {
                    e = e8;
                    str18 = "Scrobble update returned 422 for ";
                    r133 = str7;
                }
            } catch (Exception e9) {
                e = e9;
                str18 = "Scrobble update returned 422 for ";
                str2 = str7;
                str16 = " S";
                r13 = str2;
                HttpException httpException = !(e instanceof HttpException) ? (HttpException) e : null;
                numBoxInt = httpException != null ? Boxing.boxInt(httpException.code()) : null;
                if (numBoxInt != null && numBoxInt.intValue() == 422) {
                    ExecutorService executorService3 = kq.a;
                    String title3 = this.$contentItem.getTitle();
                    num = this.$season;
                    if (num != null && (num2 = this.$episode) != null) {
                        str17 = str16 + num + "E" + num2;
                    }
                    kq.h(r13, str18 + title3 + str17);
                } else {
                    Boxing.boxInt(Log.e(r13, "Failed to update progress", e));
                }
                return new q38(Boxing.boxBoolean(true));
            }
        } catch (HttpException e10) {
            e = e10;
            str = "Scrobble update returned 422 for ";
            str5 = str7;
            str16 = " S";
            r132 = str5;
            if (e.code() == 429) {
            }
        }
        if (this.this$0.hasScrobbled || dCalculateProgress < 90.0d) {
            str15 = str7;
            if (dCalculateProgress >= 80.0d) {
                Boxing.boxInt(Log.d(str15, "In 80-89% dead zone (" + ((int) dCalculateProgress) + "%), skipping API call"));
                r133 = str15;
                str18 = "Scrobble update returned 422 for ";
                str16 = " S";
                this.this$0.lastUpdateTime = jCurrentTimeMillis;
                return new q38(Boxing.boxBoolean(true));
            }
            try {
            } catch (HttpException e11) {
                e = e11;
                r133 = str15;
                str18 = "Scrobble update returned 422 for ";
                str16 = " S";
            }
            if (this.$isPaused) {
                TraktScrobbleRepository traktScrobbleRepository2 = this.this$0;
                str7 = str15;
                String accessToken = traktAccount.getAccessToken();
                ContentItem contentItem2 = this.$contentItem;
                Integer num9 = this.$season;
                try {
                    num8 = this.$episode;
                    this.J$0 = jCurrentTimeMillis;
                    this.D$0 = dCalculateProgress;
                    this.label = 4;
                    str18 = "Scrobble update returned 422 for ";
                    str16 = " S";
                    str12 = str7;
                } catch (HttpException e12) {
                    e = e12;
                    str18 = "Scrobble update returned 422 for ";
                    str16 = " S";
                    r133 = str7;
                    str = str18;
                    r132 = r133;
                    if (e.code() == 429) {
                    }
                } catch (Exception e13) {
                    e = e13;
                    str18 = "Scrobble update returned 422 for ";
                    str16 = " S";
                    r13 = str7;
                    if (!(e instanceof HttpException)) {
                    }
                    if (httpException != null) {
                    }
                    if (numBoxInt != null) {
                    }
                }
                if (traktScrobbleRepository2.sendPauseWithSessionRecovery(accessToken, traktScrobbleRequestBuildScrobbleRequest, contentItem2, num9, num8, this) != ck1Var) {
                    d2 = dCalculateProgress;
                    j = jCurrentTimeMillis;
                    Boxing.boxInt(Log.d(str12, str11 + ((int) d2) + "%"));
                    str10 = str12;
                    jCurrentTimeMillis = j;
                    r133 = str10;
                    this.this$0.lastUpdateTime = jCurrentTimeMillis;
                    return new q38(Boxing.boxBoolean(true));
                }
            } else {
                str9 = str15;
                str18 = "Scrobble update returned 422 for ";
                str16 = " S";
                TraktScrobbleRepository traktScrobbleRepository3 = this.this$0;
                String accessToken2 = traktAccount.getAccessToken();
                ContentItem contentItem3 = this.$contentItem;
                Integer num10 = this.$season;
                Integer num11 = this.$episode;
                this.J$0 = jCurrentTimeMillis;
                this.D$0 = dCalculateProgress;
                this.label = 5;
                if (traktScrobbleRepository3.sendPauseWithSessionRecovery(accessToken2, traktScrobbleRequestBuildScrobbleRequest, contentItem3, num10, num11, this) != ck1Var) {
                    d = dCalculateProgress;
                    j = jCurrentTimeMillis;
                    Boxing.boxInt(Log.d(str9, str8 + ((int) d) + "%"));
                    str10 = str9;
                    jCurrentTimeMillis = j;
                    r133 = str10;
                    this.this$0.lastUpdateTime = jCurrentTimeMillis;
                    return new q38(Boxing.boxBoolean(true));
                }
            }
            str16 = " S";
            str = str18;
            r132 = r133;
            if (e.code() == 429) {
                Response<?> response = e.response();
                long jLongValue = (response == null || (headers = response.headers()) == null || (str4 = headers.get("Retry-After")) == null || (lBf = n89.bf(str4)) == null) ? 300L : lBf.longValue();
                this.this$0.rateLimitBackoffUntilMs = (((long) 1000) * jLongValue) + System.currentTimeMillis();
                ExecutorService executorService4 = kq.a;
                kq.h(r132, "Rate limited (429), backing off for " + jLongValue + CmcdData.STREAMING_FORMAT_SS);
            } else if (e.code() == 422) {
                ExecutorService executorService5 = kq.a;
                String title4 = this.$contentItem.getTitle();
                Integer num12 = this.$season;
                if (num12 != null && (num3 = this.$episode) != null) {
                    str17 = str16 + num12 + "E" + num3;
                }
                kq.h(r132, str + title4 + str17);
            } else {
                Boxing.boxInt(Log.e(r132, "Failed to update progress: HTTP " + e.code(), e));
            }
            return new q38(Boxing.boxBoolean(true));
        }
        try {
            traktApiService = this.this$0.traktApiService;
            str14 = "Bearer " + traktAccount.getAccessToken();
            this.J$0 = jCurrentTimeMillis;
            this.D$0 = dCalculateProgress;
            this.label = 2;
        } catch (HttpException e14) {
            e = e14;
            str15 = str7;
            str6 = str15;
            str = "Scrobble update returned 422 for ";
            str5 = str6;
            str16 = " S";
            r132 = str5;
            if (e.code() == 429) {
            }
        } catch (Exception e15) {
            e = e15;
            str15 = str7;
            str3 = str15;
            str18 = "Scrobble update returned 422 for ";
            str2 = str3;
            str16 = " S";
            r13 = str2;
            if (!(e instanceof HttpException)) {
            }
            if (httpException != null) {
            }
            if (numBoxInt != null) {
            }
        }
        if (traktApiService.stopScrobble(str14, traktScrobbleRequestBuildScrobbleRequest, this) != ck1Var) {
            d3 = dCalculateProgress;
            this.this$0.hasScrobbled = true;
            ExecutorService executorService22 = kq.a;
            int i22 = (int) d3;
            String title22 = this.$contentItem.getTitle();
            num4 = this.$season;
            if (num4 != null) {
                str13 = "";
                str15 = str7;
                kq.h(str15, "Auto-scrobbled (marked watched) at " + i22 + "%: " + title22 + str13);
                traktScrobbleRepository = this.this$0;
                contentItem = this.$contentItem;
                num5 = this.$season;
                num6 = this.$episode;
                this.J$0 = jCurrentTimeMillis;
                this.label = 3;
                if (traktScrobbleRepository.handleWatchedSideEffects(contentItem, num5, num6, this) == ck1Var) {
                }
                r133 = str15;
                str18 = "Scrobble update returned 422 for ";
                str16 = " S";
                this.this$0.lastUpdateTime = jCurrentTimeMillis;
            }
            return new q38(Boxing.boxBoolean(true));
        }
        return ck1Var;
    }
}
