package p;

import a0.r;
import android.os.Handler;
import android.os.SystemClock;
import b0.AbstractC0399d;
import b0.D;
import java.io.IOException;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import n.C0633A;
import n.InterfaceC0634B;
import n.x;
import n.y;
import u.InterfaceC0722a;
import v.C0727c;

/* JADX INFO: loaded from: classes.dex */
public class f implements InterfaceC0634B, InterfaceC0634B.a, r.a {

    /* JADX INFO: renamed from: A, reason: collision with root package name */
    private j f6100A;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    protected final C0727c f6101a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private final int f6102b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private final n.n f6103c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private final p.g f6104d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private final p.e f6105e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private final LinkedList f6106f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private final List f6107g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    private final int f6108h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    private final Handler f6109i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    private final g f6110j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    private final int f6111k;

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

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    private long f6113m;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    private long f6114n;

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    private long f6115o;

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    private long f6116p;

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    private boolean f6117q;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    private r f6118r;

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    private boolean f6119s;

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    private IOException f6120t;

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    private int f6121u;

    /* JADX INFO: renamed from: v, reason: collision with root package name */
    private int f6122v;

    /* JADX INFO: renamed from: w, reason: collision with root package name */
    private long f6123w;

    /* JADX INFO: renamed from: x, reason: collision with root package name */
    private long f6124x;

    /* JADX INFO: renamed from: y, reason: collision with root package name */
    private InterfaceC0722a f6125y;

    /* JADX INFO: renamed from: z, reason: collision with root package name */
    private x f6126z;

    class a implements Runnable {

        /* JADX INFO: renamed from: H, reason: collision with root package name */
        final /* synthetic */ long f6127H;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        final /* synthetic */ long f6129b;

        /* JADX INFO: renamed from: g, reason: collision with root package name */
        final /* synthetic */ int f6130g;

        /* JADX INFO: renamed from: r, reason: collision with root package name */
        final /* synthetic */ int f6131r;

        /* JADX INFO: renamed from: x, reason: collision with root package name */
        final /* synthetic */ j f6132x;

        /* JADX INFO: renamed from: y, reason: collision with root package name */
        final /* synthetic */ long f6133y;

        a(long j2, int i2, int i3, j jVar, long j3, long j4) {
            this.f6129b = j2;
            this.f6130g = i2;
            this.f6131r = i3;
            this.f6132x = jVar;
            this.f6133y = j3;
            this.f6127H = j4;
        }

        @Override // java.lang.Runnable
        public void run() {
            f.this.f6110j.onLoadStarted(f.this.f6102b, this.f6129b, this.f6130g, this.f6131r, this.f6132x, f.this.L(this.f6133y), f.this.L(this.f6127H));
        }
    }

    class b implements Runnable {

        /* JADX INFO: renamed from: H, reason: collision with root package name */
        final /* synthetic */ long f6134H;

        /* JADX INFO: renamed from: I, reason: collision with root package name */
        final /* synthetic */ long f6135I;

        /* JADX INFO: renamed from: L, reason: collision with root package name */
        final /* synthetic */ long f6136L;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        final /* synthetic */ long f6138b;

        /* JADX INFO: renamed from: g, reason: collision with root package name */
        final /* synthetic */ int f6139g;

        /* JADX INFO: renamed from: r, reason: collision with root package name */
        final /* synthetic */ int f6140r;

        /* JADX INFO: renamed from: x, reason: collision with root package name */
        final /* synthetic */ j f6141x;

        /* JADX INFO: renamed from: y, reason: collision with root package name */
        final /* synthetic */ long f6142y;

        b(long j2, int i2, int i3, j jVar, long j3, long j4, long j5, long j6) {
            this.f6138b = j2;
            this.f6139g = i2;
            this.f6140r = i3;
            this.f6141x = jVar;
            this.f6142y = j3;
            this.f6134H = j4;
            this.f6135I = j5;
            this.f6136L = j6;
        }

        @Override // java.lang.Runnable
        public void run() {
            f.this.f6110j.onLoadCompleted(f.this.f6102b, this.f6138b, this.f6139g, this.f6140r, this.f6141x, f.this.L(this.f6142y), f.this.L(this.f6134H), this.f6135I, this.f6136L);
        }
    }

