package i3;

import android.app.ActivityManager;
import android.content.Context;
import android.os.Process;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import m3.AbstractC2467b;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import org.json.JSONException;
import org.json.JSONObject;
import w4.C3137A;

/* JADX INFO: renamed from: i3.c, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public final class C2269c extends C4.j implements J4.e {

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public kotlin.jvm.internal.B f18914b;

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

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

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

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public /* synthetic */ Object f18918f;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public final /* synthetic */ Context f18919j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public final /* synthetic */ C2267a f18920k;

    /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
    public C2269c(Context context, C2267a c2267a, A4.d dVar) {
        super(2, dVar);
        this.f18919j = context;
        this.f18920k = c2267a;
    }

    @Override // C4.a
    public final A4.d create(Object obj, A4.d dVar) {
        C2269c c2269c = new C2269c(this.f18919j, this.f18920k, dVar);
        c2269c.f18918f = obj;
        return c2269c;
    }

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

    /* JADX WARN: Can't wrap try/catch for region: R(15:16|(1:18)(1:19)|(4:24|(2:25|(2:27|(1:169)(1:30))(2:168|31))|32|(1:162)(1:37))(1:166)|41|(1:43)|44|158|45|156|46|(2:153|48)(1:54)|55|(1:57)|58|(7:151|60|61|146|62|63|64)(24:69|149|70|(3:72|148|73)(1:77)|142|78|82|(1:84)|85|(1:87)|(1:89)(1:90)|91|92|(10:109|(0)|155|123|135|(1:170)|138|167|14|(3:164|139|140)(0))(2:94|(2:104|(1:106)(2:107|108))(2:96|(2:102|103)))|111|(4:115|(1:120)|121|122)|155|123|135|(0)|138|167|14|(0)(0))) */
    /* JADX WARN: Can't wrap try/catch for region: R(23:149|70|(3:72|148|73)(1:77)|142|78|82|(1:84)|85|(1:87)|(1:89)(1:90)|91|92|(10:109|(0)|155|123|135|(1:170)|138|167|14|(3:164|139|140)(0))(2:94|(2:104|(1:106)(2:107|108))(2:96|(2:102|103)))|111|(4:115|(1:120)|121|122)|155|123|135|(0)|138|167|14|(0)(0)) */
    /* JADX WARN: Can't wrap try/catch for region: R(24:69|149|70|(3:72|148|73)(1:77)|142|78|82|(1:84)|85|(1:87)|(1:89)(1:90)|91|92|(10:109|(0)|155|123|135|(1:170)|138|167|14|(3:164|139|140)(0))(2:94|(2:104|(1:106)(2:107|108))(2:96|(2:102|103)))|111|(4:115|(1:120)|121|122)|155|123|135|(0)|138|167|14|(0)(0)) */
    /* JADX WARN: Code restructure failed: missing block: B:134:0x025b, code lost:
    
        r7 = r2;
        r24 = r3;
     */
    /* JADX WARN: Code restructure failed: missing block: B:80:0x01d2, code lost:
    
        r0 = move-exception;
     */
    /* JADX WARN: Code restructure failed: missing block: B:81:0x01d3, code lost:
    
        r2 = m3.AbstractC2467b.d(r0);
     */
    /* JADX WARN: Finally extract failed */
    /* JADX WARN: Removed duplicated region for block: B:164:0x0281 A[SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:16:0x0090  */
    /* JADX WARN: Removed duplicated region for block: B:170:? A[RETURN, SYNTHETIC] */
    /* JADX WARN: Unsupported multi-entry loop pattern (BACK_EDGE: B:136:0x0277 -> B:138:0x027a). Please report as a decompilation issue!!! */
    @Override // C4.a
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object invokeSuspend(Object obj) throws JSONException, IOException {
        kotlin.jvm.internal.B b5;
        K k6;
        long j4;
        long j6;
        V4.C c6;
        Request.Builder builder;
        String str;
        B4.a aVar;
        long j7;
        Object objL;
        B4.a aVar2;
        Response responseD;
        Response response;
        Throwable th;
        String strD;
        String strOptString;
        List<ActivityManager.RunningAppProcessInfo> runningAppProcesses;
        Object next;
        String str2 = "getString(...)";
        B4.a aVar3 = B4.a.f492a;
        int i6 = this.f18917e;
        int i7 = 1;
        if (i6 == 0) {
            AbstractC2467b.l(obj);
            V4.C c7 = (V4.C) this.f18918f;
            K k7 = new K(this.f18919j);
            long jCurrentTimeMillis = System.currentTimeMillis();
            b5 = new kotlin.jvm.internal.B();
            C2267a c2267a = this.f18920k;
            long j8 = c2267a.f18912e;
            if (j8 < 1) {
                j8 = 1;
            }
            b5.f20114a = j8;
            k6 = k7;
            j4 = ((long) c2267a.f18911d) * 1000;
            j6 = jCurrentTimeMillis;
            c6 = c7;
        } else if (i6 == 1) {
            j4 = this.f18916d;
            long j9 = this.f18915c;
            kotlin.jvm.internal.B b6 = this.f18914b;
            K k8 = this.f18913a;
            V4.C c8 = (V4.C) this.f18918f;
            AbstractC2467b.l(obj);
            c6 = c8;
            b5 = b6;
            k6 = k8;
            j6 = j9;
        } else {
            if (i6 != 2) {
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
            }
            j4 = this.f18916d;
            long j10 = this.f18915c;
            kotlin.jvm.internal.B b7 = this.f18914b;
            K k9 = this.f18913a;
            V4.C c9 = (V4.C) this.f18918f;
            AbstractC2467b.l(obj);
            j7 = 1000;
            c6 = c9;
            str = "getString(...)";
            aVar2 = aVar3;
            b5 = b7;
            k6 = k9;
            j6 = j10;
            aVar3 = aVar2;
            str2 = str;
            i7 = 1;
        }
        do {
            V4.E.m(c6.getCoroutineContext());
            if (System.currentTimeMillis() - j6 <= j4) {
                try {
                    OkHttpClient okHttpClient = AbstractC2270d.f18921a;
                    Object systemService = this.f18919j.getSystemService("activity");
                    ActivityManager activityManager = systemService instanceof ActivityManager ? (ActivityManager) systemService : null;
                    if (activityManager != null && (runningAppProcesses = activityManager.getRunningAppProcesses()) != null) {
                        Iterator<T> it = runningAppProcesses.iterator();
                        while (true) {
                            if (!it.hasNext()) {
                                next = null;
                                break;
                            }
                            next = it.next();
                            if (((ActivityManager.RunningAppProcessInfo) next).pid == Process.myPid()) {
                                break;
                            }
                        }
                        ActivityManager.RunningAppProcessInfo runningAppProcessInfo = (ActivityManager.RunningAppProcessInfo) next;
                        if (runningAppProcessInfo != null && runningAppProcessInfo.importance > 200) {
                            this.f18918f = c6;
                            this.f18913a = k6;
                            this.f18914b = b5;
                            this.f18915c = j6;
                            this.f18916d = j4;
                            this.f18917e = i7;
                        }
                    }
                    JSONObject jSONObjectPut = new JSONObject().put("code", this.f18920k.f18908a).put("client_id", "cb6c23120401d06808b06c16c78d0e54e1953df0fce43d921280c708619c1b34");
                    builder = new Request.Builder();
                    builder.i("https://api.trakt.tv/oauth/device/token");
                    builder.a("Content-Type", "application/json");
                    String str3 = W.d.f8386g;
                    if (str3 == null) {
                        str3 = "DebridStream/3.0.0";
                    }
                    builder.a("User-Agent", str3);
                    RequestBody.Companion companion = RequestBody.f22136a;
                    String string = jSONObjectPut.toString();
                    kotlin.jvm.internal.o.g(string, "toString(...)");
                    MediaType.f22043d.getClass();
                    MediaType mediaTypeB = MediaType.Companion.b("application/json");
                    companion.getClass();
                    builder.h(RequestBody.Companion.a(string, mediaTypeB));
                    ResponseBody responseBody = responseD.f22146j;
                    if (responseBody != null) {
                        try {
                            strD = responseBody.d();
                        } catch (Throwable th2) {
                            th = th2;
                            str = str2;
                            aVar = aVar3;
                            response = responseD;
                            th = th;
                            try {
                                throw th;
                            } catch (Throwable th3) {
                                android.support.v4.media.session.b.j(response, th);
                                throw th3;
                            }
                        }
                    } else {
                        strD = null;
                    }
                    String str4 = "";
                    if (strD == null) {
                        strD = "";
                    }
                    if (responseD.c()) {
                        try {
                            JSONObject jSONObject = new JSONObject(strD);
                            String string2 = jSONObject.getString("access_token");
                            kotlin.jvm.internal.o.g(string2, str2);
                            String string3 = jSONObject.getString("refresh_token");
                            kotlin.jvm.internal.o.g(string3, str2);
                            str = str2;
                            aVar = aVar3;
                            try {
                                k6.b((jSONObject.getLong("expires_in") * 1000) + System.currentTimeMillis(), string2, string3);
                                C3137A c3137a = C3137A.f25453a;
                                responseD.close();
                                return c3137a;
                            } catch (Throwable th4) {
                                th = th4;
                                th = th;
                                response = responseD;
                                throw th;
                            }
                        } catch (Throwable th5) {
                            th = th5;
                            str = str2;
                            aVar = aVar3;
                        }
                    } else {
                        str = str2;
                        aVar = aVar3;
                        try {
                            if (S4.p.D0(strD, "slow_down", true)) {
                                response = responseD;
                                try {
                                    b5.f20114a += (long) 5;
                                } catch (Throwable th6) {
                                    th = th6;
                                    th = th;
                                    throw th;
                                }
                            } else {
                                response = responseD;
                            }
                            Object objD = new JSONObject(strD);
                            if (objD instanceof w4.m) {
                                objD = null;
                            }
                            JSONObject jSONObject2 = (JSONObject) objD;
                            String strOptString2 = jSONObject2 != null ? jSONObject2.optString("error", "") : null;
                            if (strOptString2 != null) {
                                str4 = strOptString2;
                            }
                            int iHashCode = str4.hashCode();
                            if (iHashCode == -1916631087) {
                                if (!str4.equals("authorization_pending")) {
                                }
                                response.close();
                                j7 = 1000;
                                long j11 = b5.f20114a * 1000;
                                this.f18918f = c6;
                                this.f18913a = k6;
                                this.f18914b = b5;
                                this.f18915c = j6;
                                this.f18916d = j4;
                                this.f18917e = 2;
                                objL = V4.E.l(j11, this);
                                aVar2 = aVar;
                                if (objL == aVar2) {
                                    return aVar2;
                                }
                                aVar3 = aVar2;
                                str2 = str;
                                i7 = 1;
                                V4.E.m(c6.getCoroutineContext());
                                if (System.currentTimeMillis() - j6 <= j4) {
                                    throw new IOException("Device code expired");
                                }
                            } else if (iHashCode == -444618026) {
                                if (str4.equals("access_denied")) {
                                    throw new IOException("Trakt authorization was denied");
                                }
                            } else if (iHashCode == 1612125279 && str4.equals("expired_token")) {
                                throw new IOException("Device code expired");
                            }
                            if (str4.length() > 0 && !str4.equals("authorization_pending")) {
                                if (jSONObject2 != null && (strOptString = jSONObject2.optString("error_description")) != null) {
                                    str4 = strOptString;
                                }
                                throw new IOException(str4);
                            }
                            response.close();
                            j7 = 1000;
                            long j112 = b5.f20114a * 1000;
                            this.f18918f = c6;
                            this.f18913a = k6;
                            this.f18914b = b5;
                            this.f18915c = j6;
                            this.f18916d = j4;
                            this.f18917e = 2;
                            objL = V4.E.l(j112, this);
                            aVar2 = aVar;
                            if (objL == aVar2) {
                            }
                            aVar3 = aVar2;
                            str2 = str;
                            i7 = 1;
                            V4.E.m(c6.getCoroutineContext());
                            if (System.currentTimeMillis() - j6 <= j4) {
                            }
                        } catch (Throwable th7) {
                            th = th7;
                            response = responseD;
                            th = th;
                            throw th;
                        }
                    }
                } catch (Throwable th8) {
                    th = th8;
                    str = str2;
                    aVar = aVar3;
                }
                responseD = AbstractC2270d.f18921a.a(builder.b()).d();
            }
        } while (V4.E.l(1000L, this) != aVar3);
        return aVar3;
    }
}
