package defpackage;

import com.strmr.ps.data.local.entity.TraktAccount;
import com.strmr.ps.data.model.trakt.TraktTokenResponse;
import com.strmr.ps.data.model.trakt.TraktUser;
import com.strmr.ps.data.remote.api.TraktApiService;
import com.strmr.ps.data.repository.TraktAccountRepository;
import com.strmr.ps.ui.mobile.settings.MobileAccountsFragment;
import defpackage.q38;
import java.util.List;
import kotlin.Metadata;
import kotlin.coroutines.Continuation;
import kotlin.coroutines.jvm.internal.DebugMetadata;
import kotlin.coroutines.jvm.internal.SuspendLambda;
import retrofit2.HttpException;

/* 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.settings.MobileAccountsFragment$pollForTraktToken$1", f = "MobileAccountsFragment.kt", i = {0, 0, 0, 1, 3, 4, 7, 7, 7}, l = {655, 659, 660, 661, 662, 663, 685, 693}, m = "invokeSuspend", n = {"$this$launch", "expiresAt", "consecutiveServerErrors", "token", "savedAccount", "savedAccount", "$this$launch", "expiresAt", "consecutiveServerErrors"}, s = {"L$0", "J$0", "I$0", "L$0", "L$0", "L$0", "L$0", "J$0", "I$0"})
@vx8
final class cz5 extends SuspendLambda implements gr3<ak1, Continuation<? super e6a>, Object> {
    public MobileAccountsFragment a;
    public long b;
    public int c;
    public int d;
    public /* synthetic */ Object e;
    public final /* synthetic */ int f;
    public final /* synthetic */ MobileAccountsFragment i;
    public final /* synthetic */ int j;
    public final /* synthetic */ String k;

    /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
    public cz5(int i, int i2, MobileAccountsFragment mobileAccountsFragment, String str, Continuation continuation) {
        super(2, continuation);
        this.f = i;
        this.i = mobileAccountsFragment;
        this.j = i2;
        this.k = str;
    }

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

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

    /* JADX WARN: Code restructure failed: missing block: B:81:0x0191, code lost:
    
        if (defpackage.h32.a(((long) r5) * r17, r19) == r2) goto L82;
     */
    /* JADX WARN: Removed duplicated region for block: B:36:0x00bd A[Catch: all -> 0x00bb, TryCatch #1 {all -> 0x00bb, blocks: (B:30:0x00a4, B:33:0x00b8, B:36:0x00bd, B:37:0x00c0), top: B:87:0x00a4 }] */
    /* JADX WARN: Removed duplicated region for block: B:41:0x00c9  */
    /* JADX WARN: Removed duplicated region for block: B:42:0x00cb  */
    /* JADX WARN: Removed duplicated region for block: B:45:0x00d0  */
    /* JADX WARN: Removed duplicated region for block: B:53:0x00fe A[PHI: r0
      0x00fe: PHI (r0v24 java.lang.Object) = (r0v8 java.lang.Object), (r0v29 java.lang.Object) binds: [B:14:0x0053, B:51:0x00fa] A[DONT_GENERATE, DONT_INLINE]] */
    /* JADX WARN: Removed duplicated region for block: B:56:0x0113  */
    /* JADX WARN: Removed duplicated region for block: B:60:0x012a  */
    /* JADX WARN: Removed duplicated region for block: B:66:0x014e  */
    /* JADX WARN: Removed duplicated region for block: B:87:0x00a4 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX WARN: Unsupported multi-entry loop pattern (BACK_EDGE: B:81:0x0191 -> B:7:0x002b). Please report as a decompilation issue!!! */
    @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object invokeSuspend(Object obj) {
        long jCurrentTimeMillis;
        int i;
        ak1 ak1Var;
        Object objPollDeviceToken;
        long j;
        TraktTokenResponse traktTokenResponse;
        Object userProfile;
        Object objSaveDeviceToken;
        MobileAccountsFragment mobileAccountsFragment;
        TraktAccount traktAccount;
        Object allAccountsSync;
        MobileAccountsFragment mobileAccountsFragment2;
        Object objCanAddAccount;
        Object objA;
        int i2;
        TraktApiService traktApiService;
        ck1 ck1Var = ck1.a;
        int i3 = this.d;
        long j2 = 1000;
        e6a e6aVar = e6a.a;
        MobileAccountsFragment mobileAccountsFragment3 = this.i;
        switch (i3) {
            case 0:
                s38.b(obj);
                ak1 ak1Var2 = (ak1) this.e;
                jCurrentTimeMillis = (((long) this.f) * 1000) + System.currentTimeMillis();
                i = 0;
                ak1Var = ak1Var2;
                if (bk1.d(ak1Var) && System.currentTimeMillis() < jCurrentTimeMillis) {
                    String str = this.k;
                    traktApiService = mobileAccountsFragment3.l;
                    j = j2;
                    if (traktApiService != null) {
                        cr4.n("traktApiService");
                        throw null;
                    }
                    try {
                    } catch (Throwable th) {
                        th = th;
                    }
                    this.e = ak1Var;
                    this.b = jCurrentTimeMillis;
                    this.c = i;
                    this.d = 1;
                    objPollDeviceToken = traktApiService.pollDeviceToken("a380a31ce8c76681c825decf7c03e5ef1e6cce3e2657fc9289e0c3594b1a555b", "d1096df8b09eca9acb9c6bda2593833743b8106ce437b4ae6498a7d0036300ce", str, this);
                    if (objPollDeviceToken == ck1Var) {
                    }
                    objA = (TraktTokenResponse) objPollDeviceToken;
                    traktTokenResponse = (TraktTokenResponse) (objA instanceof q38.b ? null : objA);
                    if (traktTokenResponse == null) {
                        Throwable thA = q38.a(objA);
                        if (!(thA instanceof HttpException) || ((HttpException) thA).code() < 500) {
                            i2 = 0;
                        } else {
                            int i4 = i + 1;
                            if (i4 >= 5) {
                                m12 m12Var = sc2.a;
                                qm5 qm5Var = sm5.a;
                                bz5 bz5Var = new bz5(mobileAccountsFragment3, null);
                                this.e = null;
                                this.d = 7;
                                if (wj0.f(qm5Var, bz5Var, this) == ck1Var) {
                                }
                            } else {
                                i2 = i4;
                            }
                        }
                        int i5 = this.j;
                        if (i5 < 5) {
                            i5 = 5;
                        }
                        this.e = ak1Var;
                        this.b = jCurrentTimeMillis;
                        this.c = i2;
                        this.d = 8;
                        break;
                    } else {
                        TraktApiService traktApiService2 = mobileAccountsFragment3.l;
                        if (traktApiService2 == null) {
                            cr4.n("traktApiService");
                            throw null;
                        }
                        String strN = n41.n("Bearer ", traktTokenResponse.getAccessToken());
                        this.e = traktTokenResponse;
                        this.d = 2;
                        userProfile = traktApiService2.getUserProfile(strN, this);
                        if (userProfile != ck1Var) {
                            TraktAccountRepository traktAccountRepositoryZ = mobileAccountsFragment3.z();
                            this.e = null;
                            this.d = 3;
                            objSaveDeviceToken = traktAccountRepositoryZ.saveDeviceToken(traktTokenResponse, (TraktUser) userProfile, this);
                            if (objSaveDeviceToken != ck1Var) {
                                TraktAccount traktAccount2 = (TraktAccount) objSaveDeviceToken;
                                TraktAccountRepository traktAccountRepositoryZ2 = mobileAccountsFragment3.z();
                                this.e = traktAccount2;
                                this.a = mobileAccountsFragment3;
                                this.d = 4;
                                allAccountsSync = traktAccountRepositoryZ2.getAllAccountsSync(this);
                                if (allAccountsSync != ck1Var) {
                                    traktAccount = traktAccount2;
                                    mobileAccountsFragment = mobileAccountsFragment3;
                                    mobileAccountsFragment.x = (List) allAccountsSync;
                                    TraktAccountRepository traktAccountRepositoryZ3 = mobileAccountsFragment3.z();
                                    this.e = traktAccount;
                                    this.a = mobileAccountsFragment3;
                                    this.d = 5;
                                    objCanAddAccount = traktAccountRepositoryZ3.canAddAccount(this);
                                    if (objCanAddAccount != ck1Var) {
                                        mobileAccountsFragment2 = mobileAccountsFragment3;
                                        mobileAccountsFragment2.ah = ((Boolean) objCanAddAccount).booleanValue();
                                        m12 m12Var2 = sc2.a;
                                        qm5 qm5Var2 = sm5.a;
                                        az5 az5Var = new az5(mobileAccountsFragment3, traktAccount, null);
                                        this.e = null;
                                        this.a = null;
                                        this.d = 6;
                                        if (wj0.f(qm5Var2, az5Var, this) != ck1Var) {
                                        }
                                    }
                                }
                            }
                        }
                    }
                    th = th;
                    objA = s38.a(th);
                    traktTokenResponse = (TraktTokenResponse) (objA instanceof q38.b ? null : objA);
                    if (traktTokenResponse == null) {
                    }
                }
            case 1:
                i = this.c;
                jCurrentTimeMillis = this.b;
                ak1Var = (ak1) this.e;
                try {
                    s38.b(obj);
                    objPollDeviceToken = obj;
                    j = 1000;
                } catch (Throwable th2) {
                    th = th2;
                    j = j2;
                    objA = s38.a(th);
                    traktTokenResponse = (TraktTokenResponse) (objA instanceof q38.b ? null : objA);
                    if (traktTokenResponse == null) {
                    }
                }
                objA = (TraktTokenResponse) objPollDeviceToken;
                traktTokenResponse = (TraktTokenResponse) (objA instanceof q38.b ? null : objA);
                if (traktTokenResponse == null) {
                }
            case 2:
                TraktTokenResponse traktTokenResponse2 = (TraktTokenResponse) this.e;
                s38.b(obj);
                traktTokenResponse = traktTokenResponse2;
                userProfile = obj;
                TraktAccountRepository traktAccountRepositoryZ4 = mobileAccountsFragment3.z();
                this.e = null;
                this.d = 3;
                objSaveDeviceToken = traktAccountRepositoryZ4.saveDeviceToken(traktTokenResponse, (TraktUser) userProfile, this);
                if (objSaveDeviceToken != ck1Var) {
                }
            case 3:
                s38.b(obj);
                objSaveDeviceToken = obj;
                TraktAccount traktAccount22 = (TraktAccount) objSaveDeviceToken;
                TraktAccountRepository traktAccountRepositoryZ22 = mobileAccountsFragment3.z();
                this.e = traktAccount22;
                this.a = mobileAccountsFragment3;
                this.d = 4;
                allAccountsSync = traktAccountRepositoryZ22.getAllAccountsSync(this);
                if (allAccountsSync != ck1Var) {
                }
            case 4:
                mobileAccountsFragment = this.a;
                TraktAccount traktAccount3 = (TraktAccount) this.e;
                s38.b(obj);
                traktAccount = traktAccount3;
                allAccountsSync = obj;
                mobileAccountsFragment.x = (List) allAccountsSync;
                TraktAccountRepository traktAccountRepositoryZ32 = mobileAccountsFragment3.z();
                this.e = traktAccount;
                this.a = mobileAccountsFragment3;
                this.d = 5;
                objCanAddAccount = traktAccountRepositoryZ32.canAddAccount(this);
                if (objCanAddAccount != ck1Var) {
                }
            case 5:
                MobileAccountsFragment mobileAccountsFragment4 = this.a;
                TraktAccount traktAccount4 = (TraktAccount) this.e;
                s38.b(obj);
                traktAccount = traktAccount4;
                mobileAccountsFragment2 = mobileAccountsFragment4;
                objCanAddAccount = obj;
                mobileAccountsFragment2.ah = ((Boolean) objCanAddAccount).booleanValue();
                m12 m12Var22 = sc2.a;
                qm5 qm5Var22 = sm5.a;
                az5 az5Var2 = new az5(mobileAccountsFragment3, traktAccount, null);
                this.e = null;
                this.a = null;
                this.d = 6;
                return wj0.f(qm5Var22, az5Var2, this) != ck1Var ? e6aVar : ck1Var;
            case 6:
                s38.b(obj);
                return e6aVar;
            case 7:
                s38.b(obj);
                return e6aVar;
            case 8:
                i2 = this.c;
                long j3 = this.b;
                ak1 ak1Var3 = (ak1) this.e;
                s38.b(obj);
                j = 1000;
                ak1Var = ak1Var3;
                jCurrentTimeMillis = j3;
                i = i2;
                j2 = j;
                if (bk1.d(ak1Var)) {
                    String str2 = this.k;
                    traktApiService = mobileAccountsFragment3.l;
                    j = j2;
                    if (traktApiService != null) {
                    }
                    th = th;
                    objA = s38.a(th);
                    traktTokenResponse = (TraktTokenResponse) (objA instanceof q38.b ? null : objA);
                    if (traktTokenResponse == null) {
                    }
                }
            default:
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
        }
    }
}
