package com.debridstream.tv;

import S4.p;
import T0.C0606q;
import V2.AbstractC0696m3;
import V2.P2;
import V2.Q2;
import Y4.H;
import Y4.O;
import Y4.a0;
import Y4.c0;
import androidx.compose.runtime.internal.StabilityInferred;
import androidx.lifecycle.U;
import androidx.lifecycle.b0;
import com.google.android.gms.common.internal.ImagesContract;
import g3.n;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import kotlin.jvm.internal.o;
import m3.AbstractC2467b;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import w4.m;

/* JADX INFO: loaded from: classes.dex */
@StabilityInferred(parameters = 0)
public final class PlayerViewModel extends b0 {
    public static final int $stable = 8;
    private final H _uiState;
    private final AtomicLong playbackSessionId;
    private final AtomicBoolean progressHistoryMarked;
    private final U savedStateHandle;
    private final a0 uiState;
    private final AtomicBoolean upNextPrefetchAttempted;

    public PlayerViewModel(U savedStateHandle) {
        o.h(savedStateHandle, "savedStateHandle");
        this.savedStateHandle = savedStateHandle;
        c0 c0VarC = O.c(restoredPlayerUiState());
        this._uiState = c0VarC;
        this.uiState = c0VarC;
        this.progressHistoryMarked = new AtomicBoolean(false);
        this.upNextPrefetchAttempted = new AtomicBoolean(false);
        this.playbackSessionId = new AtomicLong(1L);
    }

    public static /* synthetic */ void prepare$default(PlayerViewModel playerViewModel, String str, String str2, String str3, P2 p22, boolean z6, Long l, n nVar, String str4, boolean z7, int i6, Object obj) throws JSONException {
        if ((i6 & 2) != 0) {
            str2 = null;
        }
        if ((i6 & 4) != 0) {
            str3 = null;
        }
        if ((i6 & 8) != 0) {
            p22 = null;
        }
        if ((i6 & 16) != 0) {
            z6 = false;
        }
        if ((i6 & 32) != 0) {
            l = null;
        }
        if ((i6 & 64) != 0) {
            nVar = null;
        }
        if ((i6 & 128) != 0) {
            str4 = null;
        }
        if ((i6 & 256) != 0) {
            z7 = false;
        }
        playerViewModel.prepare(str, str2, str3, p22, z6, l, nVar, str4, z7);
    }

    private final Q2 restoredPlayerUiState() {
        Object objD;
        U u6 = this.savedStateHandle;
        o.h(u6, "<this>");
        String str = (String) u6.a("nav_player_snapshot_json");
        Q2 q22 = null;
        String string = str != null ? p.o1(str).toString() : null;
        if (string == null) {
            string = "";
        }
        if (!p.Q0(string)) {
            try {
                objD = AbstractC0696m3.e(new JSONObject(string));
            } catch (Throwable th) {
                objD = AbstractC2467b.d(th);
            }
            q22 = (Q2) (objD instanceof m ? null : objD);
        }
        if (q22 == null) {
            return new Q2(null, null, null, null, null, null, false, null, 8191);
        }
        return q22;
    }

    public final void clearInitialSeekPosition() {
        c0 c0Var;
        Object value;
        Q2 q2A;
        H h5 = this._uiState;
        do {
            c0Var = (c0) h5;
            value = c0Var.getValue();
            q2A = (Q2) value;
            if (q2A.f7506k != null) {
                q2A = Q2.a(q2A, null, null, 4095);
            }
        } while (!c0Var.i(value, q2A));
    }

    public final void clearPendingOpenLinksPanelForManualPick() {
        c0 c0Var;
        Object value;
        Q2 q2A;
        H h5 = this._uiState;
        do {
            c0Var = (c0) h5;
            value = c0Var.getValue();
            q2A = (Q2) value;
            if (q2A.f7505j) {
                q2A = Q2.a(q2A, null, null, 6143);
            }
        } while (!c0Var.i(value, q2A));
    }

    public final long currentPlaybackSessionId() {
        return this.playbackSessionId.get();
    }

    public final AtomicBoolean getProgressHistoryMarked() {
        return this.progressHistoryMarked;
    }

    public final a0 getUiState() {
        return this.uiState;
    }

