package com.strmr.ps.data.repository;

import android.util.Log;
import androidx.media3.container.NalUnitUtil;
import com.strmr.ps.data.local.dao.EasyDebridAccountDao;
import com.strmr.ps.data.local.entity.EasyDebridAccount;
import com.strmr.ps.data.local.entity.PlaybackProgress;
import com.strmr.ps.data.model.DebridError;
import com.strmr.ps.data.model.DebridProvider;
import com.strmr.ps.data.remote.api.EasyDebridApiService;
import com.strmr.ps.data.remote.model.easydebrid.EasyDebridGenerateRequest;
import com.strmr.ps.data.remote.model.easydebrid.EasyDebridGenerateResponse;
import com.strmr.ps.data.remote.model.easydebrid.EasyDebridGeneratedFile;
import com.strmr.ps.data.remote.model.easydebrid.EasyDebridLookupItem;
import com.strmr.ps.data.remote.model.easydebrid.EasyDebridLookupRequest;
import com.strmr.ps.data.remote.model.easydebrid.EasyDebridLookupResponse;
import com.strmr.ps.data.remote.model.easydebrid.EasyDebridUserResponse;
import com.strmr.ps.data.supabase.DeviceManager;
import com.strmr.ps.data.supabase.SettingsSyncManager;
import dagger.Lazy;
import defpackage.ak1;
import defpackage.bk1;
import defpackage.ck1;
import defpackage.cr4;
import defpackage.dr4;
import defpackage.e6a;
import defpackage.ik2;
import defpackage.kp6;
import defpackage.m02;
import defpackage.m12;
import defpackage.n89;
import defpackage.pq6;
import defpackage.q07;
import defpackage.q38;
import defpackage.qo9;
import defpackage.s38;
import defpackage.sc2;
import defpackage.vo2;
import defpackage.vw2;
import defpackage.vx8;
import defpackage.wo5;
import defpackage.y41;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.inject.Inject;
import javax.inject.Singleton;
import kotlin.Metadata;
import kotlin.coroutines.Continuation;
import kotlin.coroutines.jvm.internal.Boxing;
import kotlin.coroutines.jvm.internal.ContinuationImpl;
import kotlin.coroutines.jvm.internal.DebugMetadata;
import net.zetetic.database.DatabaseUtils;

