package r7;

import java.io.IOException;
import java.io.InterruptedIOException;
import java.util.ArrayDeque;

/* JADX INFO: loaded from: classes.dex */
public final class w {

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final o f16686b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public long f16687c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public long f16688d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public long f16689e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public long f16690f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final ArrayDeque f16691g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public boolean f16692h;
    public final u i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public final t f16693j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public final v f16694k;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public final v f16695l;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public int f16696m;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public IOException f16697n;

    public w(int i, o oVar, boolean z8, boolean z9, k7.o oVar2) {
        P6.g.e(oVar, "connection");
        this.f16685a = i;
        this.f16686b = oVar;
        this.f16690f = oVar.f16638G.a();
        ArrayDeque arrayDeque = new ArrayDeque();
        this.f16691g = arrayDeque;
        this.i = new u(this, oVar.f16637F.a(), z9);
        this.f16693j = new t(this, z8);
        this.f16694k = new v(this);
        this.f16695l = new v(this);
        if (oVar2 == null) {
            if (!h()) {
                throw new IllegalStateException("remotely-initiated streams should have headers");
            }
        } else {
            if (h()) {
                throw new IllegalStateException("locally-initiated streams shouldn't have headers yet");
            }
            arrayDeque.add(oVar2);
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:15:0x001b  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void a() {
        boolean z8;
        boolean zI;
        byte[] bArr = l7.b.f14412a;
        synchronized (this) {
            try {
                u uVar = this.i;
                if (uVar.f16679r || !uVar.f16682u) {
                    z8 = false;
                } else {
                    t tVar = this.f16693j;
                    if (tVar.f16674q || tVar.f16676s) {
                        z8 = true;
                    }
                }
                zI = i();
            } catch (Throwable th) {
                throw th;
            }
        }
        if (z8) {
            c(9, null);
        } else {
            if (zI) {
                return;
            }
            this.f16686b.j(this.f16685a);
        }
    }

    public final void b() throws IOException {
        t tVar = this.f16693j;
        if (tVar.f16676s) {
            throw new IOException("stream closed");
        }
        if (tVar.f16674q) {
            throw new IOException("stream finished");
        }
        if (this.f16696m != 0) {
            IOException iOException = this.f16697n;
            if (iOException != null) {
                throw iOException;
            }
            int i = this.f16696m;
            I1.a.z(i);
            throw new B(i);
        }
    }

    public final void c(int i, IOException iOException) {
        I1.a.t(i, "rstStatusCode");
        if (d(i, iOException)) {
            o oVar = this.f16686b;
            oVar.getClass();
            I1.a.t(i, "statusCode");
            oVar.f16643M.r(this.f16685a, i);
        }
    }

    public final boolean d(int i, IOException iOException) {
        byte[] bArr = l7.b.f14412a;
        synchronized (this) {
            if (this.f16696m != 0) {
                return false;
            }
            this.f16696m = i;
            this.f16697n = iOException;
            notifyAll();
            if (this.i.f16679r) {
                if (this.f16693j.f16674q) {
                    return false;
                }
            }
            this.f16686b.j(this.f16685a);
            return true;
        }
    }

    public final void e(int i) {
        I1.a.t(i, "errorCode");
        if (d(i, null)) {
            this.f16686b.u(this.f16685a, i);
        }
    }

    public final synchronized int f() {
        return this.f16696m;
    }

    public final t g() {
        synchronized (this) {
            if (!this.f16692h && !h()) {
                throw new IllegalStateException("reply before requesting the sink");
            }
        }
        return this.f16693j;
    }

    public final boolean h() {
        boolean z8 = (this.f16685a & 1) == 1;
        this.f16686b.getClass();
        return true == z8;
    }

    public final synchronized boolean i() {
        if (this.f16696m != 0) {
            return false;
        }
        u uVar = this.i;
        if (uVar.f16679r || uVar.f16682u) {
            t tVar = this.f16693j;
            if (tVar.f16674q || tVar.f16676s) {
                if (this.f16692h) {
                    return false;
                }
            }
        }
        return true;
    }

    public final void j(k7.o oVar, boolean z8) {
        boolean zI;
        P6.g.e(oVar, "headers");
        byte[] bArr = l7.b.f14412a;
        synchronized (this) {
            try {
                if (this.f16692h && z8) {
                    this.i.getClass();
                } else {
                    this.f16692h = true;
                    this.f16691g.add(oVar);
                }
                if (z8) {
                    this.i.f16679r = true;
                }
                zI = i();
                notifyAll();
            } catch (Throwable th) {
                throw th;
            }
        }
        if (zI) {
            return;
        }
        this.f16686b.j(this.f16685a);
    }

    public final synchronized void k(int i) {
        I1.a.t(i, "errorCode");
        if (this.f16696m == 0) {
            this.f16696m = i;
            notifyAll();
        }
    }

    public final void l() throws InterruptedIOException {
        try {
            wait();
        } catch (InterruptedException unused) {
            Thread.currentThread().interrupt();
            throw new InterruptedIOException();
        }
    }
}
