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

/**
 * Cтруктурирующий блок из заголовочной части, тела, окончания и приложений. 
 * Выделяется только для некоторых форматов, если задать LoadDocumentStructure = true в параметрах создания. 
 * Но этот элемент активно используется на других этапах анализа, когда структура документа восстанавливается 
 * по плоскому тексту, а затем их иерархия оформляется этими элементами. Например, для нормативных актов 
 * это главы, статьи, части, пункты и подпункты.
 * Структурирующий блок с заголовком
 */
public class UnitextDocblock extends UnitextItem {

    /**
     * Заголовочная часть
     * 
     */
    public UnitextContainer head;

    /**
     * Содержимое
     * 
     */
    public UnitextItem body;

    /**
     * Подписная часть
     * 
     */
    public UnitextContainer tail;

    /**
     * Приложения
     * 
     */
    public UnitextItem appendix;

    /**
     * Тип блока (глава, статья, приложение и т.п. - если только ключевое слово явно указано)
     */
    public String typname;

    /**
     * Выделенный номер (если есть) - относится к Head
     */
    public String number;

    /**
     * Класс блока
     */
    public UnitextDocblockType typ = UnitextDocblockType.UNDEFINED;

    /**
     * Признак того, что фрагмент НПА утратил силу
     */
    public boolean expired = false;

