/*
 * 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;

/**
 * Переход на новую строку
 */
public class UnitextNewline extends UnitextItem {

    /**
     * Количество переходов
     */
    public int count = 1;

    /**
     * Предположительный тип (вычисляется в ходе анализа)
     */
    public WhitespaceType typ = WhitespaceType.ORIGINAL;

    @Override
    public String toString() {
        return ("NewLine (" + count + ")" + (typ == WhitespaceType.IGNORABLE ? " - ignore" : (typ == WhitespaceType.ONESPACE ? " - space" : (typ == WhitespaceType.SOFT ? " - soft" : ""))));
    }

    @Override
    public UnitextItem clone() {
        UnitextNewline res = new UnitextNewline();
        res._cloneFrom(this);
        res.count = count;
        res.typ = typ;
        return res;
    }

    @Override
    public boolean getStartsOnNewline() {
        return true;
    }


    @Override
    public void getPlaintext(StringBuilder res, GetPlaintextParam pars) {
        if (pars != null && pars.setPositions) 
            beginChar = res.length();
        if (typ == WhitespaceType.IGNORABLE) {
        }
        else if (typ == WhitespaceType.ONESPACE) 
            res.append(' ');
        else 
            for (int i = 0; i < count; i++) {
                res.append(((((pars != null ? pars : UnitextItem.m_DefParams))).newLine != null ? (((pars != null ? pars : UnitextItem.m_DefParams))).newLine : ""));
            }
        if (pars != null && pars.setPositions) 
            endChar = res.length() - 1;
    }

    private static boolean _isAllUpper(UnitextPlaintext it) {
        if (it == null) 
            return false;
        for (char ch : it.text.toCharArray()) {
            if (Character.isLetter(ch)) {
                if (!Character.isUpperCase(ch)) 
                    return false;
            }
        }
        return true;
    }

    @Override
    public void getHtml(StringBuilder res, GetHtmlParam par) {
        if (typ == WhitespaceType.IGNORABLE) 
            return;
        if (typ == WhitespaceType.ONESPACE) {
            res.append(' ');
            return;
        }
        if (!par.callBefore(this, res)) 
            return;
        boolean isDummy = false;
        if (parent instanceof UnitextContainer) {
            if (((UnitextContainer)com.pullenti.unisharp.Utils.cast(parent, UnitextContainer.class)).children.size() < 1000) {
                int ii = ((UnitextContainer)com.pullenti.unisharp.Utils.cast(parent, UnitextContainer.class)).getChildIndexOf(this);
                if (ii > 0 && (ii < (((UnitextContainer)com.pullenti.unisharp.Utils.cast(parent, UnitextContainer.class)).children.size() - 1))) {
                    UnitextContainer cnt = (UnitextContainer)com.pullenti.unisharp.Utils.cast(parent, UnitextContainer.class);
                    if (cnt.typ == UnitextContainerType.NAME) {
                        isDummy = true;
                        if ((ii == 1 && (cnt.children.get(ii - 1) instanceof UnitextPlaintext) && ((ii + 1) < cnt.children.size())) && (cnt.children.get(ii + 1) instanceof UnitextPlaintext)) {
                            if (_isAllUpper((UnitextPlaintext)com.pullenti.unisharp.Utils.cast(cnt.children.get(ii - 1), UnitextPlaintext.class)) && !_isAllUpper((UnitextPlaintext)com.pullenti.unisharp.Utils.cast(cnt.children.get(ii + 1), UnitextPlaintext.class))) 
                                isDummy = false;
                        }
                    }
                    else if (parent.parent instanceof UnitextDocblock) {
                        boolean hasDb = false;
                        for (UnitextItem ch : ((UnitextContainer)com.pullenti.unisharp.Utils.cast(parent, UnitextContainer.class)).children) {
                            if (ch instanceof UnitextDocblock) 
                                hasDb = true;
                        }
                        if (!hasDb) {
                        }
                    }
                }
            }
        }
        if (isDummy) 
            res.append(" ");
        else {
            for (int i = 0; i < count; i++) {
                res.append("\r\n<BR/>");
            }
            if (par != null) 
                par._outFootnotes(res);
        }
        par.callAfter(this, res);
    }

    @Override
    public void getXml(com.pullenti.unisharp.XmlWriterWrapper xml) throws javax.xml.stream.XMLStreamException {
        xml.wr.writeStartElement("newline");
        this._writeXmlAttrs(xml);
        if (count > 1) 
            xml.wr.writeAttribute("count", ((Integer)count).toString());
        if (typ != WhitespaceType.ORIGINAL) 
            xml.wr.writeAttribute("typ", typ.toString().toLowerCase());
        xml.wr.writeEndElement();
    }

    @Override
    public void fromXml(org.w3c.dom.Node xml) throws NumberFormatException {
        super.fromXml(xml);
        if (xml.getAttributes() != null) {
            for (org.w3c.dom.Node a : (new com.pullenti.unisharp.XmlAttrListWrapper(xml.getAttributes())).arr) {
                if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(a), "count")) 
                    count = com.pullenti.unisharp.Utils.parseInteger(com.pullenti.unisharp.Utils.getXmlAttrByName(xml.getAttributes(), "count").getNodeValue(), 0, null);
                else if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(a), "typ")) {
                    try {
                        typ = (WhitespaceType)WhitespaceType.of(a.getNodeValue());
                    } catch (Exception ex617) {
                    }
                }
            }
        }
    }

    @Override
    public boolean isWhitespaces() {
        return true;
    }


    @Override
    public boolean isInline() {
        if (typ != WhitespaceType.ORIGINAL && typ != WhitespaceType.SOFT) 
            return true;
        return false;
    }


    @Override
    public void correct(com.pullenti.unitext.internal.uni.LocCorrTyp _typ, Object data) {
        if (_typ == com.pullenti.unitext.internal.uni.LocCorrTyp.MERGENEWLINES) {
            if (count == 2) 
                count = 1;
        }
    }

    @Override
    public void setDefaultTextPos(com.pullenti.unisharp.Outargwrapper<Integer> cp, StringBuilder res) {
        beginChar = cp.value;
        cp.value += count;
        endChar = cp.value - 1;
        if (res != null) {
            for (int i = 0; i < count; i++) {
                res.append('\n');
            }
        }
    }

    public static UnitextNewline _new352(int _arg1, String _arg2) {
        UnitextNewline res = new UnitextNewline();
        res.count = _arg1;
        res.pageSectionId = _arg2;
        return res;
    }

    public static UnitextNewline _new369(String _arg1, WhitespaceType _arg2) {
        UnitextNewline res = new UnitextNewline();
        res.pageSectionId = _arg1;
        res.typ = _arg2;
        return res;
    }

    public static UnitextNewline _new398(UnitextItem _arg1) {
        UnitextNewline res = new UnitextNewline();
        res.parent = _arg1;
        return res;
    }

    public static UnitextNewline _new635(Object _arg1) {
        UnitextNewline res = new UnitextNewline();
        res.tag = _arg1;
        return res;
    }
    public UnitextNewline() {
        super();
    }
}
