/*
 * SDK Pullenti Unitext, version 4.33, fabruary 2026. Copyright (c) 2013-2026, Pullenti. All rights reserved. 
 * Non-Commercial Freeware and Commercial Software. 
 * This class is generated using the converter Unisharping (www.unisharping.ru) from Pullenti C# project. 
 * The latest version of the code is available on the site www.pullenti.ru
 */

package com.pullenti.unitext.internal.html;

public class EmlPart {

    public java.util.HashMap<String, String> attrs = new java.util.HashMap<String, String>();

    public byte[] data;

    public String stringData;

    public int stringDataPos;

    public String contentType;

    public String contentCharset;

    public String contentLocation;

    public String contentId;

    public String filename;

    public String boundaryRef;

    public String boundaryId;

    @Override
    public String toString() {
        StringBuilder tmp = new StringBuilder();
        if (boundaryId == null) 
            tmp.append("(*) ");
        tmp.append("Attrs: ").append(attrs.size()).append(", String: ").append((stringData == null ? 0 : stringData.length())).append(", Data: ").append((data == null ? 0 : data.length));
        if (contentType != null) 
            tmp.append(" ").append(contentType);
        if (contentCharset != null) 
            tmp.append(" ").append(contentCharset);
        if (stringData != null) {
            tmp.append("  ");
            if (stringData.length() < 40) 
                tmp.append(stringData);
            else 
                tmp.append(stringData.substring(0, 0 + 40) + "...");
        }
        return tmp.toString();
    }