    class c implements Runnable {

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        final /* synthetic */ long f6143b;

        c(long j2) {
            this.f6143b = j2;
        }

        @Override // java.lang.Runnable
        public void run() {
            f.this.f6110j.h(f.this.f6102b, this.f6143b);
        }
    }

    class d implements Runnable {

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        final /* synthetic */ IOException f6145b;

        d(IOException iOException) {
            this.f6145b = iOException;
        }

        @Override // java.lang.Runnable
        public void run() {
            f.this.f6110j.a(f.this.f6102b, this.f6145b);
        }
    }

    class e implements Runnable {

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        final /* synthetic */ long f6147b;

        /* JADX INFO: renamed from: g, reason: collision with root package name */
        final /* synthetic */ long f6148g;

        e(long j2, long j3) {
            this.f6147b = j2;
            this.f6148g = j3;
        }

        @Override // java.lang.Runnable
        public void run() {
            f.this.f6110j.s(f.this.f6102b, f.this.L(this.f6147b), f.this.L(this.f6148g));
        }
    }

    /* JADX INFO: renamed from: p.f$f, reason: collision with other inner class name */
    class RunnableC0088f implements Runnable {

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        final /* synthetic */ j f6150b;

        /* JADX INFO: renamed from: g, reason: collision with root package name */
        final /* synthetic */ int f6151g;

        /* JADX INFO: renamed from: r, reason: collision with root package name */
        final /* synthetic */ long f6152r;

        RunnableC0088f(j jVar, int i2, long j2) {
            this.f6150b = jVar;
            this.f6151g = i2;
            this.f6152r = j2;
        }

        @Override // java.lang.Runnable
        public void run() {
            f.this.f6110j.c(f.this.f6102b, this.f6150b, this.f6151g, f.this.L(this.f6152r));
        }
    }

    public interface g extends InterfaceC0689a {
    }

    public f(p.g gVar, n.n nVar, int i2, Handler handler, g gVar2, int i3) {
        this(gVar, nVar, i2, handler, gVar2, i3, 3);
    }

    private void A() {
        p.c cVar = this.f6105e.f6098b;
        if (cVar == null) {
            return;
        }
        this.f6124x = SystemClock.elapsedRealtime();
        if (y(cVar)) {
            p.b bVar = (p.b) cVar;
            bVar.o(this.f6101a);
            this.f6106f.add(bVar);
            if (z()) {
                this.f6115o = Long.MIN_VALUE;
            }
            F(bVar.f6089d.f1944e, bVar.f6086a, bVar.f6087b, bVar.f6088c, bVar.f6193g, bVar.f6194h);
        } else {
            ((m) cVar).o(this.f6101a);
            F(cVar.f6089d.f1944e, cVar.f6086a, cVar.f6087b, cVar.f6088c, -1L, -1L);
        }
        this.f6118r.g(cVar, this);
    }

    private void B(j jVar, int i2, long j2) {
        Handler handler = this.f6109i;
        if (handler == null || this.f6110j == null) {
            return;
        }
        handler.post(new RunnableC0088f(jVar, i2, j2));
    }

    private void C(long j2) {
        Handler handler = this.f6109i;
        if (handler == null || this.f6110j == null) {
            return;
        }
        handler.post(new c(j2));
    }

    private void D(long j2, int i2, int i3, j jVar, long j3, long j4, long j5, long j6) {
        Handler handler = this.f6109i;
        if (handler == null || this.f6110j == null) {
            return;
        }
        handler.post(new b(j2, i2, i3, jVar, j3, j4, j5, j6));
    }

    private void E(IOException iOException) {
        Handler handler = this.f6109i;
        if (handler == null || this.f6110j == null) {
            return;
        }
        handler.post(new d(iOException));
    }

    private void F(long j2, int i2, int i3, j jVar, long j3, long j4) {
        Handler handler = this.f6109i;
        if (handler == null || this.f6110j == null) {
            return;
        }
        handler.post(new a(j2, i2, i3, jVar, j3, j4));
    }

    private void G(long j2, long j3) {
        Handler handler = this.f6109i;
        if (handler == null || this.f6110j == null) {
            return;
        }
        handler.post(new e(j2, j3));
    }