    /* JADX WARN: Removed duplicated region for block: B:17:0x0033  */
    /* JADX WARN: Removed duplicated region for block: B:73:0x00dc  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void prepare(String url, String str, String str2, P2 p22, boolean z6, Long l, n nVar, String str3, boolean z7) throws JSONException {
        String str4;
        boolean z8;
        o.h(url, "url");
        Q2 q22 = (Q2) ((c0) this._uiState).getValue();
        P2 p23 = p22 == null ? z6 ? q22.f7500e : null : p22;
        n nVar2 = nVar == null ? z6 ? q22.f7502g : null : nVar;
        if (str3 != null) {
            str4 = !p.Q0(str3) ? str3 : null;
        } else if (z6) {
            str4 = q22.f7501f;
        }
        boolean zC = o.c(q22.f7496a, url);
        Integer numValueOf = p23 != null ? Integer.valueOf(p23.f7478a) : null;
        P2 p24 = q22.f7500e;
        if (o.c(numValueOf, p24 != null ? Integer.valueOf(p24.f7478a) : null)) {
            if (o.c(p23 != null ? Boolean.valueOf(p23.f7480c) : null, p24 != null ? Boolean.valueOf(p24.f7480c) : null)) {
                if (o.c(p23 != null ? Integer.valueOf(p23.f7481d) : null, p24 != null ? Integer.valueOf(p24.f7481d) : null)) {
                    if (o.c(p23 != null ? Integer.valueOf(p23.f7482e) : null, p24 != null ? Integer.valueOf(p24.f7482e) : null)) {
                        Integer numValueOf2 = nVar2 != null ? Integer.valueOf(nVar2.f16513a) : null;
                        n nVar3 = q22.f7502g;
                        if (o.c(numValueOf2, nVar3 != null ? Integer.valueOf(nVar3.f16513a) : null)) {
                            if ((nVar2 != null ? nVar2.f16514b : "").equals(nVar3 != null ? nVar3.f16514b : "")) {
                                z8 = false;
                            }
                        }
                    }
                }
            }
        } else {
            z8 = true;
        }
        if (!zC || z8) {
            this.progressHistoryMarked.set(false);
            this.upNextPrefetchAttempted.set(false);
            this.playbackSessionId.incrementAndGet();
        }
        String str5 = (str == null || p.Q0(str)) ? null : str;
        String str6 = (str2 == null || p.Q0(str2)) ? null : str2;
        Long l6 = (l == null || l.longValue() <= 0) ? null : l;
        Q2 q23 = new Q2(url, str5, str6, true, p23, str4, nVar2, null, null, z7, l6);
        c0 c0Var = (c0) this._uiState;
        c0Var.getClass();
        c0Var.k(null, q23);
        U u6 = this.savedStateHandle;
        o.h(u6, "<this>");
        if (p.Q0(url) && !z7) {
            C0606q c0606q = u6.f12136b;
            ((LinkedHashMap) c0606q.f6543b).remove("nav_player_snapshot_json");
            ((LinkedHashMap) c0606q.f6545d).remove("nav_player_snapshot_json");
            ((LinkedHashMap) c0606q.f6546e).remove("nav_player_snapshot_json");
            if (u6.f12135a.remove("nav_player_snapshot_json") != null) {
                throw new ClassCastException();
            }
            return;
        }
        JSONObject jSONObjectPut = new JSONObject().put(ImagesContract.URL, url).put("currentLinkHash", str5).put("currentDebridProviderKey", str6).put("playbackTitleOverride", str4).put("pendingOpenLinksPanelForManualPick", z7);
        Object obj = l6;
        if (l6 == null) {
            obj = JSONObject.NULL;
        }
        JSONObject jSONObjectPut2 = jSONObjectPut.put("initialSeekPositionMs", obj);
        if (p23 != null) {
            JSONObject jSONObjectPut3 = new JSONObject().put("tmdbId", p23.f7478a).put("imdbId", p23.f7479b).put("isTv", p23.f7480c).put("season", p23.f7481d).put("episode", p23.f7482e);
            Object obj2 = p23.f7483f;
            if (obj2 == null) {
                obj2 = JSONObject.NULL;
            }
            jSONObjectPut2.put("openSubtitlesContext", jSONObjectPut3.put("episodeTmdbId", obj2));
        }
        if (nVar2 != null) {
            JSONObject jSONObjectPut4 = new JSONObject().put("tmdbId", nVar2.f16513a).put("type", nVar2.f16514b).put("title", nVar2.f16515c).put("overview", nVar2.f16516d).put("year", nVar2.f16517e).put("rating", nVar2.f16518f).put("posterUrl", nVar2.f16519g).put("backdropUrl", nVar2.f16520h).put("logoUrl", nVar2.f16521i).put("certificate", nVar2.f16522j).put("runTimeMinutes", nVar2.f16523k).put("primaryGenre", nVar2.l);
            JSONArray jSONArray = new JSONArray();
            Iterator it = nVar2.f16524m.iterator();
            while (it.hasNext()) {
                jSONArray.put((String) it.next());
            }
            jSONObjectPut2.put("progressMeta", jSONObjectPut4.put("originCountries", jSONArray).put("originalLanguage", nVar2.f16525n));
        }
        o.e(jSONObjectPut2);
        u6.b(jSONObjectPut2.toString(), "nav_player_snapshot_json");
    }

    public final void setExternalSubtitle(String str, String str2) {
        c0 c0Var;
        Object value;
        Q2 q22;
        String str3;
        String str4;
        H h5 = this._uiState;
        do {
            c0Var = (c0) h5;
            value = c0Var.getValue();
            q22 = (Q2) value;
            str3 = null;
            str4 = (str == null || p.Q0(str)) ? null : str;
            if (str2 != null && !p.Q0(str2)) {
                str3 = str2;
            }
        } while (!c0Var.i(value, Q2.a(q22, str4, str3, 6655)));
    }

    public final boolean tryMarkUpNextPrefetchAttempted() {
        return this.upNextPrefetchAttempted.compareAndSet(false, true);
    }
}
