package P;

import I.d;
import I.e;
import I.g;
import I.i;
import b0.o;
import b0.u;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import n.z;
import okhttp3.HttpUrl;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

/* JADX INFO: loaded from: classes.dex */
public class a implements i, e {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private final boolean f1556a;

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

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

    /* JADX INFO: renamed from: P.a$a, reason: collision with other inner class name */
    private static class C0031a {

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

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

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

        public C0031a() {
            this(-1L, -1L, HttpUrl.FRAGMENT_ENCODE_SET);
        }

        public String toString() {
            return "SmiCue [startTime=" + this.f1559a + ", endTime=" + this.f1560b + ", text=" + this.f1561c + "]";
        }

        public C0031a(long j2, long j3, String str) {
            this.f1559a = j2;
            this.f1560b = j3;
            this.f1561c = str;
        }
    }

    public a() {
        this(true);
    }

    private static long e(String str) {
        o.d("SmiParser", " parseTimestampUs  = " + str, 16);
        return Long.parseLong(str) * 1000;
    }

    @Override // I.i
    public boolean a(String str) {
        boolean z2 = "text/ext/smi".equals(str) || "text/smi".equals(str);
        o.d("SmiParser", "canParse  type = " + str + " can = " + z2, 16);
        return z2;
    }

    @Override // I.i
    public g b(byte[] bArr, int i2, int i3) {
        String str;
        if (this.f1558c == null) {
            this.f1558c = u.a(bArr, i2, i3);
        }
        ArrayList arrayList = new ArrayList();
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bArr, i2, i3);
        try {
            try {
                XmlPullParser xmlPullParserNewPullParser = this.f1557b.newPullParser();
                xmlPullParserNewPullParser.setFeature("http://xmlpull.org/v1/doc/features.html#relaxed", true);
                xmlPullParserNewPullParser.setInput(byteArrayInputStream, this.f1558c);
                C0031a c0031a = null;
                boolean z2 = false;
                for (int eventType = xmlPullParserNewPullParser.getEventType(); eventType != 1; eventType = xmlPullParserNewPullParser.getEventType()) {
                    String name = xmlPullParserNewPullParser.getName();
                    if (eventType == 2) {
                        if ("sync".equalsIgnoreCase(name)) {
                            o.d("SmiParser", "smicue = " + c0031a, 16);
                            if (!z2) {
                                o.d("SmiParser", "warning this sync is not in body tag!!!!! ", 16);
                            }
                            if (c0031a != null) {
                                arrayList.add(c0031a);
                            }
                            c0031a = new C0031a();
                            int attributeCount = xmlPullParserNewPullParser.getAttributeCount();
                            for (int i4 = 0; i4 < attributeCount; i4++) {
                                String attributeName = xmlPullParserNewPullParser.getAttributeName(i4);
                                String attributeValue = xmlPullParserNewPullParser.getAttributeValue(i4);
                                if (attributeName.equalsIgnoreCase("start")) {
                                    c0031a.f1559a = e(attributeValue);
                                } else if (attributeName.equalsIgnoreCase("end")) {
                                    c0031a.f1560b = e(attributeValue);
                                }
                            }
                        } else if ("p".equalsIgnoreCase(name)) {
                            o.d("SmiParser", "P tag: start ~~", 16);
                        } else if ("body".equalsIgnoreCase(name)) {
                            z2 = true;
                        } else if (!"br".equalsIgnoreCase(name)) {
                            o.d("SmiParser", "Ignoring unsupported tag: " + xmlPullParserNewPullParser.getName(), 16);
                        } else if (c0031a != null && (str = c0031a.f1561c) != null && !str.isEmpty()) {
                            c0031a.f1561c += "\n";
                        }
                    } else if (eventType == 4) {
                        if (c0031a != null) {
                            c0031a.f1561c += f(xmlPullParserNewPullParser.getText());
                        }
                    } else if (eventType == 3) {
                        if ("body".equalsIgnoreCase(name)) {
                            if (c0031a != null) {
                                arrayList.add(c0031a);
                            }
                            c0031a = null;
                            z2 = false;
                        } else if ("sync".equalsIgnoreCase(name)) {
                            if (c0031a != null) {
                                arrayList.add(c0031a);
                            }
                            c0031a = null;
                        }
                        o.d("SmiParser", "end TAG : " + name, 16);
                    }
                    xmlPullParserNewPullParser.next();
                }
            } catch (Throwable th) {
                try {
                    byteArrayInputStream.close();
                } catch (IOException unused) {
                }
                throw th;
            }
        } catch (IOException e2) {
            throw new z(e2);
        } catch (XmlPullParserException e3) {
            e3.printStackTrace();
        }
        try {
            byteArrayInputStream.close();
        } catch (IOException unused2) {
        }
        if (arrayList.size() <= 0) {
            return new d();
        }
        String[] strArr = new String[arrayList.size()];
        long[] jArr = new long[arrayList.size() * 2];
        for (int i5 = 0; i5 < arrayList.size(); i5++) {
            int i6 = i5 * 2;
            C0031a c0031a2 = (C0031a) arrayList.get(i5);
            jArr[i6] = c0031a2.f1559a;
            long j2 = c0031a2.f1560b;
            if (j2 != -1) {
                jArr[i6 + 1] = j2;
            } else {
                int i7 = i5 + 1;
                if (i7 >= arrayList.size()) {
                    jArr[i6 + 1] = c0031a2.f1559a + 1000000;
                } else {
                    jArr[i6 + 1] = ((C0031a) arrayList.get(i7)).f1559a;
                }
            }
            strArr[i5] = c0031a2.f1561c;
        }
        return new d(strArr, 0L, jArr);
    }

    @Override // I.e
    public void d(String str) {
        this.f1558c = str;
    }

    protected String f(String str) {
        return this.f1556a ? str.replaceAll("\\<.*?>", HttpUrl.FRAGMENT_ENCODE_SET).replaceAll("&lt;", "<").replaceAll("&gt;", ">").replaceAll("&nbsp;", " ").replaceAll("&amp;", "&").replaceAll("&quota;", "\"").replaceAll("\n", HttpUrl.FRAGMENT_ENCODE_SET).trim() : str.trim();
    }

    public final void finalize() {
    }

    public a(boolean z2) {
        this.f1556a = z2;
        try {
            this.f1557b = XmlPullParserFactory.newInstance();
        } catch (XmlPullParserException e2) {
            throw new RuntimeException("Couldn't create XmlPullParserFactory instance", e2);
        }
    }
}