    private void I(long j2) {
        this.f6115o = j2;
        this.f6119s = false;
        if (this.f6118r.d()) {
            this.f6118r.c();
            return;
        }
        this.f6101a.h();
        this.f6106f.clear();
        i();
        K();
    }

    private void J() {
        this.f6120t = null;
        p.c cVar = this.f6105e.f6098b;
        if (!y(cVar)) {
            u();
            q(this.f6105e.f6097a);
            if (this.f6105e.f6098b == cVar) {
                this.f6118r.g(cVar, this);
                return;
            } else {
                C(cVar.f());
                A();
                return;
            }
        }
        if (cVar == this.f6106f.getFirst()) {
            this.f6118r.g(cVar, this);
            return;
        }
        p.b bVar = (p.b) this.f6106f.removeLast();
        AbstractC0399d.e(cVar == bVar);
        u();
        this.f6106f.add(bVar);
        if (this.f6105e.f6098b == cVar) {
            this.f6118r.g(cVar, this);
            return;
        }
        C(cVar.f());
        q(this.f6105e.f6097a);
        m();
        A();
    }

    private void K() {
        long j2;
        long jElapsedRealtime = SystemClock.elapsedRealtime();
        long jW = w();
        boolean z2 = this.f6120t != null;
        boolean z3 = this.f6118r.d() || z2;
        if (z3 || ((this.f6105e.f6098b != null || jW == -1) && jElapsedRealtime - this.f6116p <= 2000)) {
            j2 = jW;
        } else {
            this.f6116p = jElapsedRealtime;
            u();
            boolean zQ = q(this.f6105e.f6097a);
            if (this.f6105e.f6098b == null) {
                j2 = -1;
            } else {
                if (zQ) {
                    jW = w();
                }
                j2 = jW;
            }
        }
        boolean zC = this.f6103c.c(this, this.f6113m, j2, z3);
        if (z2) {
            if (jElapsedRealtime - this.f6123w >= x(this.f6122v)) {
                J();
            }
        } else {
            if (this.f6118r.d() || !zC) {
                return;
            }
            A();
        }
    }

    private void i() {
        this.f6105e.f6098b = null;
        m();
    }

    private void m() {
        this.f6120t = null;
        this.f6122v = 0;
    }

    private boolean q(int i2) {
        if (this.f6106f.size() <= i2) {
            return false;
        }
        long j2 = ((p.b) this.f6106f.getLast()).f6194h;
        long j3 = 0;
        p.b bVar = null;
        while (this.f6106f.size() > i2) {
            bVar = (p.b) this.f6106f.removeLast();
            j3 = bVar.f6193g;
            this.f6119s = false;
        }
        this.f6101a.k(bVar.k());
        G(j3, j2);
        return true;
    }

    private void u() {
        p.e eVar = this.f6105e;
        eVar.f6099c = false;
        eVar.f6097a = this.f6107g.size();
        p.g gVar = this.f6104d;
        List list = this.f6107g;
        long j2 = this.f6115o;
        if (j2 == Long.MIN_VALUE) {
            j2 = this.f6113m;
        }
        gVar.j(list, j2, this.f6105e);
        this.f6119s = this.f6105e.f6099c;
    }

    private long w() {
        if (z()) {
            return this.f6115o;
        }
        if (this.f6119s) {
            return -1L;
        }
        return ((p.b) this.f6106f.getLast()).f6194h;
    }

    private long x(long j2) {
        return Math.min((j2 - 1) * 1000, 5000L);
    }

    private boolean y(p.c cVar) {
        return cVar instanceof p.b;
    }

    private boolean z() {
        return this.f6115o != Long.MIN_VALUE;
    }

    protected void H(n nVar, C0633A c0633a) {
    }

    protected final long L(long j2) {
        return j2 / 1000;
    }

    @Override // n.InterfaceC0634B.a
    public x a(int i2) {
        int i3 = this.f6112l;
        AbstractC0399d.e(i3 == 2 || i3 == 3);
        return this.f6104d.a(i2);
    }

