package e5;

import B6.k;
import B6.t;
import P6.n;
import R0.m;
import a5.C0405d;
import android.content.SharedPreferences;
import com.google.android.gms.internal.measurement.C1;
import com.martsnew.data.model.response.LoginResponse;
import g6.o;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.ProtocolException;
import java.net.Proxy;
import java.net.SocketTimeoutException;
import java.security.cert.CertificateException;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLHandshakeException;
import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLSocketFactory;
import k7.A;
import k7.B;
import k7.C;
import k7.C1109a;
import k7.C1114f;
import k7.D;
import k7.E;
import k7.F;
import k7.l;
import k7.p;
import k7.q;
import k7.r;
import k7.s;
import k7.w;
import k7.y;
import m4.C1179o;
import n4.u0;
import r7.C1511a;

/* JADX INFO: loaded from: classes.dex */
public final class i implements r {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final /* synthetic */ int f12492a = 0;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final Object f12493b;

    public i(C0405d c0405d) {
        this.f12493b = c0405d;
    }

    public static int d(C c3, int i) {
        String strA = C.a("Retry-After", c3);
        if (strA == null) {
            return i;
        }
        Pattern patternCompile = Pattern.compile("\\d+");
        P6.g.d(patternCompile, "compile(...)");
        if (!patternCompile.matcher(strA).matches()) {
            return Integer.MAX_VALUE;
        }
        Integer numValueOf = Integer.valueOf(strA);
        P6.g.d(numValueOf, "valueOf(header)");
        return numValueOf.intValue();
    }

