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.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;
import okhttp3.internal.http.StatusLine;

/* 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$installConfiguredAddon$2", f = "AddonRepository.kt", i = {1, 1, 2, 2, 4, 4, 5, 5, 6}, l = {290, 296, 304, StatusLine.HTTP_TEMP_REDIRECT, 317, 330, 334}, m = "invokeSuspend", n = {"manifest", "baseUrl", "manifest", "existingAddon", "manifest", "baseUrl", "manifest", "instanceId", "manifest"}, s = {"L$0", "L$1", "L$0", "L$1", "L$0", "L$1", "L$0", "L$1", "L$0"})
@vx8
public final class AddonRepository$installConfiguredAddon$2 extends SuspendLambda implements gr3<ak1, Continuation<? super q38<? extends InstalledAddon>>, Object> {
    final /* synthetic */ boolean $allowMultipleInstances;
    final /* synthetic */ String $configuredUrl;
    final /* synthetic */ String $manifestUrl;
    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$installConfiguredAddon$2(String str, AddonRepository addonRepository, boolean z, String str2, Continuation<? super AddonRepository$installConfiguredAddon$2> continuation) {
        super(2, continuation);
        this.$configuredUrl = str;
        this.this$0 = addonRepository;
        this.$allowMultipleInstances = z;
        this.$manifestUrl = str2;
    }

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

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

    /* JADX WARN: Code restructure failed: missing block: B:46:0x015e, code lost:
    
        if (r2 != r0) goto L48;
     */
    /* JADX WARN: Code restructure failed: missing block: B:82:0x028b, code lost:
    
        if (r3 != r0) goto L84;
     */
    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:30:0x00a9  */
    /* JADX WARN: Removed duplicated region for block: B:54:0x0182 A[Catch: Exception -> 0x0023, TRY_ENTER, TryCatch #3 {Exception -> 0x0023, blocks: (B:7:0x001c, B:84:0x028e, B:86:0x0292, B:88:0x02a3, B:81:0x0257, B:54:0x0182, B:57:0x018f), top: B:105:0x000c }] */
    /* JADX WARN: Removed duplicated region for block: B:56:0x018e  */
    /* JADX WARN: Removed duplicated region for block: B:60:0x019e  */
    /* JADX WARN: Removed duplicated region for block: B:64:0x01b7 A[Catch: Exception -> 0x01de, TryCatch #0 {Exception -> 0x01de, blocks: (B:61:0x01a0, B:62:0x01b0, B:64:0x01b7, B:66:0x01c8, B:71:0x01d2, B:77:0x01f7, B:76:0x01e2, B:90:0x02a9, B:91:0x02b0), top: B:100:0x01a0 }] */
    /* JADX WARN: Removed duplicated region for block: B:93:0x02cb  */
    /* JADX WARN: Type inference failed for: r2v0, types: [int] */
    /* JADX WARN: Type inference failed for: r2v35 */
    /* JADX WARN: Type inference failed for: r2v49 */
    /* JADX WARN: Type inference failed for: r2v53 */
    @Override // kotlin.coroutines.jvm.internal.BaseContinuationImpl
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object invokeSuspend(Object obj) {
        Object objA;
        Object objB;
        StremioAddonService.c cVar;
        Object addonsByAddonId;
        String str;
        StremioManifest stremioManifest;
        List list;
        String id;
        Object usedInstanceNumbers;
        StremioManifest stremioManifest2;
        String str2;
        StremioManifest stremioManifest3;
        InstalledAddonEntity installedAddonEntity;
        Object installedAddon;
        Iterator<Integer> it;
        String str3;
        StremioManifest stremioManifest4;
        Object installedAddon2;
        ck1 ck1Var = ck1.a;
        StremioManifest stremioManifest5 = this.label;
        String str4 = "";
        try {
        } catch (Exception e) {
            e = e;
        }
        switch (stremioManifest5) {
            case 0:
                s38.b(obj);
                ExecutorService executorService = kq.a;
                kq.h("AddonRepository", "Installing configured addon: " + this.$configuredUrl);
                StremioAddonService stremioAddonService = this.this$0.stremioAddonService;
                String str5 = this.$configuredUrl;
                this.label = 1;
                objB = stremioAddonService.b(str5, this);
                if (objB != ck1Var) {
                    cVar = (StremioAddonService.c) objB;
                    if (cVar instanceof StremioAddonService.c.b) {
                        if (!(cVar instanceof StremioAddonService.c.a)) {
                            throw new io6();
                        }
                        String str6 = ((StremioAddonService.c.a) cVar).a;
                        Log.w("AddonRepository", "Configured addon validation failed: ".concat(str6));
                        objA = s38.a(new IllegalArgumentException(str6));
                        return new q38(objA);
                    }
                    StremioAddonService.c.b bVar = (StremioAddonService.c.b) cVar;
                    StremioManifest stremioManifest6 = bVar.a;
                    String str7 = bVar.b;
                    InstalledAddonDao installedAddonDao = this.this$0.addonDao;
                    String id2 = stremioManifest6.getId();
                    if (id2 == null) {
                        id2 = "";
                    }
                    this.L$0 = stremioManifest6;
                    this.L$1 = str7;
                    this.label = 2;
                    addonsByAddonId = installedAddonDao.getAddonsByAddonId(id2, this);
                    if (addonsByAddonId != ck1Var) {
                        str = str7;
                        stremioManifest = stremioManifest6;
                        list = (List) addonsByAddonId;
                        if (list.isEmpty() && !this.$allowMultipleInstances) {
                            InstalledAddonEntity installedAddonEntity2 = (InstalledAddonEntity) y41.z(list);
                            InstalledAddonEntity installedAddonEntityCopy$default = InstalledAddonEntity.copy$default(installedAddonEntity2, null, null, 0, null, null, null, null, null, this.$configuredUrl, true, null, null, null, null, 0, false, false, null, 0L, null, null, null, false, null, null, null, null, null, 268434687, null);
                            InstalledAddonDao installedAddonDao2 = this.this$0.addonDao;
                            this.L$0 = stremioManifest;
                            this.L$1 = installedAddonEntity2;
                            this.label = 3;
                            if (installedAddonDao2.upsertAddon(installedAddonEntityCopy$default, this) != ck1Var) {
                                stremioManifest3 = stremioManifest;
                                installedAddonEntity = installedAddonEntity2;
                                ExecutorService executorService2 = kq.a;
                                kq.h("AddonRepository", "Updated addon config: " + stremioManifest3.getName());
                                AddonRepository addonRepository = this.this$0;
                                String instanceId = installedAddonEntity.getInstanceId();
                                this.L$0 = null;
                                this.L$1 = null;
                                this.label = 4;
                                installedAddon = addonRepository.getInstalledAddon(instanceId, this);
                            }
                            break;
                        } else {
                            InstalledAddonDao installedAddonDao3 = this.this$0.addonDao;
                            id = stremioManifest.getId();
                            if (id == null) {
                                id = "";
                            }
                            this.L$0 = stremioManifest;
                            this.L$1 = str;
                            this.label = 5;
                            usedInstanceNumbers = installedAddonDao3.getUsedInstanceNumbers(id, this);
                            if (usedInstanceNumbers != ck1Var) {
                                stremioManifest2 = stremioManifest;
                                str2 = str;
                                try {
                                    List list2 = (List) usedInstanceNumbers;
                                    it = new dp4(1, list2.size() + 1, 1).iterator();
                                    while (((cp4) it).c) {
                                        int iNextInt = ((to4) it).nextInt();
                                        if (!list2.contains(Boxing.boxInt(iNextInt))) {
                                            String id3 = stremioManifest2.getId();
                                            if (id3 != null) {
                                                str4 = id3;
                                            }
                                            if (iNextInt != 1 || list2.contains(Boxing.boxInt(1))) {
                                                str4 = str4 + "_" + iNextInt;
                                            }
                                            str3 = str4;
                                            InstalledAddonEntity installedAddonEntityCopy$default2 = InstalledAddonEntity.copy$default(this.this$0.createAddonEntity(stremioManifest2, str2, this.$manifestUrl, str3, iNextInt), null, null, 0, null, null, null, null, null, this.$configuredUrl, true, null, null, null, null, 0, false, false, null, 0L, null, null, null, false, null, null, null, null, null, 268434687, null);
                                            InstalledAddonDao installedAddonDao4 = this.this$0.addonDao;
                                            this.L$0 = stremioManifest2;
                                            this.L$1 = str3;
                                            this.label = 6;
                                            if (installedAddonDao4.upsertAddon(installedAddonEntityCopy$default2, this) != ck1Var) {
                                                stremioManifest4 = stremioManifest2;
                                                ExecutorService executorService3 = kq.a;
                                                kq.h("AddonRepository", "Installed configured addon: " + stremioManifest4.getName() + " (instance: " + str3 + ")");
                                                AddonRepository addonRepository2 = this.this$0;
                                                this.L$0 = stremioManifest4;
                                                this.L$1 = null;
                                                this.label = 7;
                                                installedAddon2 = addonRepository2.getInstalledAddon(str3, this);
                                            }
                                            break;
                                        }
                                    }
                                    throw new NoSuchElementException("Collection contains no element matching the predicate.");
                                } catch (Exception e2) {
                                    e = e2;
                                    stremioManifest5 = stremioManifest2;
                                    Log.e("AddonRepository", "Failed to install configured addon: " + stremioManifest5.getId(), e);
                                    objA = s38.a(e);
                                }
                            }
                        }
                    }
                }
                return ck1Var;
            case 1:
                s38.b(obj);
                objB = obj;
                cVar = (StremioAddonService.c) objB;
                if (cVar instanceof StremioAddonService.c.b) {
                }
                break;
            case 2:
                String str8 = (String) this.L$1;
                StremioManifest stremioManifest7 = (StremioManifest) this.L$0;
                s38.b(obj);
                str = str8;
                stremioManifest = stremioManifest7;
                addonsByAddonId = obj;
                list = (List) addonsByAddonId;
                if (list.isEmpty()) {
                    InstalledAddonDao installedAddonDao32 = this.this$0.addonDao;
                    id = stremioManifest.getId();
                    if (id == null) {
                    }
                    this.L$0 = stremioManifest;
                    this.L$1 = str;
                    this.label = 5;
                    usedInstanceNumbers = installedAddonDao32.getUsedInstanceNumbers(id, this);
                    if (usedInstanceNumbers != ck1Var) {
                    }
                    return ck1Var;
                }
                return new q38(objA);
            case 3:
                installedAddonEntity = (InstalledAddonEntity) this.L$1;
                stremioManifest3 = (StremioManifest) this.L$0;
                s38.b(obj);
                ExecutorService executorService22 = kq.a;
                kq.h("AddonRepository", "Updated addon config: " + stremioManifest3.getName());
                AddonRepository addonRepository3 = this.this$0;
                String instanceId2 = installedAddonEntity.getInstanceId();
                this.L$0 = null;
                this.L$1 = null;
                this.label = 4;
                installedAddon = addonRepository3.getInstalledAddon(instanceId2, this);
                break;
            case 4:
                s38.b(obj);
                installedAddon = obj;
                InstalledAddon installedAddon3 = (InstalledAddon) installedAddon;
                if (installedAddon3 == null) {
                    return new q38(s38.a(new IllegalStateException("Failed to retrieve updated addon")));
                }
                this.this$0.triggerCloudSync();
                return new q38(installedAddon3);
            case 5:
                String str9 = (String) this.L$1;
                StremioManifest stremioManifest8 = (StremioManifest) this.L$0;
                try {
                    s38.b(obj);
                    usedInstanceNumbers = obj;
                    str2 = str9;
                    stremioManifest2 = stremioManifest8;
                    List list22 = (List) usedInstanceNumbers;
                    it = new dp4(1, list22.size() + 1, 1).iterator();
                    while (((cp4) it).c) {
                    }
                    throw new NoSuchElementException("Collection contains no element matching the predicate.");
                } catch (Exception e3) {
                    e = e3;
                    stremioManifest5 = stremioManifest8;
                    Log.e("AddonRepository", "Failed to install configured addon: " + stremioManifest5.getId(), e);
                    objA = s38.a(e);
                    return new q38(objA);
                }
            case 6:
                String str10 = (String) this.L$1;
                StremioManifest stremioManifest9 = (StremioManifest) this.L$0;
                try {
                    s38.b(obj);
                    str3 = str10;
                    stremioManifest4 = stremioManifest9;
                    ExecutorService executorService32 = kq.a;
                    kq.h("AddonRepository", "Installed configured addon: " + stremioManifest4.getName() + " (instance: " + str3 + ")");
                    AddonRepository addonRepository22 = this.this$0;
                    this.L$0 = stremioManifest4;
                    this.L$1 = null;
                    this.label = 7;
                    installedAddon2 = addonRepository22.getInstalledAddon(str3, this);
                } catch (Exception e4) {
                    e = e4;
                    stremioManifest5 = stremioManifest9;
                    Log.e("AddonRepository", "Failed to install configured addon: " + stremioManifest5.getId(), e);
                    objA = s38.a(e);
                }
                break;
            case 7:
                s38.b(obj);
                installedAddon2 = obj;
                objA = (InstalledAddon) installedAddon2;
                if (objA == null) {
                    return new q38(s38.a(new IllegalStateException("Failed to retrieve installed addon")));
                }
                this.this$0.triggerCloudSync();
                return new q38(objA);
            default:
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
        }
    }
}