    @Override // n.InterfaceC0634B.a
    public int c() {
        int i2 = this.f6112l;
        AbstractC0399d.e(i2 == 2 || i2 == 3);
        return this.f6104d.c();
    }

    @Override // n.InterfaceC0634B.a
    public void d(int i2, long j2) {
        AbstractC0399d.e(this.f6112l == 2);
        int i3 = this.f6121u;
        this.f6121u = i3 + 1;
        AbstractC0399d.e(i3 == 0);
        this.f6112l = 3;
        this.f6104d.d(i2);
        this.f6103c.a(this, this.f6108h);
        this.f6100A = null;
        this.f6126z = null;
        this.f6125y = null;
        this.f6113m = j2;
        this.f6114n = j2;
        this.f6117q = false;
        I(j2);
    }

    @Override // n.InterfaceC0634B.a
    public void e() throws IOException {
        IOException iOException = this.f6120t;
        if (iOException != null && this.f6122v > this.f6111k) {
            throw iOException;
        }
        if (this.f6105e.f6098b == null) {
            this.f6104d.e();
        }
    }

    @Override // n.InterfaceC0634B.a
    public boolean f(int i2, long j2) {
        AbstractC0399d.e(this.f6112l == 3);
        this.f6113m = j2;
        this.f6104d.g(j2);
        K();
        return this.f6119s || !this.f6101a.s();
    }

    @Override // n.InterfaceC0634B.a
    public int h() {
        return this.f6104d.h();
    }

    @Override // n.InterfaceC0634B.a
    public long j(int i2) {
        if (!this.f6117q) {
            return Long.MIN_VALUE;
        }
        this.f6117q = false;
        return this.f6114n;
    }

    @Override // n.InterfaceC0634B.a
    public void k(int i2) {
        AbstractC0399d.e(this.f6112l == 3);
        int i3 = this.f6121u - 1;
        this.f6121u = i3;
        AbstractC0399d.e(i3 == 0);
        this.f6112l = 2;
        try {
            this.f6104d.l(this.f6106f);
            this.f6103c.d(this);
            if (this.f6118r.d()) {
                this.f6118r.c();
                return;
            }
            this.f6101a.h();
            this.f6106f.clear();
            i();
            this.f6103c.b();
        } catch (Throwable th) {
            this.f6103c.d(this);
            if (this.f6118r.d()) {
                this.f6118r.c();
            } else {
                this.f6101a.h();
                this.f6106f.clear();
                i();
                this.f6103c.b();
            }
            throw th;
        }
    }

    @Override // a0.r.a
    public void l(r.c cVar) {
        long jElapsedRealtime = SystemClock.elapsedRealtime();
        long j2 = jElapsedRealtime - this.f6124x;
        p.c cVar2 = this.f6105e.f6098b;
        this.f6104d.k(cVar2);
        if (y(cVar2)) {
            p.b bVar = (p.b) cVar2;
            D(cVar2.f(), bVar.f6086a, bVar.f6087b, bVar.f6088c, bVar.f6193g, bVar.f6194h, jElapsedRealtime, j2);
        } else {
            D(cVar2.f(), cVar2.f6086a, cVar2.f6087b, cVar2.f6088c, -1L, -1L, jElapsedRealtime, j2);
        }
        i();
        K();
    }

    @Override // a0.r.a
    public void n(r.c cVar, IOException iOException) {
        this.f6120t = iOException;
        this.f6122v++;
        this.f6123w = SystemClock.elapsedRealtime();
        E(iOException);
        this.f6104d.f(this.f6105e.f6098b, iOException);
        K();
    }

    @Override // n.InterfaceC0634B.a
    public void o(long j2) {
        AbstractC0399d.e(this.f6112l == 3);
        long j3 = z() ? this.f6115o : this.f6113m;
        this.f6113m = j2;
        this.f6114n = j2;
        if (j3 == j2) {
            return;
        }
        if (z() || !this.f6101a.u(j2)) {
            I(j2);
        } else {
            boolean zS = this.f6101a.s();
            while (!zS && this.f6106f.size() > 1 && ((p.b) this.f6106f.get(1)).k() <= this.f6101a.o()) {
                this.f6106f.removeFirst();
            }
        }
        this.f6117q = true;
    }

