package xq;

import ar.c;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import kotlin.jvm.internal.k;
import kotlin.jvm.internal.t;
import kotlin.text.y;
import uq.b0;
import uq.d;
import uq.d0;
import uq.u;
import vq.m;

/* loaded from: classes3.dex */
public final class b {

    /* renamed from: c, reason: collision with root package name */
    public static final a f51019c = new a(null);

    /* renamed from: a, reason: collision with root package name */
    private final b0 f51020a;

    /* renamed from: b, reason: collision with root package name */
    private final d0 f51021b;

    /* loaded from: classes3.dex */
    public static final class a {
        private a() {
        }

        public /* synthetic */ a(k kVar) {
            this();
        }

        public final boolean a(d0 response, b0 request) {
            t.g(response, "response");
            t.g(request, "request");
            int f10 = response.f();
            if (f10 != 200 && f10 != 410 && f10 != 414 && f10 != 501 && f10 != 203 && f10 != 204) {
                if (f10 != 307) {
                    if (f10 != 308 && f10 != 404 && f10 != 405) {
                        switch (f10) {
                            case 300:
                            case 301:
                                break;
                            case 302:
                                break;
                            default:
                                return false;
                        }
                    }
                }
                if (d0.p(response, "Expires", null, 2, null) == null && response.c().e() == -1 && !response.c().d() && !response.c().c()) {
                    return false;
                }
            }
            return (response.c().j() || request.b().j()) ? false : true;
        }
    }

    /* renamed from: xq.b$b, reason: collision with other inner class name */
    /* loaded from: classes3.dex */
    public static final class C1024b {

        /* renamed from: a, reason: collision with root package name */
        private final long f51022a;

        /* renamed from: b, reason: collision with root package name */
        private final b0 f51023b;

        /* renamed from: c, reason: collision with root package name */
        private final d0 f51024c;

        /* renamed from: d, reason: collision with root package name */
        private Date f51025d;

        /* renamed from: e, reason: collision with root package name */
        private String f51026e;

        /* renamed from: f, reason: collision with root package name */
        private Date f51027f;

        /* renamed from: g, reason: collision with root package name */
        private String f51028g;

        /* renamed from: h, reason: collision with root package name */
        private Date f51029h;

        /* renamed from: i, reason: collision with root package name */
        private long f51030i;

        /* renamed from: j, reason: collision with root package name */
        private long f51031j;

        /* renamed from: k, reason: collision with root package name */
        private String f51032k;

        /* renamed from: l, reason: collision with root package name */
        private int f51033l;