    public static EmlPart tryParse(String txt, com.pullenti.unisharp.Outargwrapper<Integer> pos) {
        for (; pos.value < txt.length(); pos.value++) {
            if (!com.pullenti.unisharp.Utils.isWhitespace(txt.charAt(pos.value))) 
                break;
        }
        if ((pos.value + 1) >= txt.length()) 
            return null;
        EmlPart res = new EmlPart();
        if (txt.charAt(pos.value) == '-' && txt.charAt(pos.value + 1) == '-') {
            int p0 = pos.value + 2;
            for (pos.value += 2; pos.value < txt.length(); pos.value++) {
                if (com.pullenti.unisharp.Utils.isWhitespace(txt.charAt(pos.value))) 
                    break;
            }
            res.boundaryId = txt.substring(p0, p0 + pos.value - p0);
            for (; pos.value < txt.length(); pos.value++) {
                if (!com.pullenti.unisharp.Utils.isWhitespace(txt.charAt(pos.value))) 
                    break;
            }
        }
        StringBuilder attrValue = new StringBuilder();
        for (; pos.value < txt.length(); pos.value++) {
            int p0 = pos.value;
            boolean isAttr = false;
            for (; pos.value < txt.length(); pos.value++) {
                if (com.pullenti.unisharp.Utils.isWhitespace(txt.charAt(pos.value))) 
                    break;
                else if (txt.charAt(pos.value) == ':') {
                    isAttr = true;
                    break;
                }
            }
            if (!isAttr) 
                break;
            String attrName = txt.substring(p0, p0 + pos.value - p0).trim();
            if (com.pullenti.unisharp.Utils.stringsEq(attrName, "From")) {
            }
            pos.value++;
            attrValue.setLength(0);
            for (; pos.value < txt.length(); pos.value++) {
                char ch = txt.charAt(pos.value);
                if (ch == ' ' || ((int)ch) == 9) {
                    if (attrValue.length() == 0 || attrValue.charAt(attrValue.length() - 1) == ' ') 
                        continue;
                    attrValue.append(' ');
                    continue;
                }
                if (((int)ch) != 0xD && ((int)ch) != 0xA) {
                    attrValue.append(ch);
                    continue;
                }
                if (((pos.value + 1) < txt.length()) && ((int)ch) == 0xD && ((int)txt.charAt(pos.value + 1)) == 0xA) 
                    pos.value++;
                if (((pos.value + 1) < txt.length()) && ((((int)txt.charAt(pos.value + 1)) == 0xD || ((int)txt.charAt(pos.value + 1)) == 0xA || !com.pullenti.unisharp.Utils.isWhitespace(txt.charAt(pos.value + 1))))) 
                    break;
            }
            if (!res.attrs.containsKey(attrName)) 
                res.attrs.put(attrName, MhtHelper._decodeString(attrValue.toString()));
        }
        String transfEnc = null;
        for (java.util.Map.Entry<String, String> a : res.attrs.entrySet()) {
            if (com.pullenti.unisharp.Utils.stringsCompare(a.getKey(), "CONTENT-TYPE", true) == 0) {
                res.contentType = a.getValue();
                int i = a.getValue().indexOf(';');
                if (i > 0) 
                    res.contentType = res.contentType.substring(0, 0 + i);
                res.contentType = MhtHelper._corrString(res.contentType);
                i = a.getValue().indexOf("boundary=");
                if (i > 0) 
                    res.boundaryRef = MhtHelper._corrString(a.getValue().substring(i + 9).trim());
                i = a.getValue().indexOf("charset=");
                if (i > 0) 
                    res.contentCharset = MhtHelper._corrString(a.getValue().substring(i + 8).trim());
            }
            else if (com.pullenti.unisharp.Utils.stringsCompare(a.getKey(), "CONTENT-TRANSFER-ENCODING", true) == 0) 
                transfEnc = MhtHelper._corrString(a.getValue()).toLowerCase();
            else if (com.pullenti.unisharp.Utils.stringsCompare(a.getKey(), "CONTENT-ID", true) == 0) {
                res.contentId = (String)com.pullenti.unisharp.Utils.notnull(MhtHelper._corrString(a.getValue()), "");
                if (res.contentId.length() > 2 && res.contentId.charAt(0) == '<') 
                    res.contentId = res.contentId.substring(1, 1 + res.contentId.length() - 2);
            }
            else if (com.pullenti.unisharp.Utils.stringsCompare(a.getKey(), "CONTENT-LOCATION", true) == 0) 
                res.contentLocation = MhtHelper._corrString(a.getValue());
            else if (com.pullenti.unisharp.Utils.stringsCompare(a.getKey(), "CONTENT-DISPOSITION", true) == 0) {
                int i = a.getValue().indexOf("filename=");
                if (i > 0) 
                    res.filename = MhtHelper._corrString(a.getValue().substring(i + 9).trim());
            }
        }
        if (transfEnc == null && ((com.pullenti.unisharp.Utils.stringsEq(res.contentType, "text/plain") || com.pullenti.unisharp.Utils.stringsEq(res.contentType, "text/html")))) 
            transfEnc = "7bit";
        if (transfEnc == null) 
            return res;
        boolean isQP = com.pullenti.unisharp.Utils.stringsEq(transfEnc, "quoted-printable");
        boolean is64 = com.pullenti.unisharp.Utils.stringsEq(transfEnc, "base64");
        if (res.boundaryId == null) {
            if (!isQP && !is64) 
                return res;
        }
        com.pullenti.util.EncodingWrapper enc = null;
        if (isQP && res.contentCharset != null) {
            try {
                enc = new com.pullenti.util.EncodingWrapper(com.pullenti.util.EncodingStandard.UNDEFINED, res.contentCharset, 0);
            } catch (Exception ex) {
            }
        }
        if (isQP && enc == null) 
            enc = new com.pullenti.util.EncodingWrapper(com.pullenti.util.EncodingStandard.ACSII, null, 0);
        java.util.ArrayList<Byte> buf = new java.util.ArrayList<Byte>();
        res.stringDataPos = pos.value;
        StringBuilder _data = new StringBuilder();
        for (; pos.value < txt.length(); pos.value++) {
            char ch = txt.charAt(pos.value);
            if ((ch == '-' && ((pos.value + 1) < txt.length()) && txt.charAt(pos.value + 1) == '-') && ((((int)txt.charAt(pos.value - 1)) == 0xD || ((int)txt.charAt(pos.value - 1)) == 0xA)) && !com.pullenti.unisharp.Utils.isNullOrEmpty(res.boundaryId)) {
                int j;
                for (j = 0; (j < res.boundaryId.length()) && ((pos.value + 2 + j) < txt.length()); j++) {
                    if (res.boundaryId.charAt(j) != txt.charAt(pos.value + 2 + j)) 
                        break;
                }
                if (j >= res.boundaryId.length()) {
                    if (((pos.value + 4 + j) < txt.length()) && txt.charAt(pos.value + j + 2) == '-' && txt.charAt(pos.value + j + 3) == '-') 
                        pos.value += (j + 4);
                    break;
                }
            }
            if (isQP && ch == '=' && ((pos.value + 2) < txt.length())) {
                if (_toInt(txt.charAt(pos.value + 1)) >= 0 && _toInt(txt.charAt(pos.value + 2)) >= 0) {
                    int k = (_toInt(txt.charAt(pos.value + 1)) * 16) + _toInt(txt.charAt(pos.value + 2));
                    buf.add((byte)k);
                    pos.value += 2;
                    continue;
                }
                if (((int)txt.charAt(pos.value + 1)) == 0xD || ((int)txt.charAt(pos.value + 1)) == 0xA) {
                    pos.value++;
                    if (((int)txt.charAt(pos.value + 1)) == 0xD || ((int)txt.charAt(pos.value + 1)) == 0xA) 
                        pos.value++;
                    continue;
                }
            }
            if (is64 && com.pullenti.unisharp.Utils.isWhitespace(ch)) 
                continue;
            if (isQP) {
                if (buf.size() > 0) {
                    _data.append(enc.getString(com.pullenti.unisharp.Utils.toBytesArray(buf.toArray(new Byte[buf.size()])), 0, -1));
                    buf.clear();
                }
                _data.append(ch);
            }
            else 
                _data.append(ch);
        }
        if (is64) {
            if (_data.length() > 0) {
                String str = _data.toString();
                int ii = str.indexOf("--");
                if (ii > 0) 
                    str = str.substring(0, 0 + ii);
                try {
                    res.data = java.util.Base64.getDecoder().decode((str).replace("\r", "").replace("\n", ""));
                } catch (Exception ex) {
                }
            }
        }
        else if (isQP) {
            if (buf.size() > 0) {
                _data.append(enc.getString(com.pullenti.unisharp.Utils.toBytesArray(buf.toArray(new Byte[buf.size()])), 0, -1));
                buf.clear();
            }
            if (_data.length() > 0) 
                res.stringData = _data.toString();
        }
        else {
            if (com.pullenti.unisharp.Utils.stringsNe(transfEnc, "7bit")) {
            }
            res.stringData = _data.toString();
        }
        return res;
    }

    public static int _toInt(char ch) {
        if (ch >= '0' && ch <= '9') 
            return ((int)ch) - ((int)'0');
        if (ch >= 'A' && ch <= 'F') 
            return 10 + ((((int)ch) - ((int)'A')));
        if (ch >= 'a' && ch <= 'f') 
            return 10 + ((((int)ch) - ((int)'a')));
        return -1;
    }
    public EmlPart() {
    }
}
