package okhttp3.internal.cache;

import S4.w;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import kotlin.jvm.internal.o;
import okhttp3.Cache;
import okhttp3.CacheControl;
import okhttp3.EventListener;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Protocol;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okhttp3.internal.Util;
import okhttp3.internal.cache.CacheStrategy;
import okhttp3.internal.cache.DiskLruCache;
import okhttp3.internal.connection.RealCall;
import okhttp3.internal.http.DatesKt;
import okhttp3.internal.http.HttpHeaders;
import okhttp3.internal.http.HttpMethod;
import okhttp3.internal.http.RealInterceptorChain;
import okhttp3.internal.http.RealResponseBody;
import p5.AbstractC2732b;
import p5.C2740j;
import p5.I;
import p5.InterfaceC2742l;
import p5.P;
import p5.S;

/* JADX INFO: loaded from: classes3.dex */
public final class CacheInterceptor implements Interceptor {

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static final Companion f22188b = new Companion(0);

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final Cache f22189a;

    public static final class Companion {
        public /* synthetic */ Companion(int i6) {
            this();
        }

        public static final Response a(Companion companion, Response response) {
            companion.getClass();
            if ((response != null ? response.f22146j : null) == null) {
                return response;
            }
            Response.Builder builderD = response.d();
            builderD.f22159g = null;
            return builderD.a();
        }

        public static boolean b(String str) {
            return ("Connection".equalsIgnoreCase(str) || "Keep-Alive".equalsIgnoreCase(str) || "Proxy-Authenticate".equalsIgnoreCase(str) || "Proxy-Authorization".equalsIgnoreCase(str) || "TE".equalsIgnoreCase(str) || "Trailers".equalsIgnoreCase(str) || "Transfer-Encoding".equalsIgnoreCase(str) || "Upgrade".equalsIgnoreCase(str)) ? false : true;
        }

        private Companion() {
        }
    }

