package x0;

import c1.C0406c;
import c1.InterfaceC0404a;
import java.util.LinkedList;
import r0.InterfaceC0703b;
import u0.C0723a;

/* JADX INFO: loaded from: classes.dex */
public class i extends m {

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

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

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

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

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private double f6798g;

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

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

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

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    private d f6802k;

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

    class a implements InterfaceC0404a {
        a() {
        }

        @Override // c1.InterfaceC0404a
        public void run() {
            i.this.p();
        }
    }

    private class b extends d {
        public b(InterfaceC0703b interfaceC0703b, int i2, int i3) {
            super(i.this, interfaceC0703b, i2, i3);
        }

        @Override // c1.InterfaceC0404a
        public void run() {
            synchronized (i.this.f6794c) {
                try {
                    if (i.this.f6794c.size() > 0 && ((e) i.this.f6794c.getLast()).g()) {
                        i.this.f6794c.removeLast();
                    }
                    if (a() > 0) {
                        if (!f()) {
                            d dVarT = i.this.t();
                            if (dVarT != null) {
                                i.this.D(dVarT, dVarT.d() + dVarT.b(), this);
                            }
                        } else if (i.this.f6795d != null) {
                            i iVar = i.this;
                            iVar.D(iVar.f6795d, i.this.f6796e, this);
                        }
                    }
                    if (f()) {
                        i.this.p();
                    }
                    i.this.f6794c.add(this);
                } catch (Throwable th) {
                    throw th;
                }
            }
        }
    }

    public i(String str) {
        super(str);
        this.f6794c = new LinkedList();
        this.f6797f = 1;
    }

    private void l() {
        int i2 = this.f6796e + 1;
        this.f6796e = i2;
        this.f6799h++;
        if (i2 >= this.f6795d.d() + this.f6795d.b()) {
            d dVar = this.f6795d;
            int i3 = dVar.f6786y;
            if (i3 > 0) {
                dVar.f6786y = i3 - 1;
                x();
            } else {
                if (this.f6794c.size() == 0 && this.f6795d.f6786y < 0) {
                    x();
                    return;
                }
                if (this.f6795d.e()) {
                    this.f6800i = true;
                }
                this.f6802k = this.f6795d;
                C(null);
            }
        }
    }

    private void n() {
        if (this.f6795d == null) {
            synchronized (this.f6794c) {
                C((d) this.f6794c.remove());
                this.f6796e = this.f6795d.d();
                d dVar = this.f6795d;
                dVar.f6786y = dVar.c();
                this.f6795d.h();
            }
        }
    }

    private void x() {
        if (this.f6795d.a() > 0) {
            d dVar = this.f6795d;
            D(dVar, this.f6796e, dVar);
        }
        this.f6796e = this.f6795d.d();
        this.f6803l = this.f6795d;
    }

    public void A(InterfaceC0703b interfaceC0703b, int i2, int i3, C0406c c0406c) {
        z(interfaceC0703b, i2, i3, -1, c0406c);
    }

    public double B(double d2) {
        m(d2);
        double dA = this.f6795d.f6777Z.a(this.f6796e);
        r();
        return dA;
    }

    public void C(d dVar) {
        this.f6795d = dVar;
    }

    protected void D(d dVar, int i2, d dVar2) {
        int iA = dVar2.a();
        InterfaceC0703b interfaceC0703bI = dVar.i();
        InterfaceC0703b interfaceC0703bI2 = dVar2.i();
        int iB = interfaceC0703bI.b() - i2;
        int i3 = iA > iB ? iB : iA;
        if (i3 > 0) {
            dVar2.f6776Q.e(interfaceC0703bI, i2, i3, interfaceC0703bI2, dVar2.d(), interfaceC0703bI2.b() - dVar2.d());
            dVar2.f6777Z = dVar2.f6776Q;
            dVar2.f6783g = 0;
        }
    }

    public boolean E() {
        boolean z2 = this.f6800i;
        this.f6800i = false;
        return z2;
    }

    public void m(double d2) {
        n();
        this.f6798g = this.f6795d.f6777Z.c(this.f6796e, d2);
    }

    public void o(C0406c c0406c) {
        f(c0406c, new a());
    }

    protected void p() {
        synchronized (this.f6794c) {
            this.f6794c.clear();
            C(null);
            this.f6801j = false;
            this.f6800i = false;
        }
    }

    public d q(InterfaceC0703b interfaceC0703b, int i2, int i3) {
        if (interfaceC0703b.d() == this.f6797f) {
            return new b(interfaceC0703b, i2, i3);
        }
        throw new C0723a("Tried to queue " + interfaceC0703b.d() + " channel data to a " + this.f6797f + " channel port.");
    }

    public void r() {
        l();
        this.f6801j = true;
    }

    public void s() {
        d dVar = this.f6803l;
        if (dVar != null) {
            dVar.h();
            this.f6803l = null;
        }
        d dVar2 = this.f6802k;
        if (dVar2 != null) {
            dVar2.h();
            this.f6802k = null;
        }
    }

    public d t() {
        if (this.f6794c.size() > 0) {
            return (d) this.f6794c.getLast();
        }
        d dVar = this.f6795d;
        if (dVar != null) {
            return dVar;
        }
        return null;
    }

    public double u() {
        return this.f6798g;
    }

    public boolean v() {
        return this.f6795d != null || this.f6794c.size() > 0;
    }

    public boolean w() {
        return this.f6801j;
    }

    public void y(InterfaceC0703b interfaceC0703b, int i2, int i3, C0406c c0406c) {
        f(c0406c, q(interfaceC0703b, i2, i3));
    }

    public void z(InterfaceC0703b interfaceC0703b, int i2, int i3, int i4, C0406c c0406c) {
        d dVarQ = q(interfaceC0703b, i2, i3);
        dVarQ.j(i4);
        f(c0406c, dVarQ);
    }
}
