package K;

import I.l;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import b0.AbstractC0399d;
import b0.D;
import java.util.Collections;
import java.util.TreeSet;
import n.C;
import n.C0633A;
import n.InterfaceC0634B;
import n.x;
import n.y;
import okhttp3.HttpUrl;

/* JADX INFO: loaded from: classes.dex */
public final class f extends C implements Handler.Callback {

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

    /* JADX INFO: renamed from: B, reason: collision with root package name */
    private String f1184B;

    /* JADX INFO: renamed from: C, reason: collision with root package name */
    private b f1185C;

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    private final e f1186q;

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

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

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    private final y f1189t;

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    private final C0633A f1190u;

    /* JADX INFO: renamed from: v, reason: collision with root package name */
    private final StringBuilder f1191v;

    /* JADX INFO: renamed from: w, reason: collision with root package name */
    private final TreeSet f1192w;

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

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

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

    public f(InterfaceC0634B interfaceC0634B, l lVar, Looper looper, String... strArr) {
        super((strArr == null || strArr.length == 0) ? HttpUrl.FRAGMENT_ENCODE_SET : strArr[0], interfaceC0634B);
        this.f1187r = (l) AbstractC0399d.d(lVar);
        this.f1188s = looper == null ? null : new Handler(looper, this);
        this.f1186q = new e();
        this.f1189t = new y();
        this.f1190u = new C0633A(1);
        this.f1191v = new StringBuilder();
        this.f1192w = new TreeSet();
    }

    private void N() {
        C0633A c0633a = this.f1190u;
        c0633a.f5412e = -1L;
        c0633a.a();
    }

    private void O(c cVar) {
        b bVar;
        int length = cVar.f1174r.length;
        if (length == 0) {
            return;
        }
        boolean z2 = false;
        for (int i2 = 0; i2 < length; i2++) {
            a aVar = cVar.f1174r[i2];
            if (aVar.f1169a == 0) {
                b bVar2 = (b) aVar;
                z2 = length == 1 && bVar2.c();
                if (z2 && (bVar = this.f1185C) != null && bVar.f1170b == bVar2.f1170b && bVar.f1171c == bVar2.f1171c) {
                    this.f1185C = null;
                } else {
                    if (z2) {
                        this.f1185C = bVar2;
                    }
                    if (bVar2.a()) {
                        Q(bVar2);
                    } else if (bVar2.b()) {
                        R();
                    }
                }
            } else {
                S((d) aVar);
            }
        }
        if (!z2) {
            this.f1185C = null;
        }
        int i3 = this.f1194y;
        if (i3 == 1 || i3 == 3) {
            this.f1183A = P();
        }
    }

    private String P() {
        int length = this.f1191v.length();
        if (length == 0) {
            return null;
        }
        int i2 = length - 1;
        boolean z2 = this.f1191v.charAt(i2) == '\n';
        if (length == 1 && z2) {
            return null;
        }
        if (z2) {
            length = i2;
        }
        if (this.f1194y != 1) {
            return this.f1191v.substring(0, length);
        }
        int iLastIndexOf = length;
        for (int i3 = 0; i3 < this.f1195z && iLastIndexOf != -1; i3++) {
            iLastIndexOf = this.f1191v.lastIndexOf("\n", iLastIndexOf - 1);
        }
        int i4 = iLastIndexOf != -1 ? iLastIndexOf + 1 : 0;
        this.f1191v.delete(0, i4);
        return this.f1191v.substring(0, length - i4);
    }