    /**
     * Идентификатор внешнего блока, благодаря которому создан этот блок
     */
    public String extBlockId;

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        if (typname != null) 
            res.append(typname).append(" ");
        if (number != null) 
            res.append(number).append(" ");
        if (typ != UnitextDocblockType.UNDEFINED) 
            res.append("Typ:").append(typ.toString()).append(" ");
        return res.toString().trim();
    }

    @Override
    public UnitextItem clone() {
        UnitextDocblock res = new UnitextDocblock();
        res._cloneFrom(this);
        if (head != null) 
            res.head = (UnitextContainer)com.pullenti.unisharp.Utils.cast(head.clone(), UnitextContainer.class);
        if (body != null) 
            res.body = body.clone();
        if (tail != null) 
            res.tail = (UnitextContainer)com.pullenti.unisharp.Utils.cast(tail.clone(), UnitextContainer.class);
        if (appendix != null) 
            res.appendix = appendix.clone();
        res.extBlockId = extBlockId;
        res.typ = typ;
        res.typname = typname;
        res.number = number;
        res.expired = expired;
        return res;
    }

    @Override
    public boolean isInline() {
        return m_IsInline;
    }

    @Override
    public boolean setInline(boolean value) {
        m_IsInline = value;
        return value;
    }


    private boolean m_IsInline = false;

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


    @Override
    public String getInnerTag() {
        return "docblk";
    }


    @Override
    public UnitextItem findById(String _id) {
        if (com.pullenti.unisharp.Utils.stringsEq(id, _id)) 
            return this;
        UnitextItem res;
        if (head != null) {
            if ((((res = head.findById(_id)))) != null) 
                return res;
        }
        if (tail != null) {
            if ((((res = tail.findById(_id)))) != null) 
                return res;
        }
        if (body != null) {
            if ((((res = body.findById(_id)))) != null) 
                return res;
        }
        if (appendix != null) {
            if ((((res = appendix.findById(_id)))) != null) 
                return res;
        }
        return null;
    }

    @Override
    public void getPlaintext(StringBuilder res, GetPlaintextParam pars) {
        if (pars != null && pars.setPositions) 
            beginChar = res.length();
        if (head != null) 
            head.getPlaintext(res, pars);
        if (body != null) 
            body.getPlaintext(res, pars);
        if (tail != null) 
            tail.getPlaintext(res, pars);
        if (appendix != null) 
            appendix.getPlaintext(res, pars);
        if (pars != null && pars.setPositions) 
            endChar = res.length() - 1;
    }

    @Override
    public void getHtml(StringBuilder res, GetHtmlParam par) {
        if (!par.callBefore(this, res)) 
            return;
        if (com.pullenti.unisharp.Utils.stringsCompare((typname != null ? typname : ""), "Footnote", true) == 0) 
            return;
        String _tag = "div";
        if (isInline()) 
            _tag = "span";
        else if (com.pullenti.unisharp.Utils.stringsCompare((typname != null ? typname : ""), "Indention", true) == 0 && (parent instanceof UnitextContainer) && ((UnitextContainer)com.pullenti.unisharp.Utils.cast(parent, UnitextContainer.class)).children.get(0) == this) {
            UnitextDocblock blk = (UnitextDocblock)com.pullenti.unisharp.Utils.cast(parent.parent, UnitextDocblock.class);
            if (blk != null && blk.head != null && blk.head.isInline()) 
                _tag = "span";
        }
        if (com.pullenti.unisharp.Utils.stringsCompare((typname != null ? typname : ""), "Part", true) == 0 && head == null && com.pullenti.unisharp.Utils.stringsNe(number, "1")) 
            res.append("\r\n<BR/>");
        res.append("\r\n<").append(_tag);
        if (htmlTitle != null) {
            res.append(" title=\"");
            com.pullenti.util.MiscHelper.correctHtmlValue(res, htmlTitle, true, false, false);
            res.append("\"");
        }
        int margleft = 0;
        if (com.pullenti.unisharp.Utils.stringsEq(typname, "ITEM") || com.pullenti.unisharp.Utils.stringsEq(typname, "SUBITEM")) {
            for (UnitextItem p = parent; p != null; p = p.parent) {
                if (p instanceof UnitextDocblock) {
                    UnitextDocblock pb = (UnitextDocblock)com.pullenti.unisharp.Utils.cast(p, UnitextDocblock.class);
                    if (com.pullenti.unisharp.Utils.stringsEq(pb.typname, "ITEM") || com.pullenti.unisharp.Utils.stringsEq(pb.typname, "SUBITEM") || com.pullenti.unisharp.Utils.stringsEq(pb.typname, "CLAUSEPART")) 
                        margleft += 10;
                }
            }
        }
        if (com.pullenti.unisharp.Utils.stringsCompare((typname != null ? typname : ""), "Footnote", true) == 0) 
            res.append(" style=\"margin-left:").append((30 + margleft)).append("pt;margin-top:10pt;margin-bottom:10pt;font-size:smaller;text-align:left;font-weight:normal\"");
        else if (com.pullenti.unisharp.Utils.stringsCompare((typname != null ? typname : ""), "Index", true) == 0) 
            res.append(" style=\"border:1pt solid black; background:lightgray; text-align:left;font-weight:normal;font-style:italic\"");
        else if (typname != null) {
            int margtop = 0;
            if (com.pullenti.unisharp.Utils.stringsCompare(typname, "Chapter", true) == 0 || com.pullenti.unisharp.Utils.stringsCompare(typname, "Section", true) == 0) 
                margtop = 20;
            else if (com.pullenti.unisharp.Utils.stringsCompare(typname, "Paragraph", true) == 0 || com.pullenti.unisharp.Utils.stringsCompare(typname, "Subparagraph", true) == 0) 
                margtop = 15;
            else if (com.pullenti.unisharp.Utils.stringsCompare(typname, "Clause", true) == 0 || com.pullenti.unisharp.Utils.stringsCompare(typname, "Subsection", true) == 0) 
                margtop = 10;
            if (margtop > 0 || expired || margleft > 0) {
                res.append(" style=\"");
                if (margtop > 0) 
                    res.append("margin-top:").append(margtop).append("pt;");
                if (margleft > 0) 
                    res.append("margin-left:").append(margleft).append("pt;");
                if (expired) 
                    res.append("background-color:lightgray;");
                res.append("\"");
            }
        }
        if (id != null) 
            res.append(" id=\"").append(id).append("\"");
        res.append(">");
        if (head != null) {
            if (com.pullenti.unisharp.Utils.stringsCompare((typname != null ? typname : ""), "Appendix", true) == 0) {
                int i = 0;
                for (i = 0; i < head.children.size(); i++) {
                    if ((head.children.get(i) instanceof UnitextContainer) && ((UnitextContainer)com.pullenti.unisharp.Utils.cast(head.children.get(i), UnitextContainer.class)).typ == UnitextContainerType.NAME) 
                        break;
                }
                if (i > 0) {
                    res.append("\r\n<div style=\"text-align:right;font-style:italic\">");
                    for (int j = 0; j < i; j++) {
                        head.children.get(j).getHtml(res, par);
                    }
                    res.append("</div>");
                }
                if (i < head.children.size()) {
                    res.append("\r\n<div style=\"text-align:center;font-weight:bold;font-style:normal\">");
                    for (; i < head.children.size(); i++) {
                        head.children.get(i).getHtml(res, par);
                    }
                    res.append("</div>");
                }
                res.append("<div style=\"margin-bottom:20pt\"> </div>");
            }
            else {
                if (com.pullenti.unisharp.Utils.stringsCompare((typname != null ? typname : ""), "Mail", true) != 0) 
                    res.append("<b>");
                else 
                    res.append("<i>");
                head.getHtml(res, par);
                if (com.pullenti.unisharp.Utils.stringsCompare((typname != null ? typname : ""), "Mail", true) != 0) 
                    res.append("</b>");
                else 
                    res.append("</i>");
            }
            if (par != null) 
                par._outFootnotes(res);
        }
        if (body != null) {
            body.getHtml(res, par);
            if (par != null) 
                par._outFootnotes(res);
        }
        if (tail != null) {
            res.append("<i>");
            tail.getHtml(res, par);
            res.append("</i>");
            if (par != null) 
                par._outFootnotes(res);
        }
        if (appendix != null) {
            appendix.getHtml(res, par);
            if (par != null) 
                par._outFootnotes(res);
        }
        res.append("</").append(_tag).append(">");
        par.callAfter(this, res);
    }

    @Override
    public void getXml(com.pullenti.unisharp.XmlWriterWrapper xml) throws javax.xml.stream.XMLStreamException {
        xml.wr.writeStartElement("docblock");
        this._writeXmlAttrs(xml);
        if (typname != null) 
            xml.wr.writeAttribute("typname", typname);
        if (number != null) 
            xml.wr.writeAttribute("num", com.pullenti.util.MiscHelper.correctXmlValue(number));
        if (typ != UnitextDocblockType.UNDEFINED) 
            xml.wr.writeAttribute("typ", typ.toString().toLowerCase());
        if (m_IsInline) 
            xml.wr.writeAttribute("inline", "true");
        if (extBlockId != null) 
            xml.wr.writeAttribute("extblockid", extBlockId);
        if (expired) 
            xml.wr.writeAttribute("expired", "true");
        if (head != null) {
            xml.wr.writeStartElement("head");
            head.getXml(xml);
            xml.wr.writeEndElement();
        }
        if (body != null) {
            xml.wr.writeStartElement("body");
            body.getXml(xml);
            xml.wr.writeEndElement();
        }
        if (tail != null) {
            xml.wr.writeStartElement("tail");
            tail.getXml(xml);
            xml.wr.writeEndElement();
        }
        if (appendix != null) {
            xml.wr.writeStartElement("appendix");
            appendix.getXml(xml);
            xml.wr.writeEndElement();
        }
        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), "typname")) 
                    typname = a.getNodeValue();
                else if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(a), "num")) 
                    number = a.getNodeValue();
                else if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(a), "extblockid")) 
                    extBlockId = a.getNodeValue();
                else if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(a), "expired")) 
                    expired = com.pullenti.unisharp.Utils.stringsEq(a.getNodeValue(), "true");
                else if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(a), "inline")) 
                    m_IsInline = com.pullenti.unisharp.Utils.stringsEq(a.getNodeValue(), "true");
                else if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(a), "typ")) {
                    try {
                        typ = (UnitextDocblockType)UnitextDocblockType.of(a.getNodeValue());
                    } catch (Exception ex590) {
                    }
                }
            }
        }
        for (org.w3c.dom.Node x : (new com.pullenti.unisharp.XmlNodeListWrapper(xml.getChildNodes())).arr) {
            if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(x), "head")) {
                for (org.w3c.dom.Node xx : (new com.pullenti.unisharp.XmlNodeListWrapper(x.getChildNodes())).arr) {
                    head = (UnitextContainer)com.pullenti.unisharp.Utils.cast(com.pullenti.unitext.internal.uni.UnitextHelper.createItem(xx, this), UnitextContainer.class);
                    if (head != null) 
                        head.typ = UnitextContainerType.HEAD;
                    break;
                }
            }
            else if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(x), "body")) {
                for (org.w3c.dom.Node xx : (new com.pullenti.unisharp.XmlNodeListWrapper(x.getChildNodes())).arr) {
                    body = com.pullenti.unitext.internal.uni.UnitextHelper.createItem(xx, this);
                    break;
                }
            }
            else if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(x), "appendix")) {
                for (org.w3c.dom.Node xx : (new com.pullenti.unisharp.XmlNodeListWrapper(x.getChildNodes())).arr) {
                    appendix = com.pullenti.unitext.internal.uni.UnitextHelper.createItem(xx, this);
                    break;
                }
            }
            else if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(x), "tail")) {
                for (org.w3c.dom.Node xx : (new com.pullenti.unisharp.XmlNodeListWrapper(x.getChildNodes())).arr) {
                    tail = (UnitextContainer)com.pullenti.unisharp.Utils.cast(com.pullenti.unitext.internal.uni.UnitextHelper.createItem(xx, this), UnitextContainer.class);
                    if (tail != null) 
                        tail.typ = UnitextContainerType.TAIL;
                    break;
                }
            }
        }
    }

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


    @Override
    public void getAllItems(java.util.ArrayList<UnitextItem> res, int lev) {
        if (res != null) 
            res.add(this);
        if (head != null) {
            if (head.children.contains(head)) {
            }
            else if (head.children.contains(this)) {
            }
            else {
                head.parent = this;
                head.getAllItems(res, lev + 1);
            }
        }
        if (body != null) {
            body.parent = this;
            body.getAllItems(res, lev + 1);
        }
        if (tail != null) {
            tail.parent = this;
            tail.getAllItems(res, lev + 1);
        }
        if (appendix != null) {
            appendix.parent = this;
            appendix.getAllItems(res, lev + 1);
        }
    }

    @Override
    public void addPlainTextPos(int d) {
        super.addPlainTextPos(d);
        if (head != null) 
            head.addPlainTextPos(d);
        if (body != null) 
            body.addPlainTextPos(d);
        if (tail != null) 
            tail.addPlainTextPos(d);
        if (appendix != null) 
            appendix.addPlainTextPos(d);
    }

    @Override
    public void correct(com.pullenti.unitext.internal.uni.LocCorrTyp _typ, Object data) {
        if (head != null) 
            head.correct(_typ, data);
        if (body != null) 
            body.correct(_typ, data);
        if (tail != null) 
            tail.correct(_typ, data);
        if (appendix != null) 
            appendix.correct(_typ, data);
    }

    @Override
    public void setDefaultTextPos(com.pullenti.unisharp.Outargwrapper<Integer> cp, StringBuilder res) {
        beginChar = cp.value;
        if (head != null) {
            head.setDefaultTextPos(cp, res);
            head.parent = this;
        }
        if (body != null) {
            body.setDefaultTextPos(cp, res);
            body.parent = this;
        }
        if (tail != null) {
            tail.setDefaultTextPos(cp, res);
            tail.parent = this;
        }
        if (appendix != null) {
            appendix.setDefaultTextPos(cp, res);
            appendix.parent = this;
        }
        endChar = cp.value - 1;
    }

    @Override
    public UnitextItem optimize(boolean isContent, CreateDocumentParam pars) {
        if (head != null) {
            if (head.children.contains(head)) {
            }
            head = (UnitextContainer)com.pullenti.unisharp.Utils.cast(head.optimize(false, pars), UnitextContainer.class);
        }
        if (body != null) 
            body = body.optimize(true, pars);
        if (tail != null) 
            tail = (UnitextContainer)com.pullenti.unisharp.Utils.cast(tail.optimize(false, pars), UnitextContainer.class);
        if (appendix != null) 
            appendix = appendix.optimize(true, pars);
        return this;
    }

    public boolean _appendChild(UnitextItem it) {
        if (appendix != null) {
            UnitextContainer cnt = (UnitextContainer)com.pullenti.unisharp.Utils.cast(appendix, UnitextContainer.class);
            if (cnt == null) {
                cnt = UnitextContainer._new591(body.beginChar);
                cnt.children.add(body);
                appendix = cnt;
            }
            cnt.children.add(it);
            endChar = (body.endChar = it.endChar);
            return true;
        }
        if (tail != null) {
            tail.children.add(it);
            endChar = (tail.endChar = it.endChar);
            return true;
        }
        if (body != null) {
            UnitextContainer cnt = (UnitextContainer)com.pullenti.unisharp.Utils.cast(body, UnitextContainer.class);
            if (cnt == null) {
                cnt = UnitextContainer._new591(body.beginChar);
                cnt.children.add(body);
                body = cnt;
            }
            cnt.children.add(it);
            endChar = (body.endChar = it.endChar);
            return true;
        }
        if (head != null) {
            head.children.add(it);
            endChar = (head.endChar = it.endChar);
            return true;
        }
        return false;
    }

    public static UnitextDocblock _new38(String _arg1) {
        UnitextDocblock res = new UnitextDocblock();
        res.typname = _arg1;
        return res;
    }

    public static UnitextDocblock _new403(int _arg1, int _arg2) {
        UnitextDocblock res = new UnitextDocblock();
        res.beginChar = _arg1;
        res.endChar = _arg2;
        return res;
    }
    public UnitextDocblock() {
        super();
    }
}