    @Override // n.InterfaceC0634B
    public InterfaceC0634B.a p() {
        AbstractC0399d.e(this.f6112l == 0);
        this.f6112l = 1;
        return this;
    }

    @Override // n.InterfaceC0634B.a
    public int r(int i2, long j2, y yVar, C0633A c0633a) {
        AbstractC0399d.e(this.f6112l == 3);
        this.f6113m = j2;
        if (this.f6117q || z()) {
            return -2;
        }
        boolean zS = this.f6101a.s();
        p.b bVar = (p.b) this.f6106f.getFirst();
        while (!zS && this.f6106f.size() > 1 && ((p.b) this.f6106f.get(1)).k() <= this.f6101a.o()) {
            this.f6106f.removeFirst();
            bVar = (p.b) this.f6106f.getFirst();
        }
        j jVar = bVar.f6088c;
        if (!jVar.equals(this.f6100A)) {
            B(jVar, bVar.f6087b, bVar.f6193g);
        }
        this.f6100A = jVar;
        if (!zS || bVar.f6083j) {
            x xVarL = bVar.l();
            InterfaceC0722a interfaceC0722aJ = bVar.j();
            if (!xVarL.equals(this.f6126z) || !D.a(this.f6125y, interfaceC0722aJ)) {
                float f2 = this.f6100A.f6170f;
                if (f2 > 0.0f) {
                    xVarL.f5851o0 = Float.valueOf(f2).intValue();
                }
                yVar.f5857a = xVarL;
                yVar.f5858b = interfaceC0722aJ;
                this.f6126z = xVarL;
                this.f6125y = interfaceC0722aJ;
                return -4;
            }
            this.f6126z = xVarL;
            this.f6125y = interfaceC0722aJ;
        }
        if (zS) {
            return this.f6119s ? -1 : -2;
        }
        if (c0633a == null || !this.f6101a.p(c0633a)) {
            return -2;
        }
        c0633a.f5411d |= c0633a.f5412e < this.f6114n ? 134217728 : 0;
        H(bVar, c0633a);
        return -3;
    }

    @Override // n.InterfaceC0634B.a
    public void release() {
        AbstractC0399d.e(this.f6112l != 3);
        r rVar = this.f6118r;
        if (rVar != null) {
            rVar.e();
            this.f6118r = null;
        }
        this.f6112l = 0;
    }

    @Override // n.InterfaceC0634B.a
    public boolean s(long j2) {
        int i2 = this.f6112l;
        AbstractC0399d.e(i2 == 1 || i2 == 2);
        if (this.f6112l == 2) {
            return true;
        }
        if (!this.f6104d.prepare()) {
            return false;
        }
        if (this.f6104d.c() > 0) {
            this.f6118r = new r("Loader:" + this.f6104d.a(0).f5842g);
        }
        this.f6112l = 2;
        return true;
    }

    @Override // n.InterfaceC0634B.a
    public long t() {
        AbstractC0399d.e(this.f6112l == 3);
        if (z()) {
            return this.f6115o;
        }
        if (this.f6119s) {
            return -3L;
        }
        long jN = this.f6101a.n();
        return jN == Long.MIN_VALUE ? this.f6113m : jN;
    }

    @Override // a0.r.a
    public void v(r.c cVar) {
        C(this.f6105e.f6098b.f());
        i();
        if (this.f6112l == 3) {
            I(this.f6115o);
            return;
        }
        this.f6101a.h();
        this.f6106f.clear();
        i();
        this.f6103c.b();
    }

    public f(p.g gVar, n.n nVar, int i2, Handler handler, g gVar2, int i3, int i4) {
        this.f6104d = gVar;
        this.f6103c = nVar;
        this.f6108h = i2;
        this.f6109i = handler;
        this.f6110j = gVar2;
        this.f6102b = i3;
        this.f6111k = i4;
        this.f6105e = new p.e();
        LinkedList linkedList = new LinkedList();
        this.f6106f = linkedList;
        this.f6107g = Collections.unmodifiableList(linkedList);
        this.f6101a = new C0727c(nVar.e());
        this.f6112l = 0;
        this.f6115o = Long.MIN_VALUE;
    }
}
