package com.oo.seex.oo_statistics_lib.utils.utdid.core;


import android.util.Xml;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlSerializer;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

class XmlUtils {
    XmlUtils() {
    }

    public static void skipCurrentTag(XmlPullParser parser) throws XmlPullParserException, IOException {
        int outerDepth = parser.getDepth();

        int type;
        while ((type = parser.next()) != 1) {
            if (type == 3 && parser.getDepth() <= outerDepth) {
                break;
            }
        }

    }

    public static final int convertValueToList(CharSequence value, String[] options, int defaultValue) {
        if (null != value) {
            for (int i = 0; i < options.length; ++i) {
                if (value.equals(options[i])) {
                    return i;
                }
            }
        }

        return defaultValue;
    }

    public static final boolean convertValueToBoolean(CharSequence value, boolean defaultValue) {
        boolean result = false;
        if (null == value) {
            return defaultValue;
        } else {
            if (value.equals("1") || value.equals("true") || value.equals("TRUE")) {
                result = true;
            }

            return result;
        }
    }

    public static final int convertValueToInt(CharSequence charSeq, int defaultValue) {
        if (null == charSeq) {
            return defaultValue;
        } else {
            String nm = charSeq.toString();
            byte sign = 1;
            int index = 0;
            int len = nm.length();
            byte base = 10;
            if (45 == nm.charAt(0)) {
                sign = -1;
                ++index;
            }

            if (48 == nm.charAt(index)) {
                if (index == len - 1) {
                    return 0;
                }

                char c = nm.charAt(index + 1);
                if (120 != c && 88 != c) {
                    ++index;
                    base = 8;
                } else {
                    index += 2;
                    base = 16;
                }
            } else if (35 == nm.charAt(index)) {
                ++index;
                base = 16;
            }

            return Integer.parseInt(nm.substring(index), base) * sign;
        }
    }

    public static final int convertValueToUnsignedInt(String value, int defaultValue) {
        return null == value ? defaultValue : parseUnsignedIntAttribute(value);
    }

    public static final int parseUnsignedIntAttribute(CharSequence charSeq) {
        String value = charSeq.toString();
        int index = 0;
        int len = value.length();
        byte base = 10;
        if (48 == value.charAt(index)) {
            if (index == len - 1) {
                return 0;
            }

            char c = value.charAt(index + 1);
            if (120 != c && 88 != c) {
                ++index;
                base = 8;
            } else {
                index += 2;
                base = 16;
            }
        } else if (35 == value.charAt(index)) {
            ++index;
            base = 16;
        }

        return (int) Long.parseLong(value.substring(index), base);
    }

    public static final void writeMapXml(Map val, OutputStream out) throws XmlPullParserException, IOException {
        FastXmlSerializer serializer = new FastXmlSerializer();
        serializer.setOutput(out, "utf-8");
        serializer.startDocument((String) null, Boolean.valueOf(true));
        serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
        writeMapXml(val, (String) null, serializer);
        serializer.endDocument();
    }

    public static final void writeListXml(List val, OutputStream out) throws XmlPullParserException, IOException {
        XmlSerializer serializer = Xml.newSerializer();
        serializer.setOutput(out, "utf-8");
        serializer.startDocument((String) null, Boolean.valueOf(true));
        serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
        writeListXml(val, (String) null, serializer);
        serializer.endDocument();
    }

    public static final void writeMapXml(Map val, String name, XmlSerializer out) throws XmlPullParserException, IOException {
        if (val == null) {
            out.startTag((String) null, "null");
            out.endTag((String) null, "null");
        } else {
            Set s = val.entrySet();
            Iterator i = s.iterator();
            out.startTag((String) null, "map");
            if (name != null) {
                out.attribute((String) null, "name", name);
            }

            while (i.hasNext()) {
                Entry e = (Entry) i.next();
                writeValueXml(e.getValue(), (String) e.getKey(), out);
            }

            out.endTag((String) null, "map");
        }
    }

