package i3;

import android.content.Context;
import j$.time.Instant;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import m3.AbstractC2467b;
import org.json.JSONArray;
import org.json.JSONObject;
import w4.C3137A;

/* JADX INFO: loaded from: classes.dex */
public final class x extends C4.j implements J4.e {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public AtomicInteger f19050a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public String f19051b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public JSONArray f19052c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public JSONArray f19053d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public Instant f19054e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public Instant f19055f;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public int f19056j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public int f19057k;
    public int l;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public final /* synthetic */ J f19058m;

    /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
    public x(J j4, A4.d dVar) {
        super(2, dVar);
        this.f19058m = j4;
    }

    @Override // C4.a
    public final A4.d create(Object obj, A4.d dVar) {
        return new x(this.f19058m, dVar);
    }

    @Override // J4.e
    public final Object invoke(Object obj, Object obj2) {
        return ((x) create((V4.C) obj, (A4.d) obj2)).invokeSuspend(C3137A.f25453a);
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:102:0x0237  */
    /* JADX WARN: Removed duplicated region for block: B:103:0x0238 A[Catch: all -> 0x0024, TryCatch #1 {all -> 0x0024, blocks: (B:8:0x001c, B:95:0x01c0, B:100:0x01d5, B:103:0x0238, B:104:0x023e, B:106:0x0245, B:110:0x0251, B:111:0x0252, B:98:0x01cd, B:82:0x018f, B:88:0x019b, B:91:0x01a2, B:105:0x023f), top: B:116:0x000a, inners: #0 }] */
    /* JADX WARN: Removed duplicated region for block: B:46:0x0115  */
    /* JADX WARN: Removed duplicated region for block: B:54:0x0132  */
    /* JADX WARN: Removed duplicated region for block: B:61:0x0143  */
    /* JADX WARN: Removed duplicated region for block: B:62:0x0146 A[Catch: all -> 0x00f6, TRY_ENTER, TryCatch #3 {all -> 0x00f6, blocks: (B:39:0x00f9, B:43:0x0103, B:47:0x0116, B:51:0x0120, B:57:0x0137, B:62:0x0146, B:68:0x0152, B:71:0x0159, B:23:0x0067, B:29:0x00cc, B:26:0x00c4), top: B:118:0x0067 }] */
    /* JADX WARN: Removed duplicated region for block: B:77:0x0182  */
    /* JADX WARN: Removed duplicated region for block: B:78:0x0183 A[Catch: all -> 0x0045, TRY_LEAVE, TryCatch #2 {all -> 0x0045, blocks: (B:15:0x003d, B:75:0x0178, B:78:0x0183, B:20:0x0055, B:33:0x00eb), top: B:116:0x000a }] */
    /* JADX WARN: Removed duplicated region for block: B:82:0x018f A[Catch: all -> 0x0024, TRY_ENTER, TryCatch #1 {all -> 0x0024, blocks: (B:8:0x001c, B:95:0x01c0, B:100:0x01d5, B:103:0x0238, B:104:0x023e, B:106:0x0245, B:110:0x0251, B:111:0x0252, B:98:0x01cd, B:82:0x018f, B:88:0x019b, B:91:0x01a2, B:105:0x023f), top: B:116:0x000a, inners: #0 }] */
    /* JADX WARN: Removed duplicated region for block: B:97:0x01cb  */
    /* JADX WARN: Removed duplicated region for block: B:98:0x01cd A[Catch: all -> 0x0024, TryCatch #1 {all -> 0x0024, blocks: (B:8:0x001c, B:95:0x01c0, B:100:0x01d5, B:103:0x0238, B:104:0x023e, B:106:0x0245, B:110:0x0251, B:111:0x0252, B:98:0x01cd, B:82:0x018f, B:88:0x019b, B:91:0x01a2, B:105:0x023f), top: B:116:0x000a, inners: #0 }] */
    /* JADX WARN: Type inference failed for: r5v0 */
    /* JADX WARN: Type inference failed for: r5v1 */
    /* JADX WARN: Type inference failed for: r5v15 */
    /* JADX WARN: Type inference failed for: r5v16, types: [java.util.concurrent.atomic.AtomicInteger] */
    /* JADX WARN: Type inference failed for: r5v17 */
    /* JADX WARN: Type inference failed for: r5v2 */
    /* JADX WARN: Type inference failed for: r5v21 */
    /* JADX WARN: Type inference failed for: r5v22 */
    /* JADX WARN: Type inference failed for: r5v3, types: [java.util.concurrent.atomic.AtomicInteger] */
    /* JADX WARN: Type inference failed for: r5v4 */
    /* JADX WARN: Type inference failed for: r5v8 */
    /* JADX WARN: Type inference failed for: r5v9, types: [java.util.concurrent.atomic.AtomicInteger] */
    @Override // C4.a
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object invokeSuspend(Object obj) throws Throwable {
        AtomicInteger atomicIntegerB;
        String syncTime;
        JSONArray jSONArrayS;
        JSONArray jSONArrayS2;
        Instant instantW;
        Instant instantW2;
        JSONObject jSONObject;
        Object objK;
        AtomicInteger atomicInteger;
        Instant instant;
        boolean z6;
        int i6;
        Object objD;
        Instant instant2;
        AtomicInteger atomicInteger2;
        int i7;
        JSONArray jSONArray;
        Object objD2;
        int i8;
        String str;
        Context context;
        B4.a aVar = B4.a.f492a;
        int i9 = this.l;
        ?? r5 = 2;
        try {
            try {
                if (i9 == 0) {
                    AbstractC2467b.l(obj);
                    atomicIntegerB = J.b(this.f19058m, EnumC2273g.f18933c);
                    try {
                        syncTime = Instant.now().toString();
                        kotlin.jvm.internal.o.g(syncTime, "toString(...)");
                        J j4 = this.f19058m;
                        jSONArrayS = J.s(j4, j4.f18889a.f18895a.getString("history_movie_payload", null));
                        J j6 = this.f19058m;
                        jSONArrayS2 = J.s(j6, j6.f18889a.f18895a.getString("history_show_payload", null));
                        J j7 = this.f19058m;
                        instantW = J.w(j7, j7.f18889a.f18895a.getString("history_movie_sync_time", null));
                        J j8 = this.f19058m;
                        instantW2 = J.w(j8, j8.f18889a.f18895a.getString("history_show_sync_time", null));
                        if ((jSONArrayS.length() > 0 && instantW != null) || (jSONArrayS2.length() > 0 && instantW2 != null)) {
                            J j9 = this.f19058m;
                            this.f19050a = atomicIntegerB;
                            this.f19051b = syncTime;
                            this.f19052c = jSONArrayS;
                            this.f19053d = jSONArrayS2;
                            this.f19054e = instantW;
                            this.f19055f = instantW2;
                            this.l = 1;
                            objK = j9.K("https://api.trakt.tv/sync/last_activities", this);
                            if (objK != aVar) {
                                atomicInteger = atomicIntegerB;
                                instant = instantW2;
                            }
                            return aVar;
                        }
                        jSONObject = null;
                        z6 = (jSONArrayS.length() > 0 || instantW == null || jSONObject == null || J.a(this.f19058m, jSONObject, instantW, "movies", new String[]{"watched_at"})) ? false : true;
                        i6 = (jSONArrayS2.length() > 0 || instantW2 == null || jSONObject == null || J.a(this.f19058m, jSONObject, instantW2, "episodes", new String[]{"watched_at"})) ? 0 : 1;
                        if (!z6 && i6 != 0) {
                            C3137A c3137a = C3137A.f25453a;
                            J.h(this.f19058m, EnumC2273g.f18933c, atomicIntegerB);
                            return c3137a;
                        }
                        if (!z6) {
                            r5 = atomicIntegerB;
                            jSONArray = jSONArrayS;
                            if (i6 == 0) {
                            }
                            JSONArray jSONArrayG = J.g(this.f19058m, jSONArray, true);
                            JSONArray jSONArrayG2 = J.g(this.f19058m, jSONArrayS2, false);
                            String string = jSONArrayG.toString();
                            kotlin.jvm.internal.o.g(string, "toString(...)");
                            String string2 = jSONArrayG2.toString();
                            kotlin.jvm.internal.o.g(string2, "toString(...)");
                            K k6 = this.f19058m.f18889a;
                            k6.getClass();
                            kotlin.jvm.internal.o.h(syncTime, "syncTime");
                            k6.f18895a.edit().putString("history_movie_sync_time", syncTime).putString("history_movie_payload", string).apply();
                            K k7 = this.f19058m.f18889a;
                            k7.getClass();
                            k7.f18895a.edit().putString("history_show_sync_time", syncTime).putString("history_show_payload", string2).apply();
                            Context context2 = M.f18897a;
                            context = M.f18897a;
                            if (context != null) {
                            }
                            J.h(this.f19058m, EnumC2273g.f18933c, r5);
                            return C3137A.f25453a;
                        }
                        int i10 = (jSONArrayS.length() == 0 || instantW == null) ? 1 : 0;
                        J j10 = this.f19058m;
                        if (i10 != 0) {
                            instantW = null;
                        }
                        this.f19050a = atomicIntegerB;
                        this.f19051b = syncTime;
                        this.f19052c = jSONArrayS;
                        this.f19053d = jSONArrayS2;
                        this.f19054e = instantW2;
                        this.f19055f = null;
                        this.f19056j = i6;
                        this.f19057k = i10;
                        this.l = 2;
                        AtomicLong atomicLong = J.f18885e;
                        objD = j10.D("https://api.trakt.tv/sync/history/movies?extended=full,images", instantW, 5, this);
                        if (objD != aVar) {
                            instant2 = instantW2;
                            atomicInteger2 = atomicIntegerB;
                            i7 = i10;
                            JSONArray jSONArrayT = J.t(this.f19058m, (JSONArray) objD, true);
                            if (i7 != 0) {
                            }
                            jSONArray = jSONArrayT;
                            r5 = atomicInteger2;
                            instantW2 = instant2;
                            if (i6 == 0) {
                            }
                            JSONArray jSONArrayG3 = J.g(this.f19058m, jSONArray, true);
                            JSONArray jSONArrayG22 = J.g(this.f19058m, jSONArrayS2, false);
                            String string3 = jSONArrayG3.toString();
                            kotlin.jvm.internal.o.g(string3, "toString(...)");
                            String string22 = jSONArrayG22.toString();
                            kotlin.jvm.internal.o.g(string22, "toString(...)");
                            K k62 = this.f19058m.f18889a;
                            k62.getClass();
                            kotlin.jvm.internal.o.h(syncTime, "syncTime");
                            k62.f18895a.edit().putString("history_movie_sync_time", syncTime).putString("history_movie_payload", string3).apply();
                            K k72 = this.f19058m.f18889a;
                            k72.getClass();
                            k72.f18895a.edit().putString("history_show_sync_time", syncTime).putString("history_show_payload", string22).apply();
                            Context context22 = M.f18897a;
                            context = M.f18897a;
                            if (context != null) {
                            }
                            J.h(this.f19058m, EnumC2273g.f18933c, r5);
                            return C3137A.f25453a;
                        }
                        return aVar;
                    } catch (Throwable th) {
                        th = th;
                        r5 = atomicIntegerB;
                        J.h(this.f19058m, EnumC2273g.f18933c, r5);
                        throw th;
                    }
                }
                if (i9 != 1) {
                    if (i9 != 2) {
                        if (i9 != 3) {
                            throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                        }
                        i8 = this.f19056j;
                        jSONArray = this.f19053d;
                        JSONArray jSONArray2 = this.f19052c;
                        str = this.f19051b;
                        AtomicInteger atomicInteger3 = this.f19050a;
                        AbstractC2467b.l(obj);
                        jSONArrayS2 = jSONArray2;
                        objD2 = obj;
                        r5 = atomicInteger3;
                        JSONArray jSONArrayT2 = J.t(this.f19058m, (JSONArray) objD2, false);
                        jSONArrayS2 = i8 == 0 ? jSONArrayT2 : J.u(this.f19058m, jSONArrayS2, jSONArrayT2);
                        syncTime = str;
                        JSONArray jSONArrayG32 = J.g(this.f19058m, jSONArray, true);
                        JSONArray jSONArrayG222 = J.g(this.f19058m, jSONArrayS2, false);
                        String string32 = jSONArrayG32.toString();
                        kotlin.jvm.internal.o.g(string32, "toString(...)");
                        String string222 = jSONArrayG222.toString();
                        kotlin.jvm.internal.o.g(string222, "toString(...)");
                        K k622 = this.f19058m.f18889a;
                        k622.getClass();
                        kotlin.jvm.internal.o.h(syncTime, "syncTime");
                        k622.f18895a.edit().putString("history_movie_sync_time", syncTime).putString("history_movie_payload", string32).apply();
                        K k722 = this.f19058m.f18889a;
                        k722.getClass();
                        k722.f18895a.edit().putString("history_show_sync_time", syncTime).putString("history_show_payload", string222).apply();
                        Context context222 = M.f18897a;
                        context = M.f18897a;
                        if (context != null) {
                            Set setB = M.b(string32, string222);
                            synchronized (M.f18898b) {
                                M.c(context, setB);
                                M.e(setB);
                            }
                        }
                        J.h(this.f19058m, EnumC2273g.f18933c, r5);
                        return C3137A.f25453a;
                    }
                    i7 = this.f19057k;
                    int i11 = this.f19056j;
                    instant2 = this.f19054e;
                    jSONArrayS2 = this.f19053d;
                    jSONArrayS = this.f19052c;
                    syncTime = this.f19051b;
                    atomicInteger2 = this.f19050a;
                    AbstractC2467b.l(obj);
                    i6 = i11;
                    objD = obj;
                    JSONArray jSONArrayT3 = J.t(this.f19058m, (JSONArray) objD, true);
                    if (i7 != 0) {
                        jSONArrayT3 = J.u(this.f19058m, jSONArrayS, jSONArrayT3);
                    }
                    jSONArray = jSONArrayT3;
                    r5 = atomicInteger2;
                    instantW2 = instant2;
                    if (i6 == 0) {
                        int i12 = (jSONArrayS2.length() == 0 || instantW2 == null) ? 1 : 0;
                        J j11 = this.f19058m;
                        if (i12 != 0) {
                            instantW2 = null;
                        }
                        this.f19050a = r5;
                        this.f19051b = syncTime;
                        this.f19052c = jSONArrayS2;
                        this.f19053d = jSONArray;
                        this.f19054e = null;
                        this.f19055f = null;
                        this.f19056j = i12;
                        this.l = 3;
                        AtomicLong atomicLong2 = J.f18885e;
                        objD2 = j11.D("https://api.trakt.tv/sync/history/shows?extended=full,images", instantW2, 15, this);
                        if (objD2 != aVar) {
                            i8 = i12;
                            str = syncTime;
                            r5 = r5;
                            JSONArray jSONArrayT22 = J.t(this.f19058m, (JSONArray) objD2, false);
                            if (i8 == 0) {
                            }
                            syncTime = str;
                        }
                        return aVar;
                    }
                    JSONArray jSONArrayG322 = J.g(this.f19058m, jSONArray, true);
                    JSONArray jSONArrayG2222 = J.g(this.f19058m, jSONArrayS2, false);
                    String string322 = jSONArrayG322.toString();
                    kotlin.jvm.internal.o.g(string322, "toString(...)");
                    String string2222 = jSONArrayG2222.toString();
                    kotlin.jvm.internal.o.g(string2222, "toString(...)");
                    K k6222 = this.f19058m.f18889a;
                    k6222.getClass();
                    kotlin.jvm.internal.o.h(syncTime, "syncTime");
                    k6222.f18895a.edit().putString("history_movie_sync_time", syncTime).putString("history_movie_payload", string322).apply();
                    K k7222 = this.f19058m.f18889a;
                    k7222.getClass();
                    k7222.f18895a.edit().putString("history_show_sync_time", syncTime).putString("history_show_payload", string2222).apply();
                    Context context2222 = M.f18897a;
                    context = M.f18897a;
                    if (context != null) {
                    }
                    J.h(this.f19058m, EnumC2273g.f18933c, r5);
                    return C3137A.f25453a;
                }
                instant = this.f19055f;
                instantW = this.f19054e;
                jSONArrayS2 = this.f19053d;
                jSONArrayS = this.f19052c;
                syncTime = this.f19051b;
                atomicInteger = this.f19050a;
                AbstractC2467b.l(obj);
                objK = obj;
                jSONObject = (JSONObject) objK;
                AtomicInteger atomicInteger4 = atomicInteger;
                instantW2 = instant;
                atomicIntegerB = atomicInteger4;
                if (jSONArrayS.length() > 0) {
                    if (jSONArrayS2.length() > 0) {
                        if (!z6) {
                        }
                        if (!z6) {
                        }
                    }
                }
                JSONArray jSONArrayG3222 = J.g(this.f19058m, jSONArray, true);
                JSONArray jSONArrayG22222 = J.g(this.f19058m, jSONArrayS2, false);
                String string3222 = jSONArrayG3222.toString();
                kotlin.jvm.internal.o.g(string3222, "toString(...)");
                String string22222 = jSONArrayG22222.toString();
                kotlin.jvm.internal.o.g(string22222, "toString(...)");
                K k62222 = this.f19058m.f18889a;
                k62222.getClass();
                kotlin.jvm.internal.o.h(syncTime, "syncTime");
                k62222.f18895a.edit().putString("history_movie_sync_time", syncTime).putString("history_movie_payload", string3222).apply();
                K k72222 = this.f19058m.f18889a;
                k72222.getClass();
                k72222.f18895a.edit().putString("history_show_sync_time", syncTime).putString("history_show_payload", string22222).apply();
                Context context22222 = M.f18897a;
                context = M.f18897a;
                if (context != null) {
                }
                J.h(this.f19058m, EnumC2273g.f18933c, r5);
                return C3137A.f25453a;
            } catch (Throwable th2) {
                th = th2;
                J.h(this.f19058m, EnumC2273g.f18933c, r5);
                throw th;
            }
        } catch (Throwable th3) {
            th = th3;
            r5 = atomicInteger2;
        }
    }
}