    /* JADX WARN: Removed duplicated region for block: B:147:0x030c  */
    @Override // k7.r
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final C a(p7.d dVar) {
        Object objJ;
        E e8;
        SSLSocketFactory sSLSocketFactory;
        HostnameVerifier hostnameVerifier;
        C1114f c1114f;
        switch (this.f12492a) {
            case u0.f15495q /* 0 */:
                String strK = o.f12958a.K();
                P6.d dVarA = n.a(LoginResponse.class);
                boolean zEquals = dVarA.equals(n.a(Float.TYPE));
                C0405d c0405d = (C0405d) this.f12493b;
                SharedPreferences sharedPreferences = c0405d.f7619a;
                if (!zEquals) {
                    if (dVarA.equals(n.a(Integer.TYPE))) {
                        objJ = (LoginResponse) Integer.valueOf(sharedPreferences.getInt(strK, 0));
                    } else if (dVarA.equals(n.a(Long.TYPE))) {
                        objJ = (LoginResponse) Long.valueOf(sharedPreferences.getLong(strK, 0L));
                    } else if (dVarA.equals(n.a(String.class))) {
                        Object string = sharedPreferences.getString(strK, "");
                        if (string == null) {
                            throw new NullPointerException("null cannot be cast to non-null type com.martsnew.data.model.response.LoginResponse");
                        }
                        objJ = (LoginResponse) string;
                    } else if (dVarA.equals(n.a(Boolean.TYPE))) {
                        objJ = (LoginResponse) Boolean.valueOf(sharedPreferences.getBoolean(strK, false));
                    } else {
                        String string2 = sharedPreferences.getString(strK, "");
                        if (string2 == null || string2.length() == 0) {
                            objJ = null;
                        } else {
                            try {
                                objJ = c0405d.f7620b.a(LoginResponse.class).a(string2);
                            } catch (Throwable th) {
                                objJ = C1.j(th);
                            }
                            if (objJ instanceof A6.i) {
                            }
                        }
                    }
                    break;
                } else {
                    objJ = (LoginResponse) Float.valueOf(sharedPreferences.getFloat(strK, 0.0f));
                }
                LoginResponse loginResponse = (LoginResponse) objJ;
                y yVar = dVar.f16161e;
                if (loginResponse == null) {
                    return dVar.b(yVar);
                }
                boolean zA = P6.g.a(yVar.f14204c.a("IncludeAuthorization"), "true");
                K2.e eVarB = yVar.b();
                eVarB.K("IncludeAuthorization");
                y yVarQ = eVarB.q();
                if (!zA) {
                    return dVar.b(yVarQ);
                }
                String str = o.f12958a.A() + " " + loginResponse.f10852b.f10967a.f10971a;
                K2.e eVarB2 = yVar.b();
                eVarB2.p("Authorization", str);
                return dVar.b(eVarB2.q());
            case 1:
                y yVar2 = dVar.f16161e;
                K2.e eVarB3 = yVar2.b();
                long j8 = -1;
                A a3 = yVar2.f14205d;
                if (a3 != null) {
                    s sVarB = a3.b();
                    if (sVarB != null) {
                        eVarB3.G("Content-Type", sVarB.f14132a);
                    }
                    long jA = a3.a();
                    if (jA != -1) {
                        eVarB3.G("Content-Length", String.valueOf(jA));
                        eVarB3.K("Transfer-Encoding");
                    } else {
                        eVarB3.G("Transfer-Encoding", "chunked");
                        eVarB3.K("Content-Length");
                    }
                }
                k7.o oVar = yVar2.f14204c;
                String strA = oVar.a("Host");
                boolean z8 = false;
                q qVar = yVar2.f14202a;
                if (strA == null) {
                    eVarB3.G("Host", l7.b.w(qVar, false));
                }
                if (oVar.a("Connection") == null) {
                    eVarB3.G("Connection", "Keep-Alive");
                }
                if (oVar.a("Accept-Encoding") == null && oVar.a("Range") == null) {
                    eVarB3.G("Accept-Encoding", "gzip");
                    z8 = true;
                }
                l lVar = (l) this.f12493b;
                lVar.getClass();
                P6.g.e(qVar, "url");
                if (oVar.a("User-Agent") == null) {
                    eVarB3.G("User-Agent", "okhttp/4.12.0");
                }
                C cB = dVar.b(eVarB3.q());
                k7.o oVar2 = cB.f14015v;
                p7.c.b(lVar, qVar, oVar2);
                B bG = cB.g();
                bG.f13995a = yVar2;
                if (z8 && "gzip".equalsIgnoreCase(C.a("Content-Encoding", cB)) && p7.c.a(cB) && (e8 = cB.f14016w) != null) {
                    x7.q qVar2 = new x7.q(e8.g());
                    N0.c cVarF = oVar2.f();
                    cVarF.k("Content-Encoding");
                    cVarF.k("Content-Length");
                    bG.f14000f = cVarF.i().f();
                    bG.f14001g = new D(C.a("Content-Type", cB), j8, s7.l.c(qVar2), 1);
                }
                return bG.a();
            default:
                y yVar3 = dVar.f16161e;
                o7.i iVar = dVar.f16157a;
                List listR0 = t.f1032q;
                C c3 = null;
                int i = 0;
                y yVarB = yVar3;
                while (true) {
                    boolean z9 = true;
                    while (true) {
                        iVar.getClass();
                        P6.g.e(yVarB, "request");
                        if (iVar.f15977y != null) {
                            throw new IllegalStateException("Check failed.");
                        }
                        synchronized (iVar) {
                            try {
                                if (iVar.f15965A) {
                                    throw new IllegalStateException("cannot make a new request because the previous response is still open: please call response.close()");
                                }
                                if (iVar.f15978z) {
                                    throw new IllegalStateException("Check failed.");
                                }
                            } catch (Throwable th2) {
                                throw th2;
                            }
                        }
                        if (z9) {
                            m mVar = iVar.f15971s;
                            q qVar3 = yVarB.f14202a;
                            boolean z10 = qVar3.f14129j;
                            w wVar = iVar.f15969q;
                            if (z10) {
                                SSLSocketFactory sSLSocketFactory2 = wVar.f14173E;
                                if (sSLSocketFactory2 == null) {
                                    throw new IllegalStateException("CLEARTEXT-only client");
                                }
                                HostnameVerifier hostnameVerifier2 = wVar.f14176I;
                                c1114f = wVar.f14177J;
                                sSLSocketFactory = sSLSocketFactory2;
                                hostnameVerifier = hostnameVerifier2;
                            } else {
                                sSLSocketFactory = null;
                                hostnameVerifier = null;
                                c1114f = null;
                            }
                            iVar.f15975w = new o7.e(mVar, new C1109a(qVar3.f14124d, qVar3.f14125e, wVar.f14170A, wVar.D, sSLSocketFactory, hostnameVerifier, c1114f, wVar.f14172C, wVar.H, wVar.f14175G, wVar.f14171B), iVar);
                        }
                        try {
                            if (iVar.f15967C) {
                                throw new IOException("Canceled");
                            }
                            try {
                                C cB2 = dVar.b(yVarB);
                                if (c3 != null) {
                                    B bG2 = cB2.g();
                                    B bG3 = c3.g();
                                    bG3.f14001g = null;
                                    C cA = bG3.a();
                                    if (cA.f14016w != null) {
                                        throw new IllegalArgumentException("priorResponse.body != null");
                                    }
                                    bG2.f14003j = cA;
                                    cB2 = bG2.a();
                                }
                                c3 = cB2;
                                yVarB = b(c3, iVar.f15977y);
                                if (yVarB == null) {
                                    iVar.h(false);
                                    return c3;
                                }
                                E e9 = c3.f14016w;
                                if (e9 != null) {
                                    l7.b.d(e9);
                                }
                                i++;
                                if (i > 20) {
                                    throw new ProtocolException("Too many follow-up requests: " + i);
                                }
                                iVar.h(true);
                            } catch (IOException e10) {
                                if (!c(e10, iVar, yVarB, !(e10 instanceof C1511a))) {
                                    Iterator it = listR0.iterator();
                                    while (it.hasNext()) {
                                        android.support.v4.media.session.a.a(e10, (Exception) it.next());
                                    }
                                    throw e10;
                                }
                                listR0 = k.r0(listR0, e10);
                                iVar.h(true);
                                z9 = false;
                            } catch (o7.m e11) {
                                if (!c(e11.f15999r, iVar, yVarB, false)) {
                                    IOException iOException = e11.f15998q;
                                    P6.g.e(iOException, "<this>");
                                    Iterator it2 = listR0.iterator();
                                    while (it2.hasNext()) {
                                        android.support.v4.media.session.a.a(iOException, (Exception) it2.next());
                                    }
                                    throw iOException;
                                }
                                listR0 = k.r0(listR0, e11.f15998q);
                                iVar.h(true);
                                z9 = false;
                            }
                        } catch (Throwable th3) {
                            iVar.h(true);
                            throw th3;
                        }
                    }
                }
                break;
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:66:0x00ba  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public y b(C c3, o7.d dVar) throws ProtocolException {
        String strA;
        C c8;
        o7.l lVar;
        F f3 = (dVar == null || (lVar = dVar.f15951e) == null) ? null : lVar.f15983b;
        int i = c3.f14013t;
        String str = c3.f14010q.f14203b;
        if (i == 307 || i == 308) {
            w wVar = (w) this.f12493b;
            if (wVar.f14191x && (strA = C.a("Location", c3)) != null) {
                y yVar = c3.f14010q;
                q qVar = yVar.f14202a;
                qVar.getClass();
                p pVarF = qVar.f(strA);
                q qVarA = pVarF != null ? pVarF.a() : null;
                if (qVarA != null) {
                    if (P6.g.a(qVarA.f14121a, yVar.f14202a.f14121a) || wVar.f14192y) {
                        K2.e eVarB = yVar.b();
                        if (W3.b.r(str)) {
                            boolean zEquals = str.equals("PROPFIND");
                            int i5 = c3.f14013t;
                            boolean z8 = zEquals || i5 == 308 || i5 == 307;
                            if (str.equals("PROPFIND") || i5 == 308 || i5 == 307) {
                                eVarB.J(str, z8 ? yVar.f14205d : null);
                            } else {
                                eVarB.J("GET", null);
                            }
                            if (!z8) {
                                eVarB.K("Transfer-Encoding");
                                eVarB.K("Content-Length");
                                eVarB.K("Content-Type");
                            }
                        }
                        if (!l7.b.a(yVar.f14202a, qVarA)) {
                            eVarB.K("Authorization");
                        }
                        eVarB.f3552q = qVarA;
                        return eVarB.q();
                    }
                }
            }
        } else {
            if (i == 401) {
                return ((w) this.f12493b).f14190w.j(f3, c3);
            }
            if (i != 421) {
                if (i == 503) {
                    C c9 = c3.f14019z;
                    if ((c9 == null || c9.f14013t != 503) && d(c3, Integer.MAX_VALUE) == 0) {
                        return c3.f14010q;
                    }
                } else {
                    if (i == 407) {
                        P6.g.b(f3);
                        if (f3.f14025b.type() != Proxy.Type.HTTP) {
                            throw new ProtocolException("Received HTTP_PROXY_AUTH (407) code while not using proxy");
                        }
                        ((w) this.f12493b).f14172C.getClass();
                        return null;
                    }
                    if (i != 408) {
                        switch (i) {
                            case 300:
                            case 301:
                            case 302:
                            case 303:
                            default:
                                return null;
                        }
                    } else if (((w) this.f12493b).f14189v && (((c8 = c3.f14019z) == null || c8.f14013t != 408) && d(c3, 0) <= 0)) {
                        return c3.f14010q;
                    }
                }
            } else if (dVar != null && !P6.g.a(dVar.f15948b.f15953b.f14041h.f14124d, dVar.f15951e.f15983b.f14024a.f14041h.f14124d)) {
                o7.l lVar2 = dVar.f15951e;
                synchronized (lVar2) {
                    lVar2.f15991k = true;
                }
                return c3.f14010q;
            }
        }
        return null;
    }

    /* JADX WARN: Removed duplicated region for block: B:33:0x0049  */
    /* JADX WARN: Removed duplicated region for block: B:36:0x004e  */
    /* JADX WARN: Removed duplicated region for block: B:63:0x0085  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public boolean c(IOException iOException, o7.i iVar, y yVar, boolean z8) {
        o7.e eVar;
        int i;
        boolean zJ;
        F4.a aVar;
        o7.l lVar;
        if (!((w) this.f12493b).f14189v || ((z8 && (iOException instanceof FileNotFoundException)) || (iOException instanceof ProtocolException))) {
            return false;
        }
        if (!(iOException instanceof InterruptedIOException)) {
            if (((iOException instanceof SSLHandshakeException) && (iOException.getCause() instanceof CertificateException)) || (iOException instanceof SSLPeerUnverifiedException)) {
                return false;
            }
            eVar = iVar.f15975w;
            P6.g.b(eVar);
            i = eVar.f15957f;
            if (i != 0) {
                if (eVar.i == null) {
                }
            }
        } else if ((iOException instanceof SocketTimeoutException) && !z8) {
            eVar = iVar.f15975w;
            P6.g.b(eVar);
            i = eVar.f15957f;
            if (i != 0 && eVar.f15958g == 0 && eVar.f15959h == 0) {
                zJ = false;
            } else if (eVar.i == null) {
                zJ = true;
            } else {
                F f3 = null;
                if (i <= 1 && eVar.f15958g <= 1 && eVar.f15959h <= 0 && (lVar = eVar.f15954c.f15976x) != null) {
                    synchronized (lVar) {
                        if (lVar.f15992l == 0 && l7.b.a(lVar.f15983b.f14024a.f14041h, eVar.f15953b.f14041h)) {
                            f3 = lVar.f15983b;
                        }
                    }
                }
                if (f3 != null) {
                    eVar.i = f3;
                } else {
                    C1179o c1179o = eVar.f15955d;
                    if ((c1179o == null || !c1179o.b()) && (aVar = eVar.f15956e) != null) {
                        zJ = aVar.j();
                    }
                }
                zJ = true;
            }
            if (zJ) {
                return true;
            }
        }
        return false;
    }

    public i(l lVar) {
        P6.g.e(lVar, "cookieJar");
        this.f12493b = lVar;
    }

    public i(w wVar) {
        P6.g.e(wVar, "client");
        this.f12493b = wVar;
    }
}