        public C1024b(long j10, b0 request, d0 d0Var) {
            boolean z10;
            boolean z11;
            boolean z12;
            boolean z13;
            boolean z14;
            t.g(request, "request");
            this.f51022a = j10;
            this.f51023b = request;
            this.f51024c = d0Var;
            this.f51033l = -1;
            if (d0Var != null) {
                this.f51030i = d0Var.G();
                this.f51031j = d0Var.A();
                u r10 = d0Var.r();
                int size = r10.size();
                for (int i10 = 0; i10 < size; i10++) {
                    String i11 = r10.i(i10);
                    String C = r10.C(i10);
                    z10 = y.z(i11, "Date", true);
                    if (z10) {
                        this.f51025d = c.a(C);
                        this.f51026e = C;
                    } else {
                        z11 = y.z(i11, "Expires", true);
                        if (z11) {
                            this.f51029h = c.a(C);
                        } else {
                            z12 = y.z(i11, "Last-Modified", true);
                            if (z12) {
                                this.f51027f = c.a(C);
                                this.f51028g = C;
                            } else {
                                z13 = y.z(i11, "ETag", true);
                                if (z13) {
                                    this.f51032k = C;
                                } else {
                                    z14 = y.z(i11, "Age", true);
                                    if (z14) {
                                        this.f51033l = m.E(C, -1);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        private final long a() {
            Date date = this.f51025d;
            long max = date != null ? Math.max(0L, this.f51031j - date.getTime()) : 0L;
            int i10 = this.f51033l;
            if (i10 != -1) {
                max = Math.max(max, TimeUnit.SECONDS.toMillis(i10));
            }
            long j10 = this.f51031j;
            return max + (j10 - this.f51030i) + (this.f51022a - j10);
        }

        private final b c() {
            String str;
            if (this.f51024c == null) {
                return new b(this.f51023b, null);
            }
            if ((!this.f51023b.f() || this.f51024c.j() != null) && b.f51019c.a(this.f51024c, this.f51023b)) {
                d b10 = this.f51023b.b();
                if (b10.i() || e(this.f51023b)) {
                    return new b(this.f51023b, null);
                }
                d c10 = this.f51024c.c();
                long a10 = a();
                long d10 = d();
                if (b10.e() != -1) {
                    d10 = Math.min(d10, TimeUnit.SECONDS.toMillis(b10.e()));
                }
                long j10 = 0;
                long millis = b10.g() != -1 ? TimeUnit.SECONDS.toMillis(b10.g()) : 0L;
                if (!c10.h() && b10.f() != -1) {
                    j10 = TimeUnit.SECONDS.toMillis(b10.f());
                }
                if (!c10.i()) {
                    long j11 = millis + a10;
                    if (j11 < j10 + d10) {
                        d0.a w10 = this.f51024c.w();
                        if (j11 >= d10) {
                            w10.a("Warning", "110 HttpURLConnection \"Response is stale\"");
                        }
                        if (a10 > 86400000 && f()) {
                            w10.a("Warning", "113 HttpURLConnection \"Heuristic expiration\"");
                        }
                        return new b(null, w10.c());
                    }
                }
                String str2 = this.f51032k;
                if (str2 != null) {
                    str = "If-None-Match";
                } else {
                    if (this.f51027f != null) {
                        str2 = this.f51028g;
                    } else {
                        if (this.f51025d == null) {
                            return new b(this.f51023b, null);
                        }
                        str2 = this.f51026e;
                    }
                    str = "If-Modified-Since";
                }
                u.a r10 = this.f51023b.e().r();
                t.d(str2);
                r10.d(str, str2);
                return new b(this.f51023b.h().n(r10.e()).b(), this.f51024c);
            }
            return new b(this.f51023b, null);
        }

        private final long d() {
            d0 d0Var = this.f51024c;
            t.d(d0Var);
            if (d0Var.c().e() != -1) {
                return TimeUnit.SECONDS.toMillis(r0.e());
            }
            Date date = this.f51029h;
            if (date != null) {
                Date date2 = this.f51025d;
                long time = date.getTime() - (date2 != null ? date2.getTime() : this.f51031j);
                if (time > 0) {
                    return time;
                }
                return 0L;
            }
            if (this.f51027f == null || this.f51024c.C().k().p() != null) {
                return 0L;
            }
            Date date3 = this.f51025d;
            long time2 = date3 != null ? date3.getTime() : this.f51030i;
            Date date4 = this.f51027f;
            t.d(date4);
            long time3 = time2 - date4.getTime();
            if (time3 > 0) {
                return time3 / 10;
            }
            return 0L;
        }

        private final boolean e(b0 b0Var) {
            return (b0Var.d("If-Modified-Since") == null && b0Var.d("If-None-Match") == null) ? false : true;
        }

        private final boolean f() {
            d0 d0Var = this.f51024c;
            t.d(d0Var);
            return d0Var.c().e() == -1 && this.f51029h == null;
        }

        public final b b() {
            b c10 = c();
            return (c10.b() == null || !this.f51023b.b().l()) ? c10 : new b(null, null);
        }
    }

    public b(b0 b0Var, d0 d0Var) {
        this.f51020a = b0Var;
        this.f51021b = d0Var;
    }

    public final d0 a() {
        return this.f51021b;
    }

    public final b0 b() {
        return this.f51020a;
    }
}