    public CacheInterceptor(Cache cache) {
        this.f22189a = cache;
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:102:0x0204  */
    /* JADX WARN: Removed duplicated region for block: B:62:0x0149  */
    /* JADX WARN: Type inference failed for: r10v0 */
    /* JADX WARN: Type inference failed for: r10v10 */
    /* JADX WARN: Type inference failed for: r10v12 */
    /* JADX WARN: Type inference failed for: r10v17, types: [okhttp3.Request, okhttp3.Response] */
    /* JADX WARN: Type inference failed for: r10v2 */
    /* JADX WARN: Type inference failed for: r10v25 */
    /* JADX WARN: Type inference failed for: r10v26 */
    /* JADX WARN: Type inference failed for: r10v35 */
    /* JADX WARN: Type inference failed for: r10v7 */
    @Override // okhttp3.Interceptor
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Response a(RealInterceptorChain realInterceptorChain) throws Throwable {
        RealCall realCall;
        CacheStrategy cacheStrategy;
        long j4;
        long jMax;
        long j6;
        long j7;
        Date date;
        String string;
        long time;
        Date date2;
        Date date3;
        String str;
        long millis;
        long j8;
        long millis2;
        String str2;
        int i6;
        Object obj;
        ResponseBody responseBody;
        ResponseBody responseBody2;
        RealCall call = realInterceptorChain.f22384a;
        Cache cache = this.f22189a;
        Response responseA = cache != null ? cache.a(realInterceptorChain.f22388e) : null;
        long jCurrentTimeMillis = System.currentTimeMillis();
        Request request = realInterceptorChain.f22388e;
        o.h(request, "request");
        CacheStrategy.Factory factory = new CacheStrategy.Factory();
        factory.f22205i = -1;
        if (responseA != null) {
            factory.f22202f = responseA.f22149n;
            factory.f22203g = responseA.f22150o;
            Headers headers = responseA.f22145f;
            int size = headers.size();
            for (int i7 = 0; i7 < size; i7++) {
                String strH = headers.h(i7);
                String strL = headers.l(i7);
                if (w.t0(strH, "Date", true)) {
                    factory.f22197a = DatesKt.a(strL);
                    factory.f22198b = strL;
                } else if (w.t0(strH, "Expires", true)) {
                    factory.f22201e = DatesKt.a(strL);
                } else if (w.t0(strH, "Last-Modified", true)) {
                    factory.f22199c = DatesKt.a(strL);
                    factory.f22200d = strL;
                } else if (w.t0(strH, "ETag", true)) {
                    factory.f22204h = strL;
                } else if (w.t0(strH, "Age", true)) {
                    factory.f22205i = Util.z(strL, -1);
                }
            }
        }
        if (responseA == null) {
            realCall = 0;
            cacheStrategy = new CacheStrategy(request, null);
        } else {
            realCall = 0;
            realCall = 0;
            if (request.f22125a.f22033j && responseA.f22144e == null) {
                cacheStrategy = new CacheStrategy(request, null);
            } else {
                CacheStrategy.f22194c.getClass();
                if (CacheStrategy.Companion.a(request, responseA)) {
                    CacheControl cacheControlA = request.a();
                    if (!cacheControlA.f21930a && request.f22127c.g("If-Modified-Since") == null && request.f22127c.g("If-None-Match") == null) {
                        CacheControl cacheControlA2 = responseA.a();
                        long j9 = factory.f22203g;
                        Date date4 = factory.f22197a;
                        if (date4 != null) {
                            j4 = jCurrentTimeMillis;
                            jMax = Math.max(0L, j9 - date4.getTime());
                            j6 = 0;
                        } else {
                            j4 = jCurrentTimeMillis;
                            jMax = 0;
                            j6 = 0;
                        }
                        int i8 = factory.f22205i;
                        if (i8 != -1) {
                            j7 = j9;
                            jMax = Math.max(jMax, TimeUnit.SECONDS.toMillis(i8));
                        } else {
                            j7 = j9;
                        }
                        long time2 = factory.f22202f;
                        long j10 = jMax + (j7 - time2) + (j4 - j7);
                        CacheControl cacheControlA3 = responseA.a();
                        Date date5 = factory.f22199c;
                        Date date6 = factory.f22201e;
                        int i9 = cacheControlA3.f21932c;
                        if (i9 != -1) {
                            date = date6;
                            time = TimeUnit.SECONDS.toMillis(i9);
                        } else {
                            date = date6;
                            if (date != null) {
                                time = date.getTime() - (date4 != null ? date4.getTime() : j7);
                                if (time <= j6) {
                                    time = j6;
                                }
                            } else if (date5 != null) {
                                ArrayList arrayList = responseA.f22140a.f22125a.f22030g;
                                if (arrayList == null) {
                                    string = null;
                                } else {
                                    StringBuilder sb = new StringBuilder();
                                    HttpUrl.f22023k.getClass();
                                    HttpUrl.Companion.f(arrayList, sb);
                                    string = sb.toString();
                                }
                                if (string == null) {
                                    if (date4 != null) {
                                        time2 = date4.getTime();
                                    }
                                    long time3 = time2 - date5.getTime();
                                    if (time3 > j6) {
                                        time = time3 / ((long) 10);
                                    }
                                }
                            }
                        }
                        int i10 = cacheControlA.f21932c;
                        if (i10 != -1) {
                            date2 = date;
                            date3 = date5;
                            time = Math.min(time, TimeUnit.SECONDS.toMillis(i10));
                        } else {
                            date2 = date;
                            date3 = date5;
                        }
                        int i11 = cacheControlA.f21938i;
                        if (i11 != -1) {
                            str = "If-None-Match";
                            millis = TimeUnit.SECONDS.toMillis(i11);
                        } else {
                            str = "If-None-Match";
                            millis = j6;
                        }
                        if (cacheControlA2.f21936g || (i6 = cacheControlA.f21937h) == -1) {
                            j8 = time;
                            millis2 = j6;
                        } else {
                            j8 = time;
                            millis2 = TimeUnit.SECONDS.toMillis(i6);
                        }
                        if (!cacheControlA2.f21930a) {
                            long j11 = j10 + millis;
                            if (j11 < j8 + millis2) {
                                Response.Builder builderD = responseA.d();
                                if (j11 >= j8) {
                                    builderD.f22158f.a("Warning", "110 HttpURLConnection \"Response is stale\"");
                                }
                                if (j10 > 86400000 && responseA.a().f21932c == -1 && date2 == null) {
                                    builderD.f22158f.a("Warning", "113 HttpURLConnection \"Heuristic expiration\"");
                                }
                                realCall = 0;
                                cacheStrategy = new CacheStrategy(null, builderD.a());
                            } else {
                                String str3 = factory.f22204h;
                                if (str3 != null) {
                                    str2 = str;
                                } else {
                                    if (date3 != null) {
                                        str3 = factory.f22200d;
                                    } else if (date4 != null) {
                                        str3 = factory.f22198b;
                                    } else {
                                        realCall = 0;
                                        cacheStrategy = new CacheStrategy(request, null);
                                    }
                                    str2 = "If-Modified-Since";
                                }
                                Headers.Builder builderJ = request.f22127c.j();
                                o.e(str3);
                                builderJ.c(str2, str3);
                                Request.Builder builderB = request.b();
                                builderB.f(builderJ.e());
                                cacheStrategy = new CacheStrategy(builderB.b(), responseA);
                                realCall = 0;
                            }
                        }
                    } else {
                        realCall = 0;
                        cacheStrategy = new CacheStrategy(request, null);
                    }
                } else {
                    cacheStrategy = new CacheStrategy(request, null);
                }
            }
        }
        if (cacheStrategy.f22195a != null && request.a().f21939j) {
            cacheStrategy = new CacheStrategy(realCall, realCall);
        }
        Request request2 = cacheStrategy.f22195a;
        Response response = cacheStrategy.f22196b;
        Cache cache2 = this.f22189a;
        if (cache2 != null) {
            synchronized (cache2) {
            }
        }
        if (call != null) {
            realCall = call;
        }
        if (realCall == 0 || (obj = realCall.f22320d) == null) {
            obj = EventListener.f22010a;
        }
        if (responseA != null && response == null && (responseBody2 = responseA.f22146j) != null) {
            Util.c(responseBody2);
        }
        if (request2 == null && response == null) {
            Response.Builder builder = new Response.Builder();
            Request request3 = realInterceptorChain.f22388e;
            o.h(request3, "request");
            builder.f22153a = request3;
            builder.f22154b = Protocol.HTTP_1_1;
            builder.f22155c = 504;
            builder.f22156d = "Unsatisfiable Request (only-if-cached)";
            builder.f22159g = Util.f22181c;
            builder.f22163k = -1L;
            builder.l = System.currentTimeMillis();
            Response responseA2 = builder.a();
            obj.getClass();
            o.h(call, "call");
            return responseA2;
        }
        if (request2 == null) {
            o.e(response);
            Response.Builder builderD2 = response.d();
            Response responseA3 = Companion.a(f22188b, response);
            Response.Builder.b("cacheResponse", responseA3);
            builderD2.f22161i = responseA3;
            Response responseA4 = builderD2.a();
            obj.getClass();
            o.h(call, "call");
            return responseA4;
        }
        if (response != null || this.f22189a != null) {
            obj.getClass();
            o.h(call, "call");
        }
        try {
            Response responseB = realInterceptorChain.b(request2);
            if (response != null) {
                if (responseB.f22143d == 304) {
                    Response.Builder builderD3 = response.d();
                    Companion companion = f22188b;
                    Headers headers2 = response.f22145f;
                    Headers headers3 = responseB.f22145f;
                    companion.getClass();
                    Headers.Builder builder2 = new Headers.Builder();
                    int size2 = headers2.size();
                    for (int i12 = 0; i12 < size2; i12++) {
                        String strH2 = headers2.h(i12);
                        String strL2 = headers2.l(i12);
                        if ((!"Warning".equalsIgnoreCase(strH2) || !w.A0(strL2, "1", false)) && ("Content-Length".equalsIgnoreCase(strH2) || "Content-Encoding".equalsIgnoreCase(strH2) || "Content-Type".equalsIgnoreCase(strH2) || !Companion.b(strH2) || headers3.g(strH2) == null)) {
                            builder2.c(strH2, strL2);
                        }
                    }
                    int size3 = headers3.size();
                    for (int i13 = 0; i13 < size3; i13++) {
                        String strH3 = headers3.h(i13);
                        if (!"Content-Length".equalsIgnoreCase(strH3) && !"Content-Encoding".equalsIgnoreCase(strH3) && !"Content-Type".equalsIgnoreCase(strH3) && Companion.b(strH3)) {
                            builder2.c(strH3, headers3.l(i13));
                        }
                    }
                    builderD3.c(builder2.e());
                    builderD3.f22163k = responseB.f22149n;
                    builderD3.l = responseB.f22150o;
                    Companion companion2 = f22188b;
                    Response responseA5 = Companion.a(companion2, response);
                    Response.Builder.b("cacheResponse", responseA5);
                    builderD3.f22161i = responseA5;
                    Response responseA6 = Companion.a(companion2, responseB);
                    Response.Builder.b("networkResponse", responseA6);
                    builderD3.f22160h = responseA6;
                    Response responseA7 = builderD3.a();
                    ResponseBody responseBody3 = responseB.f22146j;
                    o.e(responseBody3);
                    responseBody3.close();
                    Cache cache3 = this.f22189a;
                    o.e(cache3);
                    synchronized (cache3) {
                    }
                    this.f22189a.getClass();
                    Cache.c(response, responseA7);
                    obj.getClass();
                    o.h(call, "call");
                    return responseA7;
                }
                ResponseBody responseBody4 = response.f22146j;
                if (responseBody4 != null) {
                    Util.c(responseBody4);
                }
            }
            Response.Builder builderD4 = responseB.d();
            Companion companion3 = f22188b;
            Response responseA8 = Companion.a(companion3, response);
            Response.Builder.b("cacheResponse", responseA8);
            builderD4.f22161i = responseA8;
            Response responseA9 = Companion.a(companion3, responseB);
            Response.Builder.b("networkResponse", responseA9);
            builderD4.f22160h = responseA9;
            Response responseA10 = builderD4.a();
            if (this.f22189a != null) {
                if (HttpHeaders.a(responseA10)) {
                    CacheStrategy.f22194c.getClass();
                    if (CacheStrategy.Companion.a(request2, responseA10)) {
                        final CacheRequest cacheRequestB = this.f22189a.b(responseA10);
                        if (cacheRequestB != null) {
                            Cache.RealCacheRequest.AnonymousClass1 anonymousClass1A = cacheRequestB.a();
                            ResponseBody responseBody5 = responseA10.f22146j;
                            o.e(responseBody5);
                            final InterfaceC2742l interfaceC2742lC = responseBody5.c();
                            final I iB = AbstractC2732b.b(anonymousClass1A);
                            P p6 = new P() { // from class: okhttp3.internal.cache.CacheInterceptor$cacheWritingResponse$cacheWritingSource$1

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

                                @Override // java.io.Closeable, java.lang.AutoCloseable
                                public final void close() throws IOException {
                                    if (!this.f22190a) {
                                        TimeUnit timeUnit = TimeUnit.MILLISECONDS;
                                        if (!Util.g(this)) {
                                            this.f22190a = true;
                                            cacheRequestB.abort();
                                        }
                                    }
                                    interfaceC2742lC.close();
                                }

                                @Override // p5.P
                                public final long read(C2740j sink, long j12) throws Throwable {
                                    o.h(sink, "sink");
                                    try {
                                        long j13 = interfaceC2742lC.read(sink, j12);
                                        I i14 = iB;
                                        if (j13 != -1) {
                                            sink.c(i14.f23056b, sink.f23100b - j13, j13);
                                            i14.a();
                                            return j13;
                                        }
                                        if (!this.f22190a) {
                                            this.f22190a = true;
                                            i14.close();
                                        }
                                        return -1L;
                                    } catch (IOException e6) {
                                        if (this.f22190a) {
                                            throw e6;
                                        }
                                        this.f22190a = true;
                                        cacheRequestB.abort();
                                        throw e6;
                                    }
                                }

                                @Override // p5.P
                                public final S timeout() {
                                    return interfaceC2742lC.timeout();
                                }
                            };
                            String strB = Response.b("Content-Type", responseA10);
                            long jA = responseA10.f22146j.a();
                            Response.Builder builderD5 = responseA10.d();
                            builderD5.f22159g = new RealResponseBody(strB, jA, AbstractC2732b.c(p6));
                            responseA10 = builderD5.a();
                        }
                        if (response != null) {
                            obj.getClass();
                            o.h(call, "call");
                        }
                        return responseA10;
                    }
                }
                HttpMethod httpMethod = HttpMethod.f22383a;
                String str4 = request2.f22126b;
                httpMethod.getClass();
                if (HttpMethod.a(str4)) {
                    try {
                        Cache cache4 = this.f22189a;
                        cache4.getClass();
                        Cache.Companion companion4 = Cache.f21901b;
                        HttpUrl httpUrl = request2.f22125a;
                        companion4.getClass();
                        String key = Cache.Companion.a(httpUrl);
                        DiskLruCache diskLruCache = cache4.f21902a;
                        synchronized (diskLruCache) {
                            o.h(key, "key");
                            diskLruCache.f();
                            diskLruCache.a();
                            DiskLruCache.C(key);
                            DiskLruCache.Entry entry = (DiskLruCache.Entry) diskLruCache.f22224k.get(key);
                            if (entry == null) {
                                return responseA10;
                            }
                            diskLruCache.A(entry);
                            if (diskLruCache.f22222f <= 104857600) {
                                diskLruCache.f22229q = false;
                            }
                            return responseA10;
                        }
                    } catch (IOException unused) {
                    }
                }
            }
            return responseA10;
        } catch (Throwable th) {
            if (responseA != null && (responseBody = responseA.f22146j) != null) {
                Util.c(responseBody);
            }
            throw th;
        }
    }
}