    public static final void writeListXml(List val, String name, XmlSerializer out) throws XmlPullParserException, IOException {
        if (val == null) {
            out.startTag((String) null, "null");
            out.endTag((String) null, "null");
        } else {
            out.startTag((String) null, "list");
            if (name != null) {
                out.attribute((String) null, "name", name);
            }

            int N = val.size();

            for (int i = 0; i < N; ++i) {
                writeValueXml(val.get(i), (String) null, out);
            }

            out.endTag((String) null, "list");
        }
    }

    public static final void writeByteArrayXml(byte[] val, String name, XmlSerializer out) throws XmlPullParserException, IOException {
        if (val == null) {
            out.startTag((String) null, "null");
            out.endTag((String) null, "null");
        } else {
            out.startTag((String) null, "byte-array");
            if (name != null) {
                out.attribute((String) null, "name", name);
            }

            int N = val.length;
            out.attribute((String) null, "num", Integer.toString(N));
            StringBuilder sb = new StringBuilder(val.length * 2);

            for (int i = 0; i < N; ++i) {
                byte b = val[i];
                int h = b >> 4;
                sb.append(h >= 10 ? 97 + h - 10 : 48 + h);
                h = b & 255;
                sb.append(h >= 10 ? 97 + h - 10 : 48 + h);
            }

            out.text(sb.toString());
            out.endTag((String) null, "byte-array");
        }
    }

    public static final void writeIntArrayXml(int[] val, String name, XmlSerializer out) throws XmlPullParserException, IOException {
        if (val == null) {
            out.startTag((String) null, "null");
            out.endTag((String) null, "null");
        } else {
            out.startTag((String) null, "int-array");
            if (name != null) {
                out.attribute((String) null, "name", name);
            }

            int N = val.length;
            out.attribute((String) null, "num", Integer.toString(N));

            for (int i = 0; i < N; ++i) {
                out.startTag((String) null, "item");
                out.attribute((String) null, "value", Integer.toString(val[i]));
                out.endTag((String) null, "item");
            }

            out.endTag((String) null, "int-array");
        }
    }

    public static final void writeValueXml(Object v, String name, XmlSerializer out) throws XmlPullParserException, IOException {
        if (v == null) {
            out.startTag((String) null, "null");
            if (name != null) {
                out.attribute((String) null, "name", name);
            }

            out.endTag((String) null, "null");
        } else if (v instanceof String) {
            out.startTag((String) null, "string");
            if (name != null) {
                out.attribute((String) null, "name", name);
            }

            out.text(v.toString());
            out.endTag((String) null, "string");
        } else {
            String typeStr;
            if (v instanceof Integer) {
                typeStr = "int";
            } else if (v instanceof Long) {
                typeStr = "long";
            } else if (v instanceof Float) {
                typeStr = "float";
            } else if (v instanceof Double) {
                typeStr = "double";
            } else {
                if (!(v instanceof Boolean)) {
                    if (v instanceof byte[]) {
                        writeByteArrayXml((byte[]) ((byte[]) v), name, out);
                        return;
                    }

                    if (v instanceof int[]) {
                        writeIntArrayXml((int[]) ((int[]) v), name, out);
                        return;
                    }

                    if (v instanceof Map) {
                        writeMapXml((Map) v, name, out);
                        return;
                    }

                    if (v instanceof List) {
                        writeListXml((List) v, name, out);
                        return;
                    }

                    if (v instanceof CharSequence) {
                        out.startTag((String) null, "string");
                        if (name != null) {
                            out.attribute((String) null, "name", name);
                        }

                        out.text(v.toString());
                        out.endTag((String) null, "string");
                        return;
                    }

                    throw new RuntimeException("writeValueXml: unable to write value " + v);
                }

                typeStr = "boolean";
            }

            out.startTag((String) null, typeStr);
            if (name != null) {
                out.attribute((String) null, "name", name);
            }

            out.attribute((String) null, "value", v.toString());
            out.endTag((String) null, typeStr);
        }
    }

