package coil.network;

import S4.w;
import coil.util.Time;
import coil.util.Utils;
import java.util.ArrayList;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import kotlin.jvm.internal.h;
import kotlin.jvm.internal.o;
import okhttp3.CacheControl;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.internal.http.DatesKt;

/* JADX INFO: loaded from: classes.dex */
public final class CacheStrategy {
    public static final Companion Companion = new Companion(null);
    private final CacheResponse cacheResponse;
    private final Request networkRequest;

    public static final class Companion {
        public /* synthetic */ Companion(h hVar) {
            this();
        }

        private final boolean isContentSpecificHeader(String str) {
            return "Content-Length".equalsIgnoreCase(str) || "Content-Encoding".equalsIgnoreCase(str) || "Content-Type".equalsIgnoreCase(str);
        }

        private final boolean isEndToEnd(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;
        }

        public final Headers combineHeaders(Headers headers, Headers headers2) {
            Headers.Builder builder = new Headers.Builder();
            int size = headers.size();
            for (int i6 = 0; i6 < size; i6++) {
                String strH = headers.h(i6);
                String strL = headers.l(i6);
                if ((!"Warning".equalsIgnoreCase(strH) || !w.A0(strL, "1", false)) && (isContentSpecificHeader(strH) || !isEndToEnd(strH) || headers2.g(strH) == null)) {
                    builder.d(strH, strL);
                }
            }
            int size2 = headers2.size();
            for (int i7 = 0; i7 < size2; i7++) {
                String strH2 = headers2.h(i7);
                if (!isContentSpecificHeader(strH2) && isEndToEnd(strH2)) {
                    builder.d(strH2, headers2.l(i7));
                }
            }
            return builder.e();
        }

        public final boolean isCacheable(Request request, Response response) {
            return (request.a().f21931b || response.a().f21931b || o.c(response.f22145f.g("Vary"), "*")) ? false : true;
        }

        private Companion() {
        }

        public final boolean isCacheable(Request request, CacheResponse cacheResponse) {
            return (request.a().f21931b || cacheResponse.getCacheControl().f21931b || o.c(cacheResponse.getResponseHeaders().g("Vary"), "*")) ? false : true;
        }
    }

    public static final class Factory {
        private int ageSeconds;
        private final CacheResponse cacheResponse;
        private String etag;
        private Date expires;
        private Date lastModified;
        private String lastModifiedString;
        private long receivedResponseMillis;
        private final Request request;
        private long sentRequestMillis;
        private Date servedDate;
        private String servedDateString;

        public Factory(Request request, CacheResponse cacheResponse) {
            this.request = request;
            this.cacheResponse = cacheResponse;
            this.ageSeconds = -1;
            if (cacheResponse != null) {
                this.sentRequestMillis = cacheResponse.getSentRequestAtMillis();
                this.receivedResponseMillis = cacheResponse.getReceivedResponseAtMillis();
                Headers responseHeaders = cacheResponse.getResponseHeaders();
                int size = responseHeaders.size();
                for (int i6 = 0; i6 < size; i6++) {
                    String strH = responseHeaders.h(i6);
                    if (w.t0(strH, "Date", true)) {
                        String strG = responseHeaders.g("Date");
                        this.servedDate = strG != null ? DatesKt.a(strG) : null;
                        this.servedDateString = responseHeaders.l(i6);
                    } else if (w.t0(strH, "Expires", true)) {
                        String strG2 = responseHeaders.g("Expires");
                        this.expires = strG2 != null ? DatesKt.a(strG2) : null;
                    } else if (w.t0(strH, "Last-Modified", true)) {
                        String strG3 = responseHeaders.g("Last-Modified");
                        this.lastModified = strG3 != null ? DatesKt.a(strG3) : null;
                        this.lastModifiedString = responseHeaders.l(i6);
                    } else if (w.t0(strH, "ETag", true)) {
                        this.etag = responseHeaders.l(i6);
                    } else if (w.t0(strH, "Age", true)) {
                        this.ageSeconds = Utils.toNonNegativeInt(responseHeaders.l(i6), -1);
                    }
                }
            }
        }

        private final long cacheResponseAge() {
            Date date = this.servedDate;
            long jMax = date != null ? Math.max(0L, this.receivedResponseMillis - date.getTime()) : 0L;
            int i6 = this.ageSeconds;
            if (i6 != -1) {
                jMax = Math.max(jMax, TimeUnit.SECONDS.toMillis(i6));
            }
            return jMax + (this.receivedResponseMillis - this.sentRequestMillis) + (Time.INSTANCE.currentMillis() - this.receivedResponseMillis);
        }