/* JADX INFO: loaded from: classes3.dex */
@Singleton
@Metadata(d1 = {"\u0000p\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u000b\n\u0000\n\u0002\u0010\u000e\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0007\n\u0002\u0010 \n\u0000\n\u0002\u0010$\n\u0002\b\u0003\n\u0002\u0010\b\n\u0002\b\f\n\u0002\u0018\u0002\n\u0002\b\b\n\u0002\u0018\u0002\n\u0002\b\u0004\b\u0007\u0018\u0000 >2\u00020\u0001:\u0001>B5\b\u0007\u0012\u0006\u0010\u0003\u001a\u00020\u0002\u0012\u0006\u0010\u0005\u001a\u00020\u0004\u0012\f\u0010\b\u001a\b\u0012\u0004\u0012\u00020\u00070\u0006\u0012\f\u0010\n\u001a\b\u0012\u0004\u0012\u00020\t0\u0006¢\u0006\u0004\b\u000b\u0010\fJ\u0010\u0010\u000e\u001a\u00020\rH\u0082@¢\u0006\u0004\b\u000e\u0010\u000fJ\u0012\u0010\u0011\u001a\u0004\u0018\u00010\u0010H\u0086@¢\u0006\u0004\b\u0011\u0010\u000fJ\u0010\u0010\u0013\u001a\u00020\u0012H\u0096@¢\u0006\u0004\b\u0013\u0010\u000fJ\u0012\u0010\u0015\u001a\u0004\u0018\u00010\u0014H\u0096@¢\u0006\u0004\b\u0015\u0010\u000fJ\u001e\u0010\u001a\u001a\b\u0012\u0004\u0012\u00020\u00100\u00172\u0006\u0010\u0016\u001a\u00020\u0014H\u0086@¢\u0006\u0004\b\u0018\u0010\u0019J\u0016\u0010\u001c\u001a\b\u0012\u0004\u0012\u00020\u00100\u0017H\u0086@¢\u0006\u0004\b\u001b\u0010\u000fJ\u0010\u0010\u001d\u001a\u00020\rH\u0086@¢\u0006\u0004\b\u001d\u0010\u000fJ\u0010\u0010\u001e\u001a\u00020\rH\u0096@¢\u0006\u0004\b\u001e\u0010\u000fJ2\u0010$\u001a\u0016\u0012\u0012\u0012\u0010\u0012\u0004\u0012\u00020\u0014\u0012\u0006\u0012\u0004\u0018\u00010\u00120!0\u00172\f\u0010 \u001a\b\u0012\u0004\u0012\u00020\u00140\u001fH\u0096@¢\u0006\u0004\b\"\u0010#J\u0012\u0010&\u001a\u0004\u0018\u00010%H\u0096@¢\u0006\u0004\b&\u0010\u000fJF\u0010.\u001a\b\u0012\u0004\u0012\u00020\u00140\u00172\u0006\u0010'\u001a\u00020\u00142\b\u0010(\u001a\u0004\u0018\u00010%2\b\u0010)\u001a\u0004\u0018\u00010%2\b\u0010*\u001a\u0004\u0018\u00010%2\b\u0010+\u001a\u0004\u0018\u00010\u0014H\u0096@¢\u0006\u0004\b,\u0010-R\u0014\u0010\u0003\u001a\u00020\u00028\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b\u0003\u0010/R\u0014\u0010\u0005\u001a\u00020\u00048\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b\u0005\u00100R\u001a\u0010\b\u001a\b\u0012\u0004\u0012\u00020\u00070\u00068\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b\b\u00101R\u001a\u0010\n\u001a\b\u0012\u0004\u0012\u00020\t0\u00068\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b\n\u00101R\u001a\u00103\u001a\u0002028\u0016X\u0096\u0004¢\u0006\f\n\u0004\b3\u00104\u001a\u0004\b5\u00106R\u001a\u00107\u001a\u00020\u00148\u0016X\u0096D¢\u0006\f\n\u0004\b7\u00108\u001a\u0004\b9\u0010:R\u0014\u0010<\u001a\u00020;8\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b<\u0010=¨\u0006?"}, d2 = {"Lcom/strmr/ps/data/repository/EasyDebridRepository;", "Lcom/strmr/ps/data/repository/IDebridProvider;", "Lcom/strmr/ps/data/remote/api/EasyDebridApiService;", "easyDebridApiService", "Lcom/strmr/ps/data/local/dao/EasyDebridAccountDao;", "accountDao", "Ldagger/Lazy;", "Lcom/strmr/ps/data/supabase/SettingsSyncManager;", "settingsSyncManager", "Lcom/strmr/ps/data/supabase/DeviceManager;", "deviceManager", "<init>", "(Lcom/strmr/ps/data/remote/api/EasyDebridApiService;Lcom/strmr/ps/data/local/dao/EasyDebridAccountDao;Ldagger/Lazy;Ldagger/Lazy;)V", "Le6a;", "triggerCloudSync", "(Lkotlin/coroutines/Continuation;)Ljava/lang/Object;", "Lcom/strmr/ps/data/local/entity/EasyDebridAccount;", "getAccount", "", "hasAccount", "", "getApiKey", "apiKey", "Lq38;", "verifyAndSaveApiKey-gIAlu-s", "(Ljava/lang/String;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;", "verifyAndSaveApiKey", "refreshAccountInfo-IoAF18A", "refreshAccountInfo", "logout", "clearAccount", "", "hashes", "", "checkCacheStatus-gIAlu-s", "(Ljava/util/List;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;", "checkCacheStatus", "", "getAccountExpirationStatus", "magnetOrHash", "fileIdx", "season", PlaybackProgress.TYPE_EPISODE, "airDate", "resolveToDirectLink-hUnOzRk", "(Ljava/lang/String;Ljava/lang/Integer;Ljava/lang/Integer;Ljava/lang/Integer;Ljava/lang/String;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;", "resolveToDirectLink", "Lcom/strmr/ps/data/remote/api/EasyDebridApiService;", "Lcom/strmr/ps/data/local/dao/EasyDebridAccountDao;", "Ldagger/Lazy;", "Lcom/strmr/ps/data/model/DebridProvider;", "provider", "Lcom/strmr/ps/data/model/DebridProvider;", "getProvider", "()Lcom/strmr/ps/data/model/DebridProvider;", "serviceName", "Ljava/lang/String;", "getServiceName", "()Ljava/lang/String;", "Lak1;", "syncScope", "Lak1;", "Companion", "app_sideloadRelease"}, k = 1, mv = {2, 0, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
@vx8
public final class EasyDebridRepository implements IDebridProvider {

    @kp6
    private static final String PROVIDER_ID = "easydebrid";

    @kp6
    private static final String TAG = "EasyDebridRepository";

    @kp6
    private final EasyDebridAccountDao accountDao;

    @kp6
    private final Lazy<DeviceManager> deviceManager;

    @kp6
    private final EasyDebridApiService easyDebridApiService;

    @kp6
    private final DebridProvider provider;

    @kp6
    private final String serviceName;

    @kp6
    private final Lazy<SettingsSyncManager> settingsSyncManager;

    @kp6
    private final ak1 syncScope;

    /* JADX INFO: renamed from: com.strmr.ps.data.repository.EasyDebridRepository$getAccountExpirationStatus$1, reason: invalid class name */
    @Metadata(k = 3, mv = {2, 0, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
    @DebugMetadata(c = "com.strmr.ps.data.repository.EasyDebridRepository", f = "EasyDebridRepository.kt", i = {}, l = {154}, m = "getAccountExpirationStatus", n = {}, s = {})
    public static final class AnonymousClass1 extends ContinuationImpl {
        int label;
        /* synthetic */ Object result;

        public AnonymousClass1(Continuation<? super AnonymousClass1> continuation) {
            super(continuation);
        }

        @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl
        @pq6
        public final Object invokeSuspend(@kp6 Object obj) {
            this.result = obj;
            this.label |= Integer.MIN_VALUE;
            return EasyDebridRepository.this.getAccountExpirationStatus(this);
        }
    }

    /* JADX INFO: renamed from: com.strmr.ps.data.repository.EasyDebridRepository$getApiKey$1, reason: invalid class name and case insensitive filesystem */
    @Metadata(k = 3, mv = {2, 0, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
    @DebugMetadata(c = "com.strmr.ps.data.repository.EasyDebridRepository", f = "EasyDebridRepository.kt", i = {}, l = {50}, m = "getApiKey", n = {}, s = {})
    public static final class C03211 extends ContinuationImpl {
        int label;
        /* synthetic */ Object result;

        public C03211(Continuation<? super C03211> continuation) {
            super(continuation);
        }

        @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl
        @pq6
        public final Object invokeSuspend(@kp6 Object obj) {
            this.result = obj;
            this.label |= Integer.MIN_VALUE;
            return EasyDebridRepository.this.getApiKey(this);
        }
    }

    /* JADX INFO: renamed from: com.strmr.ps.data.repository.EasyDebridRepository$hasAccount$1, reason: invalid class name and case insensitive filesystem */
    @Metadata(k = 3, mv = {2, 0, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
    @DebugMetadata(c = "com.strmr.ps.data.repository.EasyDebridRepository", f = "EasyDebridRepository.kt", i = {}, l = {NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED}, m = "hasAccount", n = {}, s = {})
    public static final class C03221 extends ContinuationImpl {
        int label;
        /* synthetic */ Object result;

        public C03221(Continuation<? super C03221> continuation) {
            super(continuation);
        }

        @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl
        @pq6
        public final Object invokeSuspend(@kp6 Object obj) {
            this.result = obj;
            this.label |= Integer.MIN_VALUE;
            return EasyDebridRepository.this.hasAccount(this);
        }
    }

    /* JADX INFO: renamed from: com.strmr.ps.data.repository.EasyDebridRepository$logout$1, reason: invalid class name and case insensitive filesystem */
    @Metadata(k = 3, mv = {2, 0, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
    @DebugMetadata(c = "com.strmr.ps.data.repository.EasyDebridRepository", f = "EasyDebridRepository.kt", i = {0}, l = {DatabaseUtils.STATEMENT_OTHER, 102}, m = "logout", n = {"this"}, s = {"L$0"})
    public static final class C03231 extends ContinuationImpl {
        Object L$0;
        int label;
        /* synthetic */ Object result;

        public C03231(Continuation<? super C03231> continuation) {
            super(continuation);
        }

        @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl
        @pq6
        public final Object invokeSuspend(@kp6 Object obj) {
            this.result = obj;
            this.label |= Integer.MIN_VALUE;
            return EasyDebridRepository.this.logout(this);
        }
    }

    /* JADX INFO: renamed from: com.strmr.ps.data.repository.EasyDebridRepository$triggerCloudSync$1, reason: invalid class name and case insensitive filesystem */
    @Metadata(k = 3, mv = {2, 0, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
    @DebugMetadata(c = "com.strmr.ps.data.repository.EasyDebridRepository", f = "EasyDebridRepository.kt", i = {}, l = {311}, m = "triggerCloudSync", n = {}, s = {})
    public static final class C03241 extends ContinuationImpl {
        int label;
        /* synthetic */ Object result;

        public C03241(Continuation<? super C03241> continuation) {
            super(continuation);
        }

        @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl
        @pq6
        public final Object invokeSuspend(@kp6 Object obj) {
            this.result = obj;
            this.label |= Integer.MIN_VALUE;
            return EasyDebridRepository.this.triggerCloudSync(this);
        }
    }

    @Inject
    public EasyDebridRepository(@kp6 EasyDebridApiService easyDebridApiService, @kp6 EasyDebridAccountDao easyDebridAccountDao, @kp6 Lazy<SettingsSyncManager> lazy, @kp6 Lazy<DeviceManager> lazy2) {
        cr4.g(easyDebridApiService, "easyDebridApiService");
        cr4.g(easyDebridAccountDao, "accountDao");
        cr4.g(lazy, "settingsSyncManager");
        cr4.g(lazy2, "deviceManager");
        this.easyDebridApiService = easyDebridApiService;
        this.accountDao = easyDebridAccountDao;
        this.settingsSyncManager = lazy;
        this.deviceManager = lazy2;
        this.provider = DebridProvider.EASY_DEBRID;
        this.serviceName = "easydebrid";
        m12 m12Var = sc2.a;
        this.syncScope = bk1.a(m02.c);
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* JADX WARN: Removed duplicated region for block: B:7:0x0013  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object triggerCloudSync(Continuation<? super e6a> continuation) {
        C03241 c03241;
        if (continuation instanceof C03241) {
            c03241 = (C03241) continuation;
            int i = c03241.label;
            if ((i & Integer.MIN_VALUE) != 0) {
                c03241.label = i - Integer.MIN_VALUE;
            } else {
                c03241 = new C03241(continuation);
            }
        }
        Object objC = c03241.result;
        ck1 ck1Var = ck1.a;
        int i2 = c03241.label;
        e6a e6aVar = e6a.a;
        try {
            if (i2 == 0) {
                s38.b(objC);
                String strE = this.deviceManager.get().e();
                if (strE == null) {
                    Log.w(TAG, "Skipping cloud sync: device not linked (no access token)");
                    return e6aVar;
                }
                EasyDebridRepository$triggerCloudSync$result$1 easyDebridRepository$triggerCloudSync$result$1 = new EasyDebridRepository$triggerCloudSync$result$1(this, strE, null);
                c03241.label = 1;
                objC = qo9.c(5000L, easyDebridRepository$triggerCloudSync$result$1, c03241);
                if (objC == ck1Var) {
                    return ck1Var;
                }
            } else {
                if (i2 != 1) {
                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                }
                s38.b(objC);
            }
            if (cr4.b((Boolean) objC, Boxing.boxBoolean(true))) {
                Log.w(TAG, "EasyDebrid cloud sync completed successfully");
            } else {
                Log.w(TAG, "EasyDebrid cloud sync timed out or returned false — pending deletion flag preserved");
            }
        } catch (Exception e) {
            Log.w(TAG, "EasyDebrid cloud sync failed — pending deletion flag preserved", e);
        }
        return e6aVar;
    }

    /* JADX WARN: Code restructure failed: missing block: B:33:0x00b1, code lost:
    
        if (r14 == r1) goto L34;
     */
    /* JADX WARN: Removed duplicated region for block: B:7:0x0013  */
    @Override // com.strmr.ps.data.repository.IDebridProvider
    @pq6
    /* JADX INFO: renamed from: checkCacheStatus-gIAlu-s */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public Object mo66checkCacheStatusgIAlus(@kp6 List<String> list, @kp6 Continuation<? super q38<? extends Map<String, Boolean>>> continuation) {
        EasyDebridRepository$checkCacheStatus$1 easyDebridRepository$checkCacheStatus$1;
        EasyDebridRepository easyDebridRepository;
        if (continuation instanceof EasyDebridRepository$checkCacheStatus$1) {
            easyDebridRepository$checkCacheStatus$1 = (EasyDebridRepository$checkCacheStatus$1) continuation;
            int i = easyDebridRepository$checkCacheStatus$1.label;
            if ((i & Integer.MIN_VALUE) != 0) {
                easyDebridRepository$checkCacheStatus$1.label = i - Integer.MIN_VALUE;
            } else {
                easyDebridRepository$checkCacheStatus$1 = new EasyDebridRepository$checkCacheStatus$1(this, continuation);
            }
        }
        Object account = easyDebridRepository$checkCacheStatus$1.result;
        ck1 ck1Var = ck1.a;
        int i2 = easyDebridRepository$checkCacheStatus$1.label;
        try {
            if (i2 == 0) {
                s38.b(account);
                EasyDebridAccountDao easyDebridAccountDao = this.accountDao;
                easyDebridRepository$checkCacheStatus$1.L$0 = this;
                easyDebridRepository$checkCacheStatus$1.L$1 = list;
                easyDebridRepository$checkCacheStatus$1.label = 1;
                account = easyDebridAccountDao.getAccount("easydebrid", easyDebridRepository$checkCacheStatus$1);
                if (account != ck1Var) {
                    easyDebridRepository = this;
                }
                return ck1Var;
            }
            if (i2 != 1) {
                if (i2 != 2) {
                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                }
                list = (List) easyDebridRepository$checkCacheStatus$1.L$0;
                s38.b(account);
                EasyDebridLookupResponse easyDebridLookupResponse = (EasyDebridLookupResponse) account;
                if (easyDebridLookupResponse.getError() != null) {
                    Log.w(TAG, "Cache check failed: " + easyDebridLookupResponse.getError());
                    return s38.a(new Exception(easyDebridLookupResponse.getError()));
                }
                List<EasyDebridLookupItem> result = easyDebridLookupResponse.getResult();
                if (result == null) {
                    result = ik2.a;
                }
                List<String> list2 = list;
                ArrayList arrayList = new ArrayList(y41.p(list2, 10));
                int i3 = 0;
                for (Object obj : list2) {
                    int i4 = i3 + 1;
                    if (i3 < 0) {
                        y41.bm();
                        throw null;
                    }
                    String str = (String) obj;
                    EasyDebridLookupItem easyDebridLookupItem = (EasyDebridLookupItem) y41.af(i3, result);
                    boolean cached = easyDebridLookupItem != null ? easyDebridLookupItem.getCached() : false;
                    String lowerCase = str.toLowerCase(Locale.ROOT);
                    cr4.f(lowerCase, "toLowerCase(...)");
                    arrayList.add(new q07(lowerCase, Boxing.boxBoolean(cached)));
                    i3 = i4;
                }
                Map mapM = wo5.m(arrayList);
                if (!mapM.isEmpty()) {
                    Iterator it = mapM.entrySet().iterator();
                    while (it.hasNext()) {
                        ((Boolean) ((Map.Entry) it.next()).getValue()).getClass();
                    }
                }
                list.size();
                return mapM;
            }
            list = (List) easyDebridRepository$checkCacheStatus$1.L$1;
            easyDebridRepository = (EasyDebridRepository) easyDebridRepository$checkCacheStatus$1.L$0;
            s38.b(account);
            EasyDebridAccount easyDebridAccount = (EasyDebridAccount) account;
            if (easyDebridAccount == null) {
                return vw2.d("No EasyDebrid account configured");
            }
            List<String> list3 = list;
            ArrayList arrayList2 = new ArrayList(y41.p(list3, 10));
            Iterator<T> it2 = list3.iterator();
            while (it2.hasNext()) {
                arrayList2.add("magnet:?xt=urn:btih:" + ((String) it2.next()));
            }
            EasyDebridLookupRequest easyDebridLookupRequest = new EasyDebridLookupRequest(arrayList2);
            EasyDebridApiService easyDebridApiService = easyDebridRepository.easyDebridApiService;
            String authHeader = easyDebridAccount.getAuthHeader();
            easyDebridRepository$checkCacheStatus$1.L$0 = list;
            easyDebridRepository$checkCacheStatus$1.L$1 = null;
            easyDebridRepository$checkCacheStatus$1.label = 2;
            account = easyDebridApiService.lookupLinks(authHeader, easyDebridLookupRequest, easyDebridRepository$checkCacheStatus$1);
        } catch (Exception e) {
            Log.e(TAG, "Failed to check cache status", e);
            return s38.a(e);
        }
    }

    @Override // com.strmr.ps.data.repository.IDebridProvider
    @pq6
    public Object clearAccount(@kp6 Continuation<? super e6a> continuation) {
        Object objLogout = logout(continuation);
        return objLogout == ck1.a ? objLogout : e6a.a;
    }

    @pq6
    public final Object getAccount(@kp6 Continuation<? super EasyDebridAccount> continuation) {
        return this.accountDao.getAccount("easydebrid", continuation);
    }

    /* JADX WARN: Removed duplicated region for block: B:7:0x0013  */
    @Override // com.strmr.ps.data.repository.IDebridProvider
    @pq6
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public Object getAccountExpirationStatus(@kp6 Continuation<? super Integer> continuation) {
        AnonymousClass1 anonymousClass1;
        Long paidUntil;
        if (continuation instanceof AnonymousClass1) {
            anonymousClass1 = (AnonymousClass1) continuation;
            int i = anonymousClass1.label;
            if ((i & Integer.MIN_VALUE) != 0) {
                anonymousClass1.label = i - Integer.MIN_VALUE;
            } else {
                anonymousClass1 = new AnonymousClass1(continuation);
            }
        }
        Object account = anonymousClass1.result;
        ck1 ck1Var = ck1.a;
        int i2 = anonymousClass1.label;
        if (i2 == 0) {
            s38.b(account);
            EasyDebridAccountDao easyDebridAccountDao = this.accountDao;
            anonymousClass1.label = 1;
            account = easyDebridAccountDao.getAccount("easydebrid", anonymousClass1);
            if (account == ck1Var) {
                return ck1Var;
            }
        } else {
            if (i2 != 1) {
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
            }
            s38.b(account);
        }
        EasyDebridAccount easyDebridAccount = (EasyDebridAccount) account;
        if (easyDebridAccount != null && (paidUntil = easyDebridAccount.getPaidUntil()) != null) {
            long jLongValue = paidUntil.longValue();
            long jCurrentTimeMillis = System.currentTimeMillis() / ((long) 1000);
            if (jLongValue < jCurrentTimeMillis) {
                return Boxing.boxInt((int) ((jCurrentTimeMillis - jLongValue) / ((long) 86400)));
            }
        }
        return null;
    }

    /* JADX WARN: Removed duplicated region for block: B:7:0x0013  */
    @Override // com.strmr.ps.data.repository.IDebridProvider
    @pq6
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public Object getApiKey(@kp6 Continuation<? super String> continuation) {
        C03211 c03211;
        if (continuation instanceof C03211) {
            c03211 = (C03211) continuation;
            int i = c03211.label;
            if ((i & Integer.MIN_VALUE) != 0) {
                c03211.label = i - Integer.MIN_VALUE;
            } else {
                c03211 = new C03211(continuation);
            }
        }
        Object account = c03211.result;
        ck1 ck1Var = ck1.a;
        int i2 = c03211.label;
        if (i2 == 0) {
            s38.b(account);
            EasyDebridAccountDao easyDebridAccountDao = this.accountDao;
            c03211.label = 1;
            account = easyDebridAccountDao.getAccount("easydebrid", c03211);
            if (account == ck1Var) {
                return ck1Var;
            }
        } else {
            if (i2 != 1) {
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
            }
            s38.b(account);
        }
        EasyDebridAccount easyDebridAccount = (EasyDebridAccount) account;
        if (easyDebridAccount != null) {
            return easyDebridAccount.getApiKey();
        }
        return null;
    }

    @Override // com.strmr.ps.data.repository.IDebridProvider
    @kp6
    public DebridProvider getProvider() {
        return this.provider;
    }

    @Override // com.strmr.ps.data.repository.IDebridProvider
    @kp6
    public String getServiceName() {
        return this.serviceName;
    }

    /* JADX WARN: Removed duplicated region for block: B:7:0x0013  */
    @Override // com.strmr.ps.data.repository.IDebridProvider
    @pq6
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public Object hasAccount(@kp6 Continuation<? super Boolean> continuation) {
        C03221 c03221;
        if (continuation instanceof C03221) {
            c03221 = (C03221) continuation;
            int i = c03221.label;
            if ((i & Integer.MIN_VALUE) != 0) {
                c03221.label = i - Integer.MIN_VALUE;
            } else {
                c03221 = new C03221(continuation);
            }
        }
        Object account = c03221.result;
        ck1 ck1Var = ck1.a;
        int i2 = c03221.label;
        if (i2 == 0) {
            s38.b(account);
            EasyDebridAccountDao easyDebridAccountDao = this.accountDao;
            c03221.label = 1;
            account = easyDebridAccountDao.getAccount("easydebrid", c03221);
            if (account == ck1Var) {
                return ck1Var;
            }
        } else {
            if (i2 != 1) {
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
            }
            s38.b(account);
        }
        return Boxing.boxBoolean(account != null);
    }

    /* JADX WARN: Code restructure failed: missing block: B:21:0x0061, code lost:
    
        if (r2.triggerCloudSync(r0) == r1) goto L22;
     */
    /* JADX WARN: Removed duplicated region for block: B:7:0x0013  */
    @pq6
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object logout(@kp6 Continuation<? super e6a> continuation) {
        C03231 c03231;
        EasyDebridRepository easyDebridRepository;
        if (continuation instanceof C03231) {
            c03231 = (C03231) continuation;
            int i = c03231.label;
            if ((i & Integer.MIN_VALUE) != 0) {
                c03231.label = i - Integer.MIN_VALUE;
            } else {
                c03231 = new C03231(continuation);
            }
        }
        Object obj = c03231.result;
        ck1 ck1Var = ck1.a;
        int i2 = c03231.label;
        if (i2 == 0) {
            s38.b(obj);
            EasyDebridAccountDao easyDebridAccountDao = this.accountDao;
            c03231.L$0 = this;
            c03231.label = 1;
            if (easyDebridAccountDao.deleteAccount("easydebrid", c03231) != ck1Var) {
                easyDebridRepository = this;
            }
            return ck1Var;
        }
        if (i2 != 1) {
            if (i2 != 2) {
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
            }
            s38.b(obj);
            return e6a.a;
        }
        easyDebridRepository = (EasyDebridRepository) c03231.L$0;
        s38.b(obj);
        easyDebridRepository.settingsSyncManager.get().e("easydebrid");
        c03231.L$0 = null;
        c03231.label = 2;
    }

    /* JADX WARN: Removed duplicated region for block: B:7:0x0013  */
    @pq6
    /* JADX INFO: renamed from: refreshAccountInfo-IoAF18A, reason: not valid java name */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object m104refreshAccountInfoIoAF18A(@kp6 Continuation<? super q38<EasyDebridAccount>> continuation) {
        EasyDebridRepository$refreshAccountInfo$1 easyDebridRepository$refreshAccountInfo$1;
        EasyDebridRepository easyDebridRepository;
        if (continuation instanceof EasyDebridRepository$refreshAccountInfo$1) {
            easyDebridRepository$refreshAccountInfo$1 = (EasyDebridRepository$refreshAccountInfo$1) continuation;
            int i = easyDebridRepository$refreshAccountInfo$1.label;
            if ((i & Integer.MIN_VALUE) != 0) {
                easyDebridRepository$refreshAccountInfo$1.label = i - Integer.MIN_VALUE;
            } else {
                easyDebridRepository$refreshAccountInfo$1 = new EasyDebridRepository$refreshAccountInfo$1(this, continuation);
            }
        }
        Object account = easyDebridRepository$refreshAccountInfo$1.result;
        ck1 ck1Var = ck1.a;
        int i2 = easyDebridRepository$refreshAccountInfo$1.label;
        if (i2 == 0) {
            s38.b(account);
            EasyDebridAccountDao easyDebridAccountDao = this.accountDao;
            easyDebridRepository$refreshAccountInfo$1.L$0 = this;
            easyDebridRepository$refreshAccountInfo$1.label = 1;
            account = easyDebridAccountDao.getAccount("easydebrid", easyDebridRepository$refreshAccountInfo$1);
            if (account != ck1Var) {
                easyDebridRepository = this;
            }
        }
        if (i2 != 1) {
            if (i2 != 2) {
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
            }
            s38.b(account);
            return ((q38) account).a;
        }
        easyDebridRepository = (EasyDebridRepository) easyDebridRepository$refreshAccountInfo$1.L$0;
        s38.b(account);
        EasyDebridAccount easyDebridAccount = (EasyDebridAccount) account;
        if (easyDebridAccount == null) {
            return vw2.d("No EasyDebrid account configured");
        }
        String apiKey = easyDebridAccount.getApiKey();
        easyDebridRepository$refreshAccountInfo$1.L$0 = null;
        easyDebridRepository$refreshAccountInfo$1.label = 2;
        Object objM105verifyAndSaveApiKeygIAlus = easyDebridRepository.m105verifyAndSaveApiKeygIAlus(apiKey, easyDebridRepository$refreshAccountInfo$1);
        return objM105verifyAndSaveApiKeygIAlus == ck1Var ? ck1Var : objM105verifyAndSaveApiKeygIAlus;
    }

    /* JADX WARN: Removed duplicated region for block: B:40:0x0104  */
    /* JADX WARN: Removed duplicated region for block: B:42:0x0126 A[Catch: Exception -> 0x004c, UnknownHostException -> 0x004f, SocketTimeoutException -> 0x0052, TRY_ENTER, TryCatch #2 {SocketTimeoutException -> 0x0052, UnknownHostException -> 0x004f, Exception -> 0x004c, blocks: (B:14:0x0046, B:50:0x015c, B:53:0x0166, B:55:0x018e, B:58:0x0197, B:60:0x019f, B:63:0x01a8, B:65:0x01b6, B:67:0x01c7, B:69:0x01d3, B:71:0x01dc, B:74:0x01e4, B:75:0x0206, B:78:0x020e, B:82:0x021d, B:84:0x0224, B:87:0x022e, B:88:0x0232, B:90:0x0238, B:92:0x024d, B:95:0x0255, B:97:0x0293, B:99:0x029d, B:103:0x02af, B:105:0x02b5, B:108:0x02bf, B:109:0x02c3, B:111:0x02c9, B:113:0x02de, B:118:0x0302, B:120:0x0309, B:121:0x0312, B:123:0x0318, B:125:0x0324, B:126:0x0328, B:129:0x0338, B:130:0x033c, B:132:0x0342, B:136:0x0355, B:138:0x0359, B:139:0x0381, B:141:0x03a6, B:160:0x03ed, B:162:0x03f1, B:181:0x0438, B:165:0x03ff, B:168:0x040a, B:170:0x0413, B:172:0x0419, B:174:0x0426, B:179:0x0432, B:184:0x043d, B:186:0x0443, B:144:0x03b4, B:147:0x03bf, B:149:0x03c8, B:151:0x03ce, B:153:0x03db, B:158:0x03e7, B:188:0x0459, B:190:0x0465, B:192:0x046b, B:195:0x0472, B:197:0x049e, B:199:0x04aa, B:42:0x0126, B:46:0x0137, B:45:0x0133), top: B:205:0x002e }] */
    /* JADX WARN: Removed duplicated region for block: B:53:0x0166 A[Catch: Exception -> 0x004c, UnknownHostException -> 0x004f, SocketTimeoutException -> 0x0052, TRY_ENTER, TryCatch #2 {SocketTimeoutException -> 0x0052, UnknownHostException -> 0x004f, Exception -> 0x004c, blocks: (B:14:0x0046, B:50:0x015c, B:53:0x0166, B:55:0x018e, B:58:0x0197, B:60:0x019f, B:63:0x01a8, B:65:0x01b6, B:67:0x01c7, B:69:0x01d3, B:71:0x01dc, B:74:0x01e4, B:75:0x0206, B:78:0x020e, B:82:0x021d, B:84:0x0224, B:87:0x022e, B:88:0x0232, B:90:0x0238, B:92:0x024d, B:95:0x0255, B:97:0x0293, B:99:0x029d, B:103:0x02af, B:105:0x02b5, B:108:0x02bf, B:109:0x02c3, B:111:0x02c9, B:113:0x02de, B:118:0x0302, B:120:0x0309, B:121:0x0312, B:123:0x0318, B:125:0x0324, B:126:0x0328, B:129:0x0338, B:130:0x033c, B:132:0x0342, B:136:0x0355, B:138:0x0359, B:139:0x0381, B:141:0x03a6, B:160:0x03ed, B:162:0x03f1, B:181:0x0438, B:165:0x03ff, B:168:0x040a, B:170:0x0413, B:172:0x0419, B:174:0x0426, B:179:0x0432, B:184:0x043d, B:186:0x0443, B:144:0x03b4, B:147:0x03bf, B:149:0x03c8, B:151:0x03ce, B:153:0x03db, B:158:0x03e7, B:188:0x0459, B:190:0x0465, B:192:0x046b, B:195:0x0472, B:197:0x049e, B:199:0x04aa, B:42:0x0126, B:46:0x0137, B:45:0x0133), top: B:205:0x002e }] */
    /* JADX WARN: Removed duplicated region for block: B:69:0x01d3 A[Catch: Exception -> 0x004c, UnknownHostException -> 0x004f, SocketTimeoutException -> 0x0052, TryCatch #2 {SocketTimeoutException -> 0x0052, UnknownHostException -> 0x004f, Exception -> 0x004c, blocks: (B:14:0x0046, B:50:0x015c, B:53:0x0166, B:55:0x018e, B:58:0x0197, B:60:0x019f, B:63:0x01a8, B:65:0x01b6, B:67:0x01c7, B:69:0x01d3, B:71:0x01dc, B:74:0x01e4, B:75:0x0206, B:78:0x020e, B:82:0x021d, B:84:0x0224, B:87:0x022e, B:88:0x0232, B:90:0x0238, B:92:0x024d, B:95:0x0255, B:97:0x0293, B:99:0x029d, B:103:0x02af, B:105:0x02b5, B:108:0x02bf, B:109:0x02c3, B:111:0x02c9, B:113:0x02de, B:118:0x0302, B:120:0x0309, B:121:0x0312, B:123:0x0318, B:125:0x0324, B:126:0x0328, B:129:0x0338, B:130:0x033c, B:132:0x0342, B:136:0x0355, B:138:0x0359, B:139:0x0381, B:141:0x03a6, B:160:0x03ed, B:162:0x03f1, B:181:0x0438, B:165:0x03ff, B:168:0x040a, B:170:0x0413, B:172:0x0419, B:174:0x0426, B:179:0x0432, B:184:0x043d, B:186:0x0443, B:144:0x03b4, B:147:0x03bf, B:149:0x03c8, B:151:0x03ce, B:153:0x03db, B:158:0x03e7, B:188:0x0459, B:190:0x0465, B:192:0x046b, B:195:0x0472, B:197:0x049e, B:199:0x04aa, B:42:0x0126, B:46:0x0137, B:45:0x0133), top: B:205:0x002e }] */
    /* JADX WARN: Removed duplicated region for block: B:7:0x001b  */
    @Override // com.strmr.ps.data.repository.IDebridProvider
    @pq6
    /* JADX INFO: renamed from: resolveToDirectLink-hUnOzRk */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public Object mo76resolveToDirectLinkhUnOzRk(@kp6 String str, @pq6 Integer num, @pq6 Integer num2, @pq6 Integer num3, @pq6 String str2, @kp6 Continuation<? super q38<String>> continuation) {
        EasyDebridRepository$resolveToDirectLink$1 easyDebridRepository$resolveToDirectLink$1;
        Integer num4;
        String str3;
        String str4;
        Integer num5;
        Integer num6;
        EasyDebridRepository easyDebridRepository;
        EasyDebridAccount easyDebridAccount;
        Integer num7;
        String str5;
        Integer num8;
        String strConcat;
        Integer num9;
        Integer num10;
        String str6;
        Integer num11;
        Integer num12;
        EasyDebridGenerateResponse easyDebridGenerateResponse;
        String str7;
        EasyDebridGeneratedFile easyDebridGeneratedFile;
        Object next;
        Object next2;
        Object next3;
        if (continuation instanceof EasyDebridRepository$resolveToDirectLink$1) {
            easyDebridRepository$resolveToDirectLink$1 = (EasyDebridRepository$resolveToDirectLink$1) continuation;
            int i = easyDebridRepository$resolveToDirectLink$1.label;
            if ((i & Integer.MIN_VALUE) != 0) {
                easyDebridRepository$resolveToDirectLink$1.label = i - Integer.MIN_VALUE;
            } else {
                easyDebridRepository$resolveToDirectLink$1 = new EasyDebridRepository$resolveToDirectLink$1(this, continuation);
            }
        }
        Object account = easyDebridRepository$resolveToDirectLink$1.result;
        dr4.c();
        ck1 ck1Var = ck1.a;
        int i2 = easyDebridRepository$resolveToDirectLink$1.label;
        try {
            if (i2 == 0) {
                s38.b(account);
                EasyDebridAccountDao easyDebridAccountDao = this.accountDao;
                easyDebridRepository$resolveToDirectLink$1.L$0 = this;
                easyDebridRepository$resolveToDirectLink$1.L$1 = str;
                easyDebridRepository$resolveToDirectLink$1.L$2 = num;
                num4 = num2;
                easyDebridRepository$resolveToDirectLink$1.L$3 = num4;
                easyDebridRepository$resolveToDirectLink$1.L$4 = num3;
                easyDebridRepository$resolveToDirectLink$1.L$5 = str2;
                easyDebridRepository$resolveToDirectLink$1.label = 1;
                account = easyDebridAccountDao.getAccount("easydebrid", easyDebridRepository$resolveToDirectLink$1);
                if (account != ck1Var) {
                    str3 = str;
                    str4 = str2;
                    num5 = num;
                    num6 = num3;
                    easyDebridRepository = this;
                }
                return ck1Var;
            }
            if (i2 != 1) {
                if (i2 == 2) {
                    easyDebridAccount = (EasyDebridAccount) easyDebridRepository$resolveToDirectLink$1.L$6;
                    str5 = (String) easyDebridRepository$resolveToDirectLink$1.L$5;
                    num8 = (Integer) easyDebridRepository$resolveToDirectLink$1.L$4;
                    Integer num13 = (Integer) easyDebridRepository$resolveToDirectLink$1.L$3;
                    num7 = (Integer) easyDebridRepository$resolveToDirectLink$1.L$2;
                    strConcat = (String) easyDebridRepository$resolveToDirectLink$1.L$1;
                    EasyDebridRepository easyDebridRepository2 = (EasyDebridRepository) easyDebridRepository$resolveToDirectLink$1.L$0;
                    s38.b(account);
                    easyDebridRepository = easyDebridRepository2;
                    num4 = num13;
                    num9 = (Integer) account;
                    if (num9 == null) {
                        Log.w(TAG, "Account expired " + num9 + " days ago");
                        return s38.a(new DebridError.AccountExpired(DebridProvider.EASY_DEBRID, num9));
                    }
                    String authHeader = easyDebridAccount.getAuthHeader();
                    if (!n89.ar(strConcat, "magnet:", false)) {
                        strConcat = "magnet:?xt=urn:btih:".concat(strConcat);
                    }
                    EasyDebridGenerateRequest easyDebridGenerateRequest = new EasyDebridGenerateRequest(strConcat, null, 2, null);
                    EasyDebridApiService easyDebridApiService = easyDebridRepository.easyDebridApiService;
                    easyDebridRepository$resolveToDirectLink$1.L$0 = num7;
                    easyDebridRepository$resolveToDirectLink$1.L$1 = num4;
                    easyDebridRepository$resolveToDirectLink$1.L$2 = num8;
                    easyDebridRepository$resolveToDirectLink$1.L$3 = str5;
                    easyDebridRepository$resolveToDirectLink$1.L$4 = null;
                    easyDebridRepository$resolveToDirectLink$1.L$5 = null;
                    easyDebridRepository$resolveToDirectLink$1.L$6 = null;
                    easyDebridRepository$resolveToDirectLink$1.label = 3;
                    account = easyDebridApiService.generateLink(authHeader, easyDebridGenerateRequest, easyDebridRepository$resolveToDirectLink$1);
                    if (account != ck1Var) {
                        num10 = num4;
                        str6 = str5;
                        num11 = num8;
                        num12 = num7;
                        easyDebridGenerateResponse = (EasyDebridGenerateResponse) account;
                        if (easyDebridGenerateResponse.getError() == null) {
                        }
                    }
                    return ck1Var;
                }
                if (i2 != 3) {
                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                }
                str6 = (String) easyDebridRepository$resolveToDirectLink$1.L$3;
                num11 = (Integer) easyDebridRepository$resolveToDirectLink$1.L$2;
                num10 = (Integer) easyDebridRepository$resolveToDirectLink$1.L$1;
                num12 = (Integer) easyDebridRepository$resolveToDirectLink$1.L$0;
                s38.b(account);
                easyDebridGenerateResponse = (EasyDebridGenerateResponse) account;
                if (easyDebridGenerateResponse.getError() == null) {
                    Log.w(TAG, "Link generation failed: " + easyDebridGenerateResponse.getError());
                    String lowerCase = easyDebridGenerateResponse.getError().toLowerCase(Locale.ROOT);
                    cr4.f(lowerCase, "toLowerCase(...)");
                    if (!n89.l(lowerCase, "not cached", false) && !n89.l(lowerCase, "not found", false)) {
                        if (!n89.l(lowerCase, "expired", false) && !n89.l(lowerCase, "subscription", false)) {
                            return s38.a(new Exception(easyDebridGenerateResponse.getError()));
                        }
                        return s38.a(new DebridError.AccountExpired(DebridProvider.EASY_DEBRID, Boxing.boxInt(0)));
                    }
                    return s38.a(new DebridError.NotCached(DebridProvider.EASY_DEBRID));
                }
                List<EasyDebridGeneratedFile> files = easyDebridGenerateResponse.getFiles();
                List<EasyDebridGeneratedFile> list = files;
                if (list != null && !list.isEmpty()) {
                    List listAn = y41.an(".mkv", ".mp4", ".avi", ".mov", ".wmv", ".m4v", ".webm");
                    ArrayList arrayList = new ArrayList();
                    Iterator<T> it = files.iterator();
                    while (true) {
                        str7 = "";
                        if (!it.hasNext()) {
                            break;
                        }
                        Object next4 = it.next();
                        String filename = ((EasyDebridGeneratedFile) next4).getFilename();
                        if (filename != null) {
                            str7 = filename;
                        }
                        List list2 = listAn;
                        if (!(list2 instanceof Collection) || !list2.isEmpty()) {
                            Iterator it2 = list2.iterator();
                            while (true) {
                                if (it2.hasNext()) {
                                    String str8 = (String) it2.next();
                                    String lowerCase2 = str7.toLowerCase(Locale.ROOT);
                                    cr4.f(lowerCase2, "toLowerCase(...)");
                                    if (n89.r(lowerCase2, str8, false)) {
                                        arrayList.add(next4);
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    Log.w(TAG, "resolveToDirectLink: " + files.size() + " files (" + arrayList.size() + " video), season=" + num10 + ", episode=" + num11 + ", fileIdx=" + num12);
                    if (num12 == null || num12.intValue() >= files.size()) {
                        easyDebridGeneratedFile = null;
                    } else {
                        easyDebridGeneratedFile = files.get(num12.intValue());
                        String filename2 = easyDebridGeneratedFile.getFilename();
                        if (filename2 != null) {
                            str7 = filename2;
                        }
                        List<String> list3 = listAn;
                        if (!(list3 instanceof Collection) || !list3.isEmpty()) {
                            for (String str9 : list3) {
                                String lowerCase3 = str7.toLowerCase(Locale.ROOT);
                                cr4.f(lowerCase3, "toLowerCase(...)");
                                if (n89.r(lowerCase3, str9, false)) {
                                    Log.w(TAG, "Using fileIdx " + num12 + ": " + str7);
                                    break;
                                }
                            }
                        }
                        easyDebridGeneratedFile = null;
                    }
                    if (easyDebridGeneratedFile == null && num10 != null && num11 != null && arrayList.size() > 1) {
                        ArrayList arrayList2 = new ArrayList();
                        Iterator it3 = arrayList.iterator();
                        while (it3.hasNext()) {
                            String filename3 = ((EasyDebridGeneratedFile) it3.next()).getFilename();
                            if (filename3 != null) {
                                arrayList2.add(filename3);
                            }
                        }
                        String strC = vo2.c(arrayList2, num10.intValue(), num11.intValue(), str6);
                        if (strC != null) {
                            Iterator it4 = arrayList.iterator();
                            while (true) {
                                if (!it4.hasNext()) {
                                    next3 = null;
                                    break;
                                }
                                next3 = it4.next();
                                if (cr4.b(((EasyDebridGeneratedFile) next3).getFilename(), strC)) {
                                    break;
                                }
                            }
                            EasyDebridGeneratedFile easyDebridGeneratedFile2 = (EasyDebridGeneratedFile) next3;
                            if (easyDebridGeneratedFile2 != null) {
                                Log.w(TAG, "Found episode S" + num10 + "E" + num11 + " by filename: " + easyDebridGeneratedFile2.getFilename());
                                easyDebridGeneratedFile = easyDebridGeneratedFile2;
                            }
                        } else {
                            Boxing.boxInt(Log.w(TAG, "Could not find episode S" + num10 + "E" + num11 + " by filename pattern"));
                        }
                    }
                    if (easyDebridGeneratedFile == null) {
                        Iterator it5 = arrayList.iterator();
                        if (it5.hasNext()) {
                            next = it5.next();
                            if (it5.hasNext()) {
                                Long size = ((EasyDebridGeneratedFile) next).getSize();
                                long jLongValue = size != null ? size.longValue() : 0L;
                                do {
                                    Object next5 = it5.next();
                                    Long size2 = ((EasyDebridGeneratedFile) next5).getSize();
                                    long jLongValue2 = size2 != null ? size2.longValue() : 0L;
                                    if (jLongValue < jLongValue2) {
                                        next = next5;
                                        jLongValue = jLongValue2;
                                    }
                                } while (it5.hasNext());
                            }
                        } else {
                            next = null;
                        }
                        EasyDebridGeneratedFile easyDebridGeneratedFile3 = (EasyDebridGeneratedFile) next;
                        if (easyDebridGeneratedFile3 == null) {
                            Iterator<T> it6 = files.iterator();
                            if (it6.hasNext()) {
                                next2 = it6.next();
                                if (it6.hasNext()) {
                                    Long size3 = ((EasyDebridGeneratedFile) next2).getSize();
                                    long jLongValue3 = size3 != null ? size3.longValue() : 0L;
                                    do {
                                        Object next6 = it6.next();
                                        Long size4 = ((EasyDebridGeneratedFile) next6).getSize();
                                        long jLongValue4 = size4 != null ? size4.longValue() : 0L;
                                        if (jLongValue3 < jLongValue4) {
                                            next2 = next6;
                                            jLongValue3 = jLongValue4;
                                        }
                                    } while (it6.hasNext());
                                }
                            } else {
                                next2 = null;
                            }
                            easyDebridGeneratedFile3 = (EasyDebridGeneratedFile) next2;
                        }
                        easyDebridGeneratedFile = easyDebridGeneratedFile3;
                        Log.w(TAG, "Using largest file as fallback: " + (easyDebridGeneratedFile != null ? easyDebridGeneratedFile.getFilename() : null));
                    }
                    if (easyDebridGeneratedFile == null) {
                        return s38.a(new Exception("No files found"));
                    }
                    String url = easyDebridGeneratedFile.getUrl();
                    if (url != null && !n89.ab(url)) {
                        Log.w(TAG, "Resolved: " + easyDebridGeneratedFile.getFilename() + " -> " + n89.bb(50, url) + "...");
                        return url;
                    }
                    return s38.a(new Exception("No download URL in response"));
                }
                return s38.a(new DebridError.NotCached(DebridProvider.EASY_DEBRID));
            }
            str4 = (String) easyDebridRepository$resolveToDirectLink$1.L$5;
            num6 = (Integer) easyDebridRepository$resolveToDirectLink$1.L$4;
            num4 = (Integer) easyDebridRepository$resolveToDirectLink$1.L$3;
            num5 = (Integer) easyDebridRepository$resolveToDirectLink$1.L$2;
            str3 = (String) easyDebridRepository$resolveToDirectLink$1.L$1;
            easyDebridRepository = (EasyDebridRepository) easyDebridRepository$resolveToDirectLink$1.L$0;
            s38.b(account);
            EasyDebridAccount easyDebridAccount2 = (EasyDebridAccount) account;
            if (easyDebridAccount2 == null) {
                return s38.a(new DebridError.NoAccount(DebridProvider.EASY_DEBRID));
            }
            easyDebridRepository$resolveToDirectLink$1.L$0 = easyDebridRepository;
            easyDebridRepository$resolveToDirectLink$1.L$1 = str3;
            easyDebridRepository$resolveToDirectLink$1.L$2 = num5;
            easyDebridRepository$resolveToDirectLink$1.L$3 = num4;
            easyDebridRepository$resolveToDirectLink$1.L$4 = num6;
            easyDebridRepository$resolveToDirectLink$1.L$5 = str4;
            easyDebridRepository$resolveToDirectLink$1.L$6 = easyDebridAccount2;
            easyDebridRepository$resolveToDirectLink$1.label = 2;
            Object accountExpirationStatus = easyDebridRepository.getAccountExpirationStatus(easyDebridRepository$resolveToDirectLink$1);
            if (accountExpirationStatus != ck1Var) {
                String str10 = str4;
                easyDebridAccount = easyDebridAccount2;
                account = accountExpirationStatus;
                num7 = num5;
                str5 = str10;
                String str11 = str3;
                num8 = num6;
                strConcat = str11;
                num9 = (Integer) account;
                if (num9 == null) {
                }
            }
            return ck1Var;
        } catch (SocketTimeoutException e) {
            Log.e(TAG, "Timeout resolving link", e);
            return s38.a(new DebridError.NetworkError(DebridProvider.EASY_DEBRID, e));
        } catch (UnknownHostException e2) {
            Log.e(TAG, "DNS error resolving link", e2);
            return s38.a(new DebridError.DnsError(DebridProvider.EASY_DEBRID, e2));
        } catch (Exception e3) {
            Log.e(TAG, "Failed to resolve direct link", e3);
            return s38.a(e3);
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:41:0x00ea A[RETURN] */
    /* JADX WARN: Removed duplicated region for block: B:42:0x00eb A[RETURN] */
    /* JADX WARN: Removed duplicated region for block: B:7:0x001d  */
    @pq6
    /* JADX INFO: renamed from: verifyAndSaveApiKey-gIAlu-s, reason: not valid java name */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object m105verifyAndSaveApiKeygIAlus(@kp6 String str, @kp6 Continuation<? super q38<EasyDebridAccount>> continuation) {
        EasyDebridRepository$verifyAndSaveApiKey$1 easyDebridRepository$verifyAndSaveApiKey$1;
        EasyDebridRepository easyDebridRepository;
        EasyDebridUserResponse easyDebridUserResponse;
        EasyDebridAccount easyDebridAccount;
        String str2 = str;
        if (continuation instanceof EasyDebridRepository$verifyAndSaveApiKey$1) {
            easyDebridRepository$verifyAndSaveApiKey$1 = (EasyDebridRepository$verifyAndSaveApiKey$1) continuation;
            int i = easyDebridRepository$verifyAndSaveApiKey$1.label;
            if ((i & Integer.MIN_VALUE) != 0) {
                easyDebridRepository$verifyAndSaveApiKey$1.label = i - Integer.MIN_VALUE;
            } else {
                easyDebridRepository$verifyAndSaveApiKey$1 = new EasyDebridRepository$verifyAndSaveApiKey$1(this, continuation);
            }
        }
        Object userDetails = easyDebridRepository$verifyAndSaveApiKey$1.result;
        ck1 ck1Var = ck1.a;
        int i2 = easyDebridRepository$verifyAndSaveApiKey$1.label;
        try {
            if (i2 == 0) {
                s38.b(userDetails);
                EasyDebridApiService easyDebridApiService = this.easyDebridApiService;
                easyDebridRepository$verifyAndSaveApiKey$1.L$0 = this;
                easyDebridRepository$verifyAndSaveApiKey$1.L$1 = str2;
                easyDebridRepository$verifyAndSaveApiKey$1.label = 1;
                userDetails = easyDebridApiService.getUserDetails("Bearer " + str2, easyDebridRepository$verifyAndSaveApiKey$1);
                if (userDetails != ck1Var) {
                    easyDebridRepository = this;
                }
            } else {
                if (i2 != 1) {
                    if (i2 != 2) {
                        if (i2 != 3) {
                            throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                        }
                        EasyDebridAccount easyDebridAccount2 = (EasyDebridAccount) easyDebridRepository$verifyAndSaveApiKey$1.L$0;
                        s38.b(userDetails);
                        return easyDebridAccount2;
                    }
                    easyDebridAccount = (EasyDebridAccount) easyDebridRepository$verifyAndSaveApiKey$1.L$2;
                    easyDebridUserResponse = (EasyDebridUserResponse) easyDebridRepository$verifyAndSaveApiKey$1.L$1;
                    easyDebridRepository = (EasyDebridRepository) easyDebridRepository$verifyAndSaveApiKey$1.L$0;
                    s38.b(userDetails);
                    easyDebridUserResponse.getId();
                    easyDebridRepository$verifyAndSaveApiKey$1.L$0 = easyDebridAccount;
                    easyDebridRepository$verifyAndSaveApiKey$1.L$1 = null;
                    easyDebridRepository$verifyAndSaveApiKey$1.L$2 = null;
                    easyDebridRepository$verifyAndSaveApiKey$1.label = 3;
                    return easyDebridRepository.triggerCloudSync(easyDebridRepository$verifyAndSaveApiKey$1) != ck1Var ? ck1Var : easyDebridAccount;
                }
                str2 = (String) easyDebridRepository$verifyAndSaveApiKey$1.L$1;
                easyDebridRepository = (EasyDebridRepository) easyDebridRepository$verifyAndSaveApiKey$1.L$0;
                s38.b(userDetails);
            }
            String str3 = str2;
            EasyDebridUserResponse easyDebridUserResponse2 = (EasyDebridUserResponse) userDetails;
            if (easyDebridUserResponse2.getError() != null) {
                Log.w(TAG, "EasyDebrid verification failed: " + easyDebridUserResponse2.getError());
                return s38.a(new Exception(easyDebridUserResponse2.getError()));
            }
            long jCurrentTimeMillis = System.currentTimeMillis();
            EasyDebridAccount easyDebridAccount3 = new EasyDebridAccount("easydebrid", str3, easyDebridUserResponse2.getId(), easyDebridUserResponse2.getPaidUntil(), jCurrentTimeMillis, jCurrentTimeMillis);
            EasyDebridAccountDao easyDebridAccountDao = easyDebridRepository.accountDao;
            easyDebridRepository$verifyAndSaveApiKey$1.L$0 = easyDebridRepository;
            easyDebridRepository$verifyAndSaveApiKey$1.L$1 = easyDebridUserResponse2;
            easyDebridRepository$verifyAndSaveApiKey$1.L$2 = easyDebridAccount3;
            easyDebridRepository$verifyAndSaveApiKey$1.label = 2;
            if (easyDebridAccountDao.upsert(easyDebridAccount3, easyDebridRepository$verifyAndSaveApiKey$1) != ck1Var) {
                easyDebridUserResponse = easyDebridUserResponse2;
                easyDebridAccount = easyDebridAccount3;
                easyDebridUserResponse.getId();
                easyDebridRepository$verifyAndSaveApiKey$1.L$0 = easyDebridAccount;
                easyDebridRepository$verifyAndSaveApiKey$1.L$1 = null;
                easyDebridRepository$verifyAndSaveApiKey$1.L$2 = null;
                easyDebridRepository$verifyAndSaveApiKey$1.label = 3;
                if (easyDebridRepository.triggerCloudSync(easyDebridRepository$verifyAndSaveApiKey$1) != ck1Var) {
                }
            }
        } catch (Exception e) {
            Log.e(TAG, "Failed to verify API key", e);
            return s38.a(e);
        }
    }
}
