package V2;

import a.AbstractC0872a;
import android.content.Context;
import androidx.media3.extractor.text.ttml.TtmlNode;
import com.debridstream.tv.data.home.HomeMetaItem;
import g3.AbstractC2060b;
import j$.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import m3.AbstractC2467b;
import n2.AbstractC2612c;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import s2.SharedPreferencesC2907b;
import x4.AbstractC3229F;
import x4.AbstractC3251q;
import x4.AbstractC3253s;
import x4.AbstractC3257w;
import y4.C3274b;

/* JADX INFO: renamed from: V2.p0, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public final class C0706p0 {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public static final Y4.c0 f7996a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static final Y4.J f7997b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public static final Y4.c0 f7998c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public static final Y4.J f7999d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public static final Y4.c0 f8000e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public static final Y4.J f8001f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public static final Y4.c0 f8002g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public static final Y4.J f8003h;

    static {
        V4.D0 d0D = V4.E.d();
        c5.e eVar = V4.N.f8259a;
        a5.c cVarC = V4.E.c(AbstractC0872a.a0(d0D, a5.m.f10845a.f8429e));
        x4.z zVar = x4.z.f26066a;
        Y4.c0 c0VarC = Y4.O.c(zVar);
        f7996a = c0VarC;
        Y4.J j4 = z3.f8213b;
        Y4.F f6 = new Y4.F(c0VarC, j4, new C0698n0(3, null, 0));
        Y4.U u6 = Y4.S.f8899a;
        x4.y yVar = x4.y.f26065a;
        f7997b = Y4.O.o(f6, cVarC, u6, yVar);
        Y4.c0 c0VarC2 = Y4.O.c(zVar);
        f7998c = c0VarC2;
        f7999d = Y4.O.o(new Y4.F(c0VarC2, j4, new C0698n0(3, null, 1)), cVarC, u6, yVar);
        Y4.c0 c0VarC3 = Y4.O.c(new JSONArray());
        f8000e = c0VarC3;
        f8001f = new Y4.J(c0VarC3);
        Y4.c0 c0VarC4 = Y4.O.c(yVar);
        f8002g = c0VarC4;
        f8003h = new Y4.J(c0VarC4);
    }

    public static final Instant a(JSONObject jSONObject) {
        Object objD;
        try {
            String strOptString = jSONObject.optString("sortDate");
            kotlin.jvm.internal.o.e(strOptString);
            objD = S4.p.Q0(strOptString) ? Instant.EPOCH : Instant.parse(strOptString);
        } catch (Throwable th) {
            objD = AbstractC2467b.d(th);
        }
        Instant instant = Instant.EPOCH;
        if (objD instanceof w4.m) {
            objD = instant;
        }
        kotlin.jvm.internal.o.g(objD, "getOrDefault(...)");
        return (Instant) objD;
    }

    public static List b(String profileKey) {
        kotlin.jvm.internal.o.h(profileKey, "profileKey");
        List list = (List) ((Map) f7996a.getValue()).get(profileKey);
        return list == null ? x4.y.f26065a : list;
    }

    public static void c() {
        Y4.c0 c0Var = f7996a;
        x4.z zVar = x4.z.f26066a;
        c0Var.getClass();
        c0Var.k(null, zVar);
        Y4.c0 c0Var2 = f7998c;
        c0Var2.getClass();
        c0Var2.k(null, zVar);
        Y4.c0 c0Var3 = f8000e;
        JSONArray jSONArray = new JSONArray();
        c0Var3.getClass();
        c0Var3.k(null, jSONArray);
        Y4.c0 c0Var4 = f8002g;
        x4.y yVar = x4.y.f26065a;
        c0Var4.getClass();
        c0Var4.k(null, yVar);
        synchronized (AbstractC2060b.f16469a) {
            AbstractC2060b.f16470b.clear();
        }
    }

    public static HomeMetaItem d(JSONObject jo) {
        int i6;
        String str;
        String str2;
        List listE;
        kotlin.jvm.internal.o.h(jo, "jo");
        int iOptInt = jo.optInt("tmdbId", 0);
        String lowerCase = A0.a.p(jo, "type", "", "optString(...)").toLowerCase(Locale.ROOT);
        kotlin.jvm.internal.o.g(lowerCase, "toLowerCase(...)");
        if (iOptInt <= 0 || !(lowerCase.equals("movie") || lowerCase.equals("tv"))) {
            return null;
        }
        w4.k kVarX0 = W.a.x0(jo.optJSONArray("genres"));
        String str3 = (String) kVarX0.f25469a;
        String str4 = (String) kVarX0.f25470b;
        String strOptString = jo.optString("primaryGenre");
        String str5 = S4.p.Q0(strOptString) ? str3 : strOptString;
        String strOptString2 = jo.optString("secondaryGenre");
        String str6 = S4.p.Q0(strOptString2) ? str4 : strOptString2;
        String strOptString3 = jo.optString("id");
        kotlin.jvm.internal.o.g(strOptString3, "optString(...)");
        String strOptString4 = jo.optString("title");
        kotlin.jvm.internal.o.g(strOptString4, "optString(...)");
        String strOptString5 = jo.optString("overview");
        kotlin.jvm.internal.o.g(strOptString5, "optString(...)");
        String strOptString6 = jo.optString("year");
        kotlin.jvm.internal.o.g(strOptString6, "optString(...)");
        double dOptDouble = jo.optDouble("rating");
        String strOptString7 = jo.optString("posterUrl");
        if (S4.p.Q0(strOptString7)) {
            strOptString7 = null;
        }
        String strOptString8 = jo.optString("backdropUrl");
        if (S4.p.Q0(strOptString8)) {
            strOptString8 = null;
        }
        String strOptString9 = jo.optString("logoUrl");
        String str7 = S4.p.Q0(strOptString9) ? null : strOptString9;
        String strOptString10 = jo.optString("certificate");
        kotlin.jvm.internal.o.g(strOptString10, "optString(...)");
        String strI = W0.e.i(strOptString10);
        int iOptInt2 = jo.optInt("runTimeMinutes");
        kotlin.jvm.internal.o.e(str5);
        kotlin.jvm.internal.o.e(str6);
        JSONArray jSONArrayOptJSONArray = jo.optJSONArray("originCountries");
        if (jSONArrayOptJSONArray == null || jSONArrayOptJSONArray.length() == 0) {
            i6 = iOptInt;
            str = lowerCase;
            str2 = str7;
            listE = x4.y.f26065a;
        } else {
            i6 = iOptInt;
            C3274b c3274bK = AbstractC2612c.k();
            str = lowerCase;
            int length = jSONArrayOptJSONArray.length();
            str2 = str7;
            int i7 = 0;
            while (i7 < length) {
                int i8 = length;
                String strOptString11 = jSONArrayOptJSONArray.optString(i7, "");
                kotlin.jvm.internal.o.g(strOptString11, "optString(...)");
                JSONArray jSONArray = jSONArrayOptJSONArray;
                String upperCase = S4.p.o1(strOptString11).toString().toUpperCase(Locale.ROOT);
                kotlin.jvm.internal.o.g(upperCase, "toUpperCase(...)");
                int i9 = i7;
                if (upperCase.length() == 2) {
                    c3274bK.add(upperCase);
                }
                i7 = i9 + 1;
                length = i8;
                jSONArrayOptJSONArray = jSONArray;
            }
            listE = AbstractC2612c.e(c3274bK);
        }
        String lowerCase2 = A0.a.p(jo, "originalLanguage", "", "optString(...)").toLowerCase(Locale.ROOT);
        kotlin.jvm.internal.o.g(lowerCase2, "toLowerCase(...)");
        return new HomeMetaItem(strOptString3, i6, str, strOptString4, strOptString5, strOptString6, dOptDouble, strOptString7, strOptString8, str2, strI, iOptInt2, str5, str6, listE, lowerCase2);
    }

    public static void e(int i6, String profileKey, String type) throws JSONException {
        kotlin.jvm.internal.o.h(profileKey, "profileKey");
        kotlin.jvm.internal.o.h(type, "type");
        if (i6 <= 0) {
            return;
        }
        String lowerCase = S4.p.o1(type).toString().toLowerCase(Locale.ROOT);
        kotlin.jvm.internal.o.g(lowerCase, "toLowerCase(...)");
        if (S4.p.Q0(lowerCase)) {
            lowerCase = "movie";
        }
        AbstractC2060b.e(i6, profileKey, lowerCase);
        Y4.c0 c0Var = f7996a;
        LinkedHashMap linkedHashMapI = AbstractC3229F.I((Map) c0Var.getValue());
        Iterable iterable = (List) linkedHashMapI.get(profileKey);
        if (iterable == null) {
            iterable = x4.y.f26065a;
        }
        ArrayList arrayList = new ArrayList();
        for (Object obj : iterable) {
            JSONObject jSONObject = (JSONObject) obj;
            if (jSONObject.optInt("tmdbId", 0) == i6) {
                String lowerCase2 = A0.a.p(jSONObject, "type", "movie", "optString(...)").toLowerCase(Locale.ROOT);
                kotlin.jvm.internal.o.g(lowerCase2, "toLowerCase(...)");
                if (S4.p.Q0(lowerCase2)) {
                    lowerCase2 = "movie";
                }
                if (!lowerCase2.equals(lowerCase)) {
                }
            }
            arrayList.add(obj);
        }
        linkedHashMapI.put(profileKey, arrayList);
        c0Var.getClass();
        c0Var.k(null, linkedHashMapI);
        AbstractC0696m3.j(linkedHashMapI);
    }

    public static void f(int i6, String type) throws JSONException {
        kotlin.jvm.internal.o.h(type, "type");
        if (i6 <= 0) {
            return;
        }
        String lowerCase = S4.p.o1(type).toString().toLowerCase(Locale.ROOT);
        kotlin.jvm.internal.o.g(lowerCase, "toLowerCase(...)");
        if (S4.p.Q0(lowerCase)) {
            lowerCase = "movie";
        }
        String str = (String) ((Y4.c0) z3.f8213b.f8871a).getValue();
        Y4.c0 c0Var = f7998c;
        LinkedHashMap linkedHashMapI = AbstractC3229F.I((Map) c0Var.getValue());
        Iterable iterable = (List) linkedHashMapI.get(str);
        if (iterable == null) {
            iterable = x4.y.f26065a;
        }
        ArrayList arrayList = new ArrayList();
        for (Object obj : iterable) {
            HomeMetaItem homeMetaItem = (HomeMetaItem) obj;
            if (homeMetaItem.getTmdbId() == i6) {
                String lowerCase2 = S4.p.o1(homeMetaItem.getType()).toString().toLowerCase(Locale.ROOT);
                kotlin.jvm.internal.o.g(lowerCase2, "toLowerCase(...)");
                if (S4.p.Q0(lowerCase2)) {
                    lowerCase2 = "movie";
                }
                if (lowerCase2.equals(lowerCase)) {
                }
            }
            arrayList.add(obj);
        }
        linkedHashMapI.put(str, arrayList);
        c0Var.getClass();
        c0Var.k(null, linkedHashMapI);
        LinkedHashMap linkedHashMap = new LinkedHashMap(AbstractC3229F.u(linkedHashMapI.size()));
        for (Map.Entry entry : linkedHashMapI.entrySet()) {
            Object key = entry.getKey();
            List list = (List) entry.getValue();
            ArrayList arrayList2 = new ArrayList(AbstractC3253s.G(list, 10));
            Iterator it = list.iterator();
            while (it.hasNext()) {
                arrayList2.add(k((HomeMetaItem) it.next()));
            }
            linkedHashMap.put(key, arrayList2);
        }
        AbstractC0696m3.k(linkedHashMap);
    }

    public static void g(String profileKey, List list) throws JSONException {
        kotlin.jvm.internal.o.h(profileKey, "profileKey");
        Y4.c0 c0Var = f7996a;
        LinkedHashMap linkedHashMapI = AbstractC3229F.I((Map) c0Var.getValue());
        linkedHashMapI.put(profileKey, list);
        c0Var.getClass();
        c0Var.k(null, linkedHashMapI);
        AbstractC0696m3.j(linkedHashMapI);
    }

    public static void h(String profileKey, List items) throws JSONException {
        kotlin.jvm.internal.o.h(profileKey, "profileKey");
        kotlin.jvm.internal.o.h(items, "items");
        Y4.c0 c0Var = f7998c;
        LinkedHashMap linkedHashMapI = AbstractC3229F.I((Map) c0Var.getValue());
        linkedHashMapI.put(profileKey, items);
        c0Var.getClass();
        c0Var.k(null, linkedHashMapI);
        LinkedHashMap linkedHashMap = new LinkedHashMap(AbstractC3229F.u(linkedHashMapI.size()));
        for (Map.Entry entry : linkedHashMapI.entrySet()) {
            Object key = entry.getKey();
            List list = (List) entry.getValue();
            ArrayList arrayList = new ArrayList(AbstractC3253s.G(list, 10));
            Iterator it = list.iterator();
            while (it.hasNext()) {
                arrayList.add(k((HomeMetaItem) it.next()));
            }
            linkedHashMap.put(key, arrayList);
        }
        AbstractC0696m3.k(linkedHashMap);
    }

    public static void i(String str) {
        if (str == null || S4.p.Q0(str)) {
            Y4.c0 c0Var = f8002g;
            x4.y yVar = x4.y.f26065a;
            c0Var.getClass();
            c0Var.k(null, yVar);
            Context context = AbstractC0696m3.f7937d0;
            if (context == null) {
                return;
            }
            context.getSharedPreferences("debridstream_app_progress_cache", 0).edit().putString("trakt_continue_watching_json", "[]").apply();
            return;
        }
        try {
            JSONArray jSONArray = new JSONArray(str);
            ArrayList arrayList = new ArrayList(jSONArray.length());
            int length = jSONArray.length();
            for (int i6 = 0; i6 < length; i6++) {
                JSONObject jSONObjectOptJSONObject = jSONArray.optJSONObject(i6);
                if (jSONObjectOptJSONObject != null) {
                    arrayList.add(AbstractC2060b.g(jSONObjectOptJSONObject));
                }
            }
            Y4.c0 c0Var2 = f8002g;
            c0Var2.getClass();
            c0Var2.k(null, arrayList);
            JSONArray jSONArray2 = new JSONArray();
            Iterator it = arrayList.iterator();
            while (it.hasNext()) {
                jSONArray2.put((JSONObject) it.next());
            }
            String string = jSONArray2.toString();
            kotlin.jvm.internal.o.g(string, "toString(...)");
            Context context2 = AbstractC0696m3.f7937d0;
            if (context2 == null) {
                return;
            }
            context2.getSharedPreferences("debridstream_app_progress_cache", 0).edit().putString("trakt_continue_watching_json", string).apply();
        } catch (Throwable th) {
            AbstractC2467b.d(th);
        }
    }

    public static void j(JSONArray items) {
        kotlin.jvm.internal.o.h(items, "items");
        Y4.c0 c0Var = f8000e;
        c0Var.getClass();
        c0Var.k(null, items);
        String string = items.toString();
        Context context = AbstractC0696m3.f7937d0;
        if (context == null) {
            return;
        }
        context.getSharedPreferences("debridstream_app_progress_cache", 0).edit().putString("trakt_watchlist_json", string).apply();
    }

    public static JSONObject k(HomeMetaItem homeMetaItem) throws JSONException {
        JSONObject jSONObjectPut = new JSONObject().put("id", homeMetaItem.getId()).put("tmdbId", homeMetaItem.getTmdbId()).put("type", homeMetaItem.getType()).put("title", homeMetaItem.getTitle()).put("overview", homeMetaItem.getOverview()).put("year", homeMetaItem.getYear()).put("rating", homeMetaItem.getRating()).put("posterUrl", homeMetaItem.getPosterUrl()).put("backdropUrl", homeMetaItem.getBackdropUrl()).put("logoUrl", homeMetaItem.getLogoUrl()).put("certificate", homeMetaItem.getCertificate()).put("runTimeMinutes", homeMetaItem.getRunTimeMinutes()).put("primaryGenre", homeMetaItem.getPrimaryGenre()).put("secondaryGenre", homeMetaItem.getSecondaryGenre());
        JSONArray jSONArray = new JSONArray();
        Iterator<T> it = homeMetaItem.getOriginCountries().iterator();
        while (it.hasNext()) {
            jSONArray.put((String) it.next());
        }
        JSONObject jSONObjectPut2 = jSONObjectPut.put("originCountries", jSONArray).put("originalLanguage", homeMetaItem.getOriginalLanguage());
        kotlin.jvm.internal.o.g(jSONObjectPut2, "put(...)");
        return jSONObjectPut2;
    }

    public static void l(HomeMetaItem item) throws JSONException {
        kotlin.jvm.internal.o.h(item, "item");
        if (item.getTmdbId() <= 0) {
            return;
        }
        String lowerCase = S4.p.o1(item.getType()).toString().toLowerCase(Locale.ROOT);
        kotlin.jvm.internal.o.g(lowerCase, "toLowerCase(...)");
        if (S4.p.Q0(lowerCase)) {
            lowerCase = "movie";
        }
        String str = (String) ((Y4.c0) z3.f8213b.f8871a).getValue();
        Y4.c0 c0Var = f7998c;
        LinkedHashMap linkedHashMapI = AbstractC3229F.I((Map) c0Var.getValue());
        Collection collection = (List) linkedHashMapI.get(str);
        if (collection == null) {
            collection = x4.y.f26065a;
        }
        ArrayList arrayListE0 = AbstractC3251q.E0(collection);
        AbstractC3257w.P(new B.B0(18, item, lowerCase), arrayListE0);
        arrayListE0.add(0, item);
        linkedHashMapI.put(str, arrayListE0);
        c0Var.getClass();
        c0Var.k(null, linkedHashMapI);
        LinkedHashMap linkedHashMap = new LinkedHashMap(AbstractC3229F.u(linkedHashMapI.size()));
        for (Map.Entry entry : linkedHashMapI.entrySet()) {
            Object key = entry.getKey();
            List list = (List) entry.getValue();
            ArrayList arrayList = new ArrayList(AbstractC3253s.G(list, 10));
            Iterator it = list.iterator();
            while (it.hasNext()) {
                arrayList.add(k((HomeMetaItem) it.next()));
            }
            linkedHashMap.put(key, arrayList);
        }
        AbstractC0696m3.k(linkedHashMap);
    }

    public static void m(Context context, JSONObject jSONObject) throws JSONException {
        String str;
        int i6 = 0;
        Instant sortInstant = Instant.now();
        kotlin.jvm.internal.o.h(sortInstant, "sortInstant");
        int iOptInt = jSONObject.optInt("tmdbId", 0);
        String lowerCase = A0.a.p(jSONObject, "type", "movie", "optString(...)").toLowerCase(Locale.ROOT);
        kotlin.jvm.internal.o.g(lowerCase, "toLowerCase(...)");
        String str2 = S4.p.Q0(lowerCase) ? "movie" : lowerCase;
        if (iOptInt <= 0) {
            return;
        }
        Object obj = AbstractC2060b.f16469a;
        String strOptString = jSONObject.optString("localStreamUrl");
        kotlin.jvm.internal.o.e(strOptString);
        String str3 = !S4.p.Q0(strOptString) ? strOptString : null;
        String strOptString2 = jSONObject.optString("localLinkHash");
        kotlin.jvm.internal.o.e(strOptString2);
        if (S4.p.Q0(strOptString2)) {
            strOptString2 = null;
        }
        String strOptString3 = jSONObject.optString("imdbId");
        kotlin.jvm.internal.o.e(strOptString3);
        if (!S4.w.A0(strOptString3, TtmlNode.TAG_TT, true)) {
            strOptString3 = null;
        }
        String strOptString4 = jSONObject.optString("localDebridProvider");
        kotlin.jvm.internal.o.e(strOptString4);
        if (S4.p.Q0(strOptString4)) {
            strOptString4 = null;
            str = null;
        } else {
            str = null;
        }
        String str4 = str;
        AbstractC2060b.d("main", str2, str3, strOptString2, iOptInt, strOptString3, strOptString4);
        JSONObject jSONObjectG = AbstractC2060b.g(new JSONObject(jSONObject.toString()));
        jSONObjectG.put("sortDate", sortInstant.toString());
        ArrayList arrayListE0 = AbstractC3251q.E0((Collection) f8002g.getValue());
        Iterator it = arrayListE0.iterator();
        int i7 = 0;
        while (true) {
            if (!it.hasNext()) {
                i7 = -1;
                break;
            }
            JSONObject jSONObject2 = (JSONObject) it.next();
            if (jSONObject2.optInt("tmdbId", 0) == iOptInt) {
                String lowerCase2 = A0.a.p(jSONObject2, "type", "movie", "optString(...)").toLowerCase(Locale.ROOT);
                kotlin.jvm.internal.o.g(lowerCase2, "toLowerCase(...)");
                if (S4.p.Q0(lowerCase2)) {
                    lowerCase2 = "movie";
                }
                if (lowerCase2.equals(str2)) {
                    break;
                }
            }
            i7++;
        }
        if (i7 >= 0) {
            arrayListE0.set(i7, jSONObjectG);
        } else {
            arrayListE0.add(jSONObjectG);
        }
        List listX0 = AbstractC3251q.x0(new C0702o0(i6), arrayListE0);
        Y4.c0 c0Var = f8002g;
        c0Var.getClass();
        c0Var.k(str4, listX0);
        Context applicationContext = context.getApplicationContext();
        kotlin.jvm.internal.o.e(applicationContext);
        Context applicationContext2 = applicationContext.getApplicationContext();
        kotlin.jvm.internal.o.g(applicationContext2, "getApplicationContext(...)");
        SharedPreferencesC2907b sharedPreferencesC2907b = (SharedPreferencesC2907b) W.c.D(applicationContext2);
        String string = sharedPreferencesC2907b.getString("continue_watching_sync_time", str4);
        if (string == null) {
            string = sortInstant.toString();
            kotlin.jvm.internal.o.g(string, "toString(...)");
        }
        JSONArray jSONArray = new JSONArray();
        Iterator it2 = listX0.iterator();
        while (it2.hasNext()) {
            jSONArray.put((JSONObject) it2.next());
        }
        String string2 = jSONArray.toString();
        kotlin.jvm.internal.o.g(string2, "toString(...)");
        sharedPreferencesC2907b.edit().putString("continue_watching_sync_time", string).putString("continue_watching_payload", string2).apply();
        Context context2 = AbstractC0696m3.f7937d0;
        if (context2 == null) {
            return;
        }
        context2.getSharedPreferences("debridstream_app_progress_cache", 0).edit().putString("trakt_continue_watching_json", string2).apply();
    }
}