    private void Q(b bVar) {
        byte b2 = bVar.f1171c;
        if (b2 == 32) {
            Y(2);
            return;
        }
        if (b2 == 41) {
            Y(3);
            return;
        }
        switch (b2) {
            case 37:
                this.f1195z = 2;
                Y(1);
                break;
            case 38:
                this.f1195z = 3;
                Y(1);
                break;
            case 39:
                this.f1195z = 4;
                Y(1);
                break;
            default:
                int i2 = this.f1194y;
                if (i2 != 0) {
                    if (b2 != 33) {
                        switch (b2) {
                            case 44:
                                this.f1183A = null;
                                if (i2 == 1 || i2 == 3) {
                                    this.f1191v.setLength(0);
                                }
                                break;
                            case 45:
                                W();
                                break;
                            case 46:
                                this.f1191v.setLength(0);
                                break;
                            case 47:
                                this.f1183A = P();
                                this.f1191v.setLength(0);
                                break;
                        }
                    } else {
                        if (this.f1191v.length() > 0) {
                            StringBuilder sb = this.f1191v;
                            sb.setLength(sb.length() - 1);
                        }
                        break;
                    }
                }
                break;
        }
    }

    private void R() {
        W();
    }

    private void S(d dVar) {
        if (this.f1194y != 0) {
            this.f1191v.append(dVar.f1175b);
        }
    }

    private void T(String str) {
        if (D.a(this.f1184B, str)) {
            return;
        }
        this.f1184B = str;
        Handler handler = this.f1188s;
        if (handler != null) {
            handler.obtainMessage(0, str).sendToTarget();
        } else {
            U(str);
        }
    }

    private void U(String str) {
        if (str == null) {
            this.f1187r.onCues(Collections.emptyList());
        } else {
            this.f1187r.onCues(Collections.singletonList(new I.c(str)));
        }
    }

    private boolean V() {
        return this.f1190u.f5412e != -1;
    }

    private void W() {
        int length = this.f1191v.length();
        if (length <= 0 || this.f1191v.charAt(length - 1) == '\n') {
            return;
        }
        this.f1191v.append('\n');
    }

    private void X(long j2) {
        C0633A c0633a = this.f1190u;
        if (c0633a.f5412e > j2 + 5000000) {
            return;
        }
        c cVarJ = this.f1186q.j(c0633a);
        N();
        if (cVarJ != null) {
            this.f1192w.add(cVarJ);
        }
    }

    private void Y(int i2) {
        if (this.f1194y == i2) {
            return;
        }
        this.f1194y = i2;
        this.f1191v.setLength(0);
        if (i2 == 1 || i2 == 0) {
            this.f1183A = null;
        }
    }

    @Override // n.C
    protected void D(long j2, long j3, boolean z2) {
        if (V()) {
            X(j2);
        }
        int iK = this.f1193x ? -1 : -3;
        while (!V() && iK == -3) {
            iK = K(j2, this.f1189t, this.f1190u);
            if (iK == -3) {
                X(j2);
            } else if (iK == -1) {
                this.f1193x = true;
            }
        }
        while (!this.f1192w.isEmpty() && ((c) this.f1192w.first()).f1172b <= j2) {
            c cVar = (c) this.f1192w.pollFirst();
            O(cVar);
            if (!cVar.f1173g) {
                T(this.f1183A);
            }
        }
    }

    @Override // n.C
    protected boolean F(x xVar) {
        return this.f1186q.d(xVar.f5842g);
    }

    @Override // n.C
    protected void J(long j2) {
        this.f1193x = false;
        this.f1185C = null;
        this.f1192w.clear();
        N();
        this.f1195z = 4;
        Y(0);
        T(null);
    }

    @Override // n.C, n.G
    protected long g() {
        return -3L;
    }

    @Override // android.os.Handler.Callback
    public boolean handleMessage(Message message) {
        if (message.what != 0) {
            return false;
        }
        U((String) message.obj);
        return true;
    }

    @Override // n.G
    protected boolean n() {
        return this.f1193x;
    }

    @Override // n.G
    protected boolean o() {
        return true;
    }

    @Override // n.C, n.G
    protected void q(long j2) {
        super.q(j2);
        T(null);
    }

    @Override // n.C, n.G
    protected void r(int i2, long j2, boolean z2) {
        super.r(i2, j2, z2);
    }
}