    public static final HashMap readMapXml(InputStream in) throws XmlPullParserException, IOException {
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(in, (String) null);
        return (HashMap) readValueXml(parser, new String[1]);
    }

    public static final ArrayList readListXml(InputStream in) throws XmlPullParserException, IOException {
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(in, (String) null);
        return (ArrayList) readValueXml(parser, new String[1]);
    }

    public static final HashMap readThisMapXml(XmlPullParser parser, String endTag, String[] name) throws XmlPullParserException, IOException {
        HashMap map = new HashMap();
        int eventType = parser.getEventType();

        do {
            if (eventType == 2) {
                Object val = readThisValueXml(parser, name);
                if (name[0] == null) {
                    throw new XmlPullParserException("Map value without name attribute: " + parser.getName());
                }

                map.put(name[0], val);
            } else if (eventType == 3) {
                if (parser.getName().equals(endTag)) {
                    return map;
                }

                throw new XmlPullParserException("Expected " + endTag + " end tag at: " + parser.getName());
            }

            eventType = parser.next();
        } while (eventType != 1);

        throw new XmlPullParserException("Document ended before " + endTag + " end tag");
    }

    public static final ArrayList readThisListXml(XmlPullParser parser, String endTag, String[] name) throws XmlPullParserException, IOException {
        ArrayList list = new ArrayList();
        int eventType = parser.getEventType();

        do {
            if (eventType == 2) {
                Object val = readThisValueXml(parser, name);
                list.add(val);
            } else if (eventType == 3) {
                if (parser.getName().equals(endTag)) {
                    return list;
                }

                throw new XmlPullParserException("Expected " + endTag + " end tag at: " + parser.getName());
            }

            eventType = parser.next();
        } while (eventType != 1);

        throw new XmlPullParserException("Document ended before " + endTag + " end tag");
    }

    public static final int[] readThisIntArrayXml(XmlPullParser parser, String endTag, String[] name) throws XmlPullParserException, IOException {
        int num;
        try {
            num = Integer.parseInt(parser.getAttributeValue((String) null, "num"));
        } catch (NullPointerException var10) {
            throw new XmlPullParserException("Need num attribute in byte-array");
        } catch (NumberFormatException var11) {
            throw new XmlPullParserException("Not a number in num attribute in byte-array");
        }

        int[] array = new int[num];
        int i = 0;
        int eventType = parser.getEventType();

        do {
            if (eventType == 2) {
                if (!parser.getName().equals("item")) {
                    throw new XmlPullParserException("Expected item tag at: " + parser.getName());
                }

                try {
                    array[i] = Integer.parseInt(parser.getAttributeValue((String) null, "value"));
                } catch (NullPointerException var8) {
                    throw new XmlPullParserException("Need value attribute in item");
                } catch (NumberFormatException var9) {
                    throw new XmlPullParserException("Not a number in value attribute in item");
                }
            } else if (eventType == 3) {
                if (parser.getName().equals(endTag)) {
                    return array;
                }

                if (!parser.getName().equals("item")) {
                    throw new XmlPullParserException("Expected " + endTag + " end tag at: " + parser.getName());
                }

                ++i;
            }

            eventType = parser.next();
        } while (eventType != 1);

        throw new XmlPullParserException("Document ended before " + endTag + " end tag");
    }

    public static final Object readValueXml(XmlPullParser parser, String[] name) throws XmlPullParserException, IOException {
        int eventType = parser.getEventType();

        while (eventType != 2) {
            if (eventType == 3) {
                throw new XmlPullParserException("Unexpected end tag at: " + parser.getName());
            }

            if (eventType == 4) {
                throw new XmlPullParserException("Unexpected text: " + parser.getText());
            }

            try {
                eventType = parser.next();
            } catch (Exception var4) {
                throw new XmlPullParserException("Unexpected call next(): " + parser.getName());
            }

            if (eventType == 1) {
                throw new XmlPullParserException("Unexpected end of document");
            }
        }

        return readThisValueXml(parser, name);
    }