        private final long computeFreshnessLifetime() {
            String string;
            CacheResponse cacheResponse = this.cacheResponse;
            o.e(cacheResponse);
            int i6 = cacheResponse.getCacheControl().f21932c;
            if (i6 != -1) {
                return TimeUnit.SECONDS.toMillis(i6);
            }
            Date date = this.expires;
            if (date != null) {
                Date date2 = this.servedDate;
                long time = date.getTime() - (date2 != null ? date2.getTime() : this.receivedResponseMillis);
                if (time > 0) {
                    return time;
                }
            } else if (this.lastModified != null) {
                ArrayList arrayList = this.request.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) {
                    Date date3 = this.servedDate;
                    long time2 = date3 != null ? date3.getTime() : this.sentRequestMillis;
                    Date date4 = this.lastModified;
                    o.e(date4);
                    long time3 = time2 - date4.getTime();
                    if (time3 > 0) {
                        return time3 / ((long) 10);
                    }
                }
            }
            return 0L;
        }

        private final boolean hasConditions(Request request) {
            return (request.f22127c.g("If-Modified-Since") == null && request.f22127c.g("If-None-Match") == null) ? false : true;
        }

        /* JADX WARN: Multi-variable type inference failed */
        public final CacheStrategy compute() {
            String str;
            int i6;
            CacheResponse cacheResponse = this.cacheResponse;
            CacheResponse cacheResponse2 = null;
            Object[] objArr = 0;
            Object[] objArr2 = 0;
            Object[] objArr3 = 0;
            Object[] objArr4 = 0;
            Object[] objArr5 = 0;
            Object[] objArr6 = 0;
            Object[] objArr7 = 0;
            Object[] objArr8 = 0;
            Object[] objArr9 = 0;
            Object[] objArr10 = 0;
            Object[] objArr11 = 0;
            Object[] objArr12 = 0;
            if (cacheResponse == null) {
                return new CacheStrategy(this.request, cacheResponse2, objArr12 == true ? 1 : 0);
            }
            if (this.request.f22125a.f22033j && !cacheResponse.isTls()) {
                return new CacheStrategy(this.request, objArr11 == true ? 1 : 0, objArr10 == true ? 1 : 0);
            }
            CacheControl cacheControl = this.cacheResponse.getCacheControl();
            if (!CacheStrategy.Companion.isCacheable(this.request, this.cacheResponse)) {
                return new CacheStrategy(this.request, objArr9 == true ? 1 : 0, objArr8 == true ? 1 : 0);
            }
            CacheControl cacheControlA = this.request.a();
            if (cacheControlA.f21930a || hasConditions(this.request)) {
                return new CacheStrategy(this.request, objArr2 == true ? 1 : 0, objArr == true ? 1 : 0);
            }
            long jCacheResponseAge = cacheResponseAge();
            long jComputeFreshnessLifetime = computeFreshnessLifetime();
            int i7 = cacheControlA.f21932c;
            if (i7 != -1) {
                jComputeFreshnessLifetime = Math.min(jComputeFreshnessLifetime, TimeUnit.SECONDS.toMillis(i7));
            }
            long millis = 0;
            int i8 = cacheControlA.f21938i;
            long millis2 = i8 != -1 ? TimeUnit.SECONDS.toMillis(i8) : 0L;
            if (!cacheControl.f21936g && (i6 = cacheControlA.f21937h) != -1) {
                millis = TimeUnit.SECONDS.toMillis(i6);
            }
            if (!cacheControl.f21930a && jCacheResponseAge + millis2 < jComputeFreshnessLifetime + millis) {
                return new CacheStrategy(objArr7 == true ? 1 : 0, this.cacheResponse, objArr6 == true ? 1 : 0);
            }
            String str2 = this.etag;
            if (str2 != null) {
                str = "If-None-Match";
            } else {
                str = "If-Modified-Since";
                if (this.lastModified != null) {
                    str2 = this.lastModifiedString;
                    o.e(str2);
                } else {
                    if (this.servedDate == null) {
                        return new CacheStrategy(this.request, objArr4 == true ? 1 : 0, objArr3 == true ? 1 : 0);
                    }
                    str2 = this.servedDateString;
                    o.e(str2);
                }
            }
            Request.Builder builderB = this.request.b();
            builderB.a(str, str2);
            return new CacheStrategy(builderB.b(), this.cacheResponse, objArr5 == true ? 1 : 0);
        }
    }

    public /* synthetic */ CacheStrategy(Request request, CacheResponse cacheResponse, h hVar) {
        this(request, cacheResponse);
    }

    public final CacheResponse getCacheResponse() {
        return this.cacheResponse;
    }

    public final Request getNetworkRequest() {
        return this.networkRequest;
    }

    private CacheStrategy(Request request, CacheResponse cacheResponse) {
        this.networkRequest = request;
        this.cacheResponse = cacheResponse;
    }
}
