package com.strmr.ps.data.repository;

import android.util.Log;
import com.strmr.ps.data.local.dao.InstalledAddonDao;
import com.strmr.ps.data.local.entity.InstalledAddonEntity;
import com.strmr.ps.data.model.addon.InstalledAddon;
import com.strmr.ps.data.model.addon.StremioManifest;
import com.strmr.ps.data.repository.AddonRepository;
import com.strmr.ps.data.service.StremioAddonService;
import defpackage.ak1;
import defpackage.ck1;
import defpackage.cp4;
import defpackage.dp4;
import defpackage.e6a;
import defpackage.gr3;
import defpackage.io6;
import defpackage.kq;
import defpackage.q38;
import defpackage.s38;
import defpackage.to4;
import defpackage.vx8;
import defpackage.y41;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
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;

/* JADX INFO: loaded from: classes3.dex */
@Metadata(d1 = {"\u0000\u0010\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\u0010\u0003\u001a\b\u0012\u0004\u0012\u00020\u00020\u0001*\u00020\u0000H\n¢\u0006\u0004\b\u0003\u0010\u0004"}, d2 = {"Lak1;", "Lq38;", "Lcom/strmr/ps/data/model/addon/InstalledAddon;", "<anonymous>", "(Lak1;)Lq38;"}, k = 3, mv = {2, 0, 0})
@DebugMetadata(c = "com.strmr.ps.data.repository.AddonRepository$installAddon$2", f = "AddonRepository.kt", i = {1, 1, 2, 2, 3, 3, 3, 4}, l = {225, 231, 241, 250, 255}, m = "invokeSuspend", n = {"manifest", "baseUrl", "manifest", "baseUrl", "manifest", "instanceId", "instanceNumber", "manifest"}, s = {"L$0", "L$1", "L$0", "L$1", "L$0", "L$1", "I$0", "L$0"})
@vx8
public final class AddonRepository$installAddon$2 extends SuspendLambda implements gr3<ak1, Continuation<? super q38<? extends InstalledAddon>>, Object> {
    final /* synthetic */ boolean $allowMultipleInstances;
    final /* synthetic */ String $url;
    int I$0;
    Object L$0;
    Object L$1;
    int label;
    final /* synthetic */ AddonRepository this$0;

    /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
    public AddonRepository$installAddon$2(String str, boolean z, AddonRepository addonRepository, Continuation<? super AddonRepository$installAddon$2> continuation) {
        super(2, continuation);
        this.$url = str;
        this.$allowMultipleInstances = z;
        this.this$0 = addonRepository;
    }

    @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl
    public final Continuation<e6a> create(Object obj, Continuation<?> continuation) {
        return new AddonRepository$installAddon$2(this.$url, this.$allowMultipleInstances, this.this$0, continuation);
    }

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