    private static final Object readThisValueXml(XmlPullParser parser, String[] name) throws XmlPullParserException, IOException {
        String valueName = parser.getAttributeValue((String) null, "name");
        String tagName = parser.getName();
        Object res;
        if (tagName.equals("null")) {
            res = null;
        } else {
            if (tagName.equals("string")) {
                String eventType2 = "";

                int eventType1;
                while ((eventType1 = parser.next()) != 1) {
                    if (eventType1 == 3) {
                        if (parser.getName().equals("string")) {
                            name[0] = valueName;
                            return eventType2;
                        }

                        throw new XmlPullParserException("Unexpected end tag in <string>: " + parser.getName());
                    }

                    if (eventType1 == 4) {
                        eventType2 = eventType2 + parser.getText();
                    } else if (eventType1 == 2) {
                        throw new XmlPullParserException("Unexpected start tag in <string>: " + parser.getName());
                    }
                }

                throw new XmlPullParserException("Unexpected end of document in <string>");
            }

            if (tagName.equals("int")) {
                res = Integer.valueOf(Integer.parseInt(parser.getAttributeValue((String) null, "value")));
            } else if (tagName.equals("long")) {
                res = Long.valueOf(parser.getAttributeValue((String) null, "value"));
            } else if (tagName.equals("float")) {
                res = new Float(parser.getAttributeValue((String) null, "value"));
            } else if (tagName.equals("double")) {
                res = new Double(parser.getAttributeValue((String) null, "value"));
            } else {
                if (!tagName.equals("boolean")) {
                    if (tagName.equals("int-array")) {
                        parser.next();
                        int[] res3 = readThisIntArrayXml(parser, "int-array", name);
                        name[0] = valueName;
                        return res3;
                    }

                    if (tagName.equals("map")) {
                        parser.next();
                        HashMap res2 = readThisMapXml(parser, "map", name);
                        name[0] = valueName;
                        return res2;
                    }

                    if (tagName.equals("list")) {
                        parser.next();
                        ArrayList res1 = readThisListXml(parser, "list", name);
                        name[0] = valueName;
                        return res1;
                    }

                    throw new XmlPullParserException("Unknown tag: " + tagName);
                }

                res = Boolean.valueOf(parser.getAttributeValue((String) null, "value"));
            }
        }

        int eventType;
        do {
            if ((eventType = parser.next()) == 1) {
                throw new XmlPullParserException("Unexpected end of document in <" + tagName + ">");
            }

            if (eventType == 3) {
                if (parser.getName().equals(tagName)) {
                    name[0] = valueName;
                    return res;
                }

                throw new XmlPullParserException("Unexpected end tag in <" + tagName + ">: " + parser.getName());
            }

            if (eventType == 4) {
                throw new XmlPullParserException("Unexpected text in <" + tagName + ">: " + parser.getName());
            }
        } while (eventType != 2);

        throw new XmlPullParserException("Unexpected start tag in <" + tagName + ">: " + parser.getName());
    }

    public static final void beginDocument(XmlPullParser parser, String firstElementName) throws XmlPullParserException, IOException {
        int type;
        while ((type = parser.next()) != 2 && type != 1) {
            ;
        }

        if (type != 2) {
            throw new XmlPullParserException("No start tag found");
        } else if (!parser.getName().equals(firstElementName)) {
            throw new XmlPullParserException("Unexpected start tag: found " + parser.getName() + ", expected " + firstElementName);
        }
    }

    public static final void nextElement(XmlPullParser parser) throws XmlPullParserException, IOException {
        int type;
        while ((type = parser.next()) != 2 && type != 1) {
            ;
        }

    }
}