    /* JADX WARN: Code restructure failed: missing block: B:75:0x01dc, code lost:
    
        if (r3 != r0) goto L77;
     */
    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:46:0x010c  */
    /* JADX WARN: Removed duplicated region for block: B:50:0x011b  */
    /* JADX WARN: Removed duplicated region for block: B:54:0x0134 A[Catch: Exception -> 0x015d, TryCatch #0 {Exception -> 0x015d, blocks: (B:51:0x011d, B:52:0x012d, B:54:0x0134, B:57:0x0147, B:59:0x0151, B:70:0x017e, B:65:0x0161, B:69:0x0169, B:83:0x01fa, B:84:0x0201), top: B:93:0x011d }] */
    /* JADX WARN: Removed duplicated region for block: B:73:0x01a1  */
    /* JADX WARN: Type inference failed for: r2v0, types: [int] */
    /* JADX WARN: Type inference failed for: r2v21 */
    /* JADX WARN: Type inference failed for: r2v39 */
    @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object invokeSuspend(Object obj) {
        Object objA;
        Object objB;
        Object addonsByAddonId;
        String str;
        StremioManifest stremioManifest;
        Object installedAddon;
        int i;
        String str2;
        StremioManifest stremioManifest2;
        String str3;
        StremioManifest stremioManifest3;
        Object usedInstanceNumbers;
        Iterator<Integer> it;
        String str4;
        InstalledAddonEntity installedAddonEntityCreateAddonEntity;
        InstalledAddonDao installedAddonDao;
        List list;
        String id;
        ck1 ck1Var = ck1.a;
        StremioManifest stremioManifest4 = this.label;
        String str5 = "";
        try {
        } catch (Exception e) {
            e = e;
        }
        if (stremioManifest4 == 0) {
            s38.b(obj);
            ExecutorService executorService = kq.a;
            kq.h("AddonRepository", "Installing addon from URL: " + this.$url + " (allowMultiple=" + this.$allowMultipleInstances + ")");
            StremioAddonService stremioAddonService = this.this$0.stremioAddonService;
            String str6 = this.$url;
            this.label = 1;
            objB = stremioAddonService.b(str6, this);
            if (objB != ck1Var) {
            }
            return ck1Var;
        }
        if (stremioManifest4 == 1) {
            s38.b(obj);
            objB = obj;
        } else {
            if (stremioManifest4 == 2) {
                String str7 = (String) this.L$1;
                StremioManifest stremioManifest5 = (StremioManifest) this.L$0;
                s38.b(obj);
                str = str7;
                stremioManifest = stremioManifest5;
                addonsByAddonId = obj;
                list = (List) addonsByAddonId;
                if (list.isEmpty() && !this.$allowMultipleInstances) {
                    return new q38(s38.a(new AddonRepository.AddonAlreadyInstalledException(((InstalledAddonEntity) y41.z(list)).getName(), null, 2, null)));
                }
                InstalledAddonDao installedAddonDao2 = this.this$0.addonDao;
                id = stremioManifest.getId();
                if (id == null) {
                    id = "";
                }
                this.L$0 = stremioManifest;
                this.L$1 = str;
                this.label = 3;
                usedInstanceNumbers = installedAddonDao2.getUsedInstanceNumbers(id, this);
                if (usedInstanceNumbers != ck1Var) {
                    stremioManifest3 = stremioManifest;
                    str3 = str;
                    List list2 = (List) usedInstanceNumbers;
                    it = new dp4(1, list2.size() + 1, 1).iterator();
                    while (((cp4) it).c) {
                    }
                    throw new NoSuchElementException("Collection contains no element matching the predicate.");
                }
                return ck1Var;
            }
            try {
            } catch (Exception e2) {
                e = e2;
                stremioManifest4 = 3;
                Log.e("AddonRepository", "Failed to install addon: " + stremioManifest4.getId(), e);
                objA = s38.a(e);
                return new q38(objA);
            }
            if (stremioManifest4 == 3) {
                String str8 = (String) this.L$1;
                StremioManifest stremioManifest6 = (StremioManifest) this.L$0;
                s38.b(obj);
                str3 = str8;
                stremioManifest3 = stremioManifest6;
                usedInstanceNumbers = obj;
                try {
                    List list22 = (List) usedInstanceNumbers;
                    it = new dp4(1, list22.size() + 1, 1).iterator();
                    while (((cp4) it).c) {
                        int iNextInt = ((to4) it).nextInt();
                        if (!list22.contains(Boxing.boxInt(iNextInt))) {
                            if (iNextInt == 1 && !list22.contains(Boxing.boxInt(1))) {
                                String id2 = stremioManifest3.getId();
                                if (id2 != null) {
                                    str4 = id2;
                                }
                                installedAddonEntityCreateAddonEntity = this.this$0.createAddonEntity(stremioManifest3, str3, this.$url, str4, iNextInt);
                                String str9 = str4;
                                i = iNextInt;
                                installedAddonDao = this.this$0.addonDao;
                                this.L$0 = stremioManifest3;
                                this.L$1 = str9;
                                this.I$0 = i;
                                this.label = 4;
                                if (installedAddonDao.upsertAddon(installedAddonEntityCreateAddonEntity, this) != ck1Var) {
                                    str2 = str9;
                                    stremioManifest2 = stremioManifest3;
                                    ExecutorService executorService2 = kq.a;
                                    kq.h("AddonRepository", "Installed addon: " + stremioManifest2.getName() + " (instance: " + str2 + ", #" + i + ")");
                                    AddonRepository addonRepository = this.this$0;
                                    this.L$0 = stremioManifest2;
                                    this.L$1 = null;
                                    this.label = 5;
                                    installedAddon = addonRepository.getInstalledAddon(str2, this);
                                }
                                return ck1Var;
                            }
                            String id3 = stremioManifest3.getId();
                            if (id3 != null) {
                                str5 = id3;
                            }
                            str5 = str5 + "_" + iNextInt;
                            str4 = str5;
                            installedAddonEntityCreateAddonEntity = this.this$0.createAddonEntity(stremioManifest3, str3, this.$url, str4, iNextInt);
                            String str92 = str4;
                            i = iNextInt;
                            installedAddonDao = this.this$0.addonDao;
                            this.L$0 = stremioManifest3;
                            this.L$1 = str92;
                            this.I$0 = i;
                            this.label = 4;
                            if (installedAddonDao.upsertAddon(installedAddonEntityCreateAddonEntity, this) != ck1Var) {
                            }
                            return ck1Var;
                        }
                    }
                    throw new NoSuchElementException("Collection contains no element matching the predicate.");
                } catch (Exception e3) {
                    e = e3;
                    stremioManifest4 = stremioManifest3;
                    Log.e("AddonRepository", "Failed to install addon: " + stremioManifest4.getId(), e);
                    objA = s38.a(e);
                }
            } else {
                if (stremioManifest4 != 4) {
                    if (stremioManifest4 != 5) {
                        throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                    }
                    s38.b(obj);
                    installedAddon = obj;
                    objA = (InstalledAddon) installedAddon;
                    if (objA == null) {
                        return new q38(s38.a(new IllegalStateException("Failed to retrieve installed addon")));
                    }
                    this.this$0.triggerCloudSync();
                    return new q38(objA);
                }
                int i2 = this.I$0;
                str2 = (String) this.L$1;
                StremioManifest stremioManifest7 = (StremioManifest) this.L$0;
                s38.b(obj);
                i = i2;
                stremioManifest2 = stremioManifest7;
                ExecutorService executorService22 = kq.a;
                kq.h("AddonRepository", "Installed addon: " + stremioManifest2.getName() + " (instance: " + str2 + ", #" + i + ")");
                AddonRepository addonRepository2 = this.this$0;
                this.L$0 = stremioManifest2;
                this.L$1 = null;
                this.label = 5;
                installedAddon = addonRepository2.getInstalledAddon(str2, this);
            }
        }
        StremioAddonService.c cVar = (StremioAddonService.c) objB;
        if (!(cVar instanceof StremioAddonService.c.b)) {
            if (!(cVar instanceof StremioAddonService.c.a)) {
                throw new io6();
            }
            String str10 = ((StremioAddonService.c.a) cVar).a;
            Log.w("AddonRepository", "Addon validation failed: ".concat(str10));
            objA = s38.a(new IllegalArgumentException(str10));
            return new q38(objA);
        }
        StremioAddonService.c.b bVar = (StremioAddonService.c.b) cVar;
        StremioManifest stremioManifest8 = bVar.a;
        String str11 = bVar.b;
        InstalledAddonDao installedAddonDao3 = this.this$0.addonDao;
        String id4 = stremioManifest8.getId();
        if (id4 == null) {
            id4 = "";
        }
        this.L$0 = stremioManifest8;
        this.L$1 = str11;
        this.label = 2;
        addonsByAddonId = installedAddonDao3.getAddonsByAddonId(id4, this);
        if (addonsByAddonId != ck1Var) {
            str = str11;
            stremioManifest = stremioManifest8;
            list = (List) addonsByAddonId;
            if (list.isEmpty()) {
            }
            InstalledAddonDao installedAddonDao22 = this.this$0.addonDao;
            id = stremioManifest.getId();
            if (id == null) {
            }
            this.L$0 = stremioManifest;
            this.L$1 = str;
            this.label = 3;
            usedInstanceNumbers = installedAddonDao22.getUsedInstanceNumbers(id, this);
            if (usedInstanceNumbers != ck1Var) {
            }
        }
        return ck1Var;
    }
}
