/*
 * 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 UnitextFootnote extends UnitextItem {

    /**
     * Содержимое сноски
     * 
     */
    public UnitextItem content;

    /**
     * Признак концевой сноски
     */
    public boolean isEndnote;

    /**
     * Специфическая комбинация для сноски
     */
    public String customMark = null;

    /**
     * Ссылка на структурный элемент с содержимым текста сноски. 
     * А текущея сноска - это просто метка в тексте на реальную сноску, которая расположена в другом месте.
     */
    public String docBlockId = null;

    @Override
    public String toString() {
        if (docBlockId != null) {
            String ttt = (((htmlTitle != null ? htmlTitle : ""))).replace('\r', ' ').replace('\n', ' ');
            if (ttt.length() > 100) 
                ttt = ttt.substring(0, 0 + 100) + "...";
            return ("<" + ((customMark != null ? customMark : "")) + "> -> " + ttt);
        }
        return ((isEndnote ? "Endnote" : "Footnote") + ": " + (content == null ? "" : content.toString()));
    }

    @Override
    public UnitextItem clone() {
        UnitextFootnote res = new UnitextFootnote();
        res._cloneFrom(this);
        if (content != null) 
            res.content = content.clone();
        res.isEndnote = isEndnote;
        res.customMark = customMark;
        res.docBlockId = docBlockId;
        return res;
    }

    @Override
    public UnitextItem optimize(boolean isContent, CreateDocumentParam pars) {
        if (content != null) 
            content = content.optimize(true, pars);
        if (content == null && htmlTitle == null) 
            return null;
        return this;
    }

    @Override
    public void getAllItems(java.util.ArrayList<UnitextItem> res, int lev) {
        if (res != null) 
            res.add(this);
        if (content != null) {
            content.parent = this;
            content.getAllItems(res, lev + 1);
        }
    }

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


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

    private static GetPlaintextParam m_FootnotesParam;

    private static GetPlaintextParam m_FootnotesParam1;

    @Override
    public void getPlaintext(StringBuilder res, GetPlaintextParam pars) {
        if (pars != null && pars.setPositions) 
            beginChar = res.length();
        if (pars == null) 
            pars = UnitextItem.m_DefParams;
        String txt = null;
        if (!com.pullenti.unisharp.Utils.isNullOrEmpty(pars.footnotesTemplate) && content != null) {
            StringBuilder tmp = new StringBuilder();
            content.getPlaintext(tmp, (pars.setPositions ? m_FootnotesParam : m_FootnotesParam1));
            if (tmp.length() > 0 && tmp.charAt(tmp.length() - 1) == '.') 
                tmp.setLength(tmp.length() - 1);
            txt = tmp.toString();
            if (((customMark != null && ((customMark.length() < txt.length())) && txt.startsWith(customMark))) && com.pullenti.unisharp.Utils.isWhitespace(txt.charAt(customMark.length()))) {
                tmp.delete(0, 0 + customMark.length() + 1);
                txt = tmp.toString();
            }
            if (txt.startsWith(" ")) 
                txt = txt.trim();
        }
        else if (!com.pullenti.unisharp.Utils.isNullOrEmpty(pars.footnotesTemplate) && htmlTitle != null && docBlockId != null) 
            txt = htmlTitle;
        if (!com.pullenti.unisharp.Utils.isNullOrEmpty(pars.footnotesTemplate) && txt != null) {
            int d = pars.footnotesTemplate.indexOf("%1");
            if (d < 0) {
                if (pars.setPositions) 
                    content.addPlainTextPos(res.length());
                res.append(pars.footnotesTemplate);
                if (pars.setPositions) 
                    content.endChar = (content.beginChar + ((res.length() - beginChar))) - 1;
            }
            else {
                if (pars.setPositions) 
                    content.addPlainTextPos(res.length() + ((d < 0 ? 0 : d)));
                if (txt.length() > 0) 
                    res.append(pars.footnotesTemplate.replace("%1", txt));
            }
        }
        if (pars != null && pars.setPositions) 
            endChar = (beginChar + ((res.length() - beginChar))) - 1;
    }

    @Override
    public void getHtml(StringBuilder res, GetHtmlParam par) {
        if (content == null && docBlockId == null) 
            return;
        if (par.hideEditionsAndComments && (content instanceof UnitextContainer)) {
            if (((UnitextContainer)com.pullenti.unisharp.Utils.cast(content, UnitextContainer.class)).typ == UnitextContainerType.EDITION || ((UnitextContainer)com.pullenti.unisharp.Utils.cast(content, UnitextContainer.class)).typ == UnitextContainerType.COMMENT) 
                return;
        }
        if (!par.callBefore(this, res)) 
            return;
        String _id = "";
        if (id != null) 
            _id = (" id=\"" + id + "\"");
        if (docBlockId != null) {
            boolean aaa = false;
            if (par != null) {
                for (UnitextItem p = parent; p != null; p = p.parent) {
                    UnitextDocblock db = (UnitextDocblock)com.pullenti.unisharp.Utils.cast(p.findById(docBlockId), UnitextDocblock.class);
                    if (db != null) {
                        if (!par.m_FootnotesDb.contains(db)) 
                            par.m_FootnotesDb.add(db);
                        aaa = true;
                        break;
                    }
                }
            }
            if (!aaa) {
            }
            res.append("<span title=\"");
            com.pullenti.util.MiscHelper.correctHtmlValue(res, (htmlTitle != null ? htmlTitle : ""), true, false, false);
            res.append("\" style=\"color:red\"><sup>").append(((customMark != null ? customMark : "*"))).append("</sup></span>");
            par.callAfter(this, res);
            return;
        }
        if (par != null && par.footnotes == GetHtmlParamFootnoteOutType.ENDOFUNIT) {
            if (isEndnote) 
                par.m_Endnotes.add(this);
            else 
                par.m_Footnotes.add(this);
            StringBuilder tmp = new StringBuilder();
            content.getPlaintext(tmp, m_FootnotesParam1);
            res.append("<span title=\"");
            com.pullenti.util.MiscHelper.correctHtmlValue(res, tmp.toString(), true, false, false);
            if (isEndnote) 
                res.append("\" style=\"color:red\"><sup>&lt;E").append(par.m_Endnotes.size()).append("&gt;</sup></span>");
            else if (customMark != null) 
                res.append("\" style=\"color:red\"><sup>&lt;").append(customMark).append("&gt;</sup></span>");
            else if ((content instanceof UnitextContainer) && ((UnitextContainer)com.pullenti.unisharp.Utils.cast(content, UnitextContainer.class)).typ == UnitextContainerType.EDITION) 
                res.append("\" style=\"color:lightgray;font-size:smaller\"><sup>&lt;").append(par.m_Footnotes.size()).append("&gt;</sup></span>");
            else 
                res.append("\" style=\"color:red\"><sup>&lt;").append(par.m_Footnotes.size()).append("&gt;</sup></span>");
            par.callAfter(this, res);
            return;
        }
        if (par != null && par.footnotes == GetHtmlParamFootnoteOutType.INBRACKETS) {
            res.append(" <i").append(_id).append("><sub title=\"Сноска ").append(((customMark != null ? customMark : ""))).append("\">(");
            content.getHtml(res, par);
            res.append(")</sub></i>");
        }
        else {
            StringBuilder tmp = new StringBuilder();
            content.getPlaintext(tmp, m_FootnotesParam1);
            String txt = tmp.toString().trim();
            res.append("<span title=\"");
            com.pullenti.util.MiscHelper.correctHtmlValue(res, txt, true, false, false);
            res.append("\" style=\"color:red\"").append(_id).append("><sup>").append((isEndnote ? "**" : "*")).append(")</sup></span>");
        }
        par.callAfter(this, res);
    }

    @Override
    public void getXml(com.pullenti.unisharp.XmlWriterWrapper xml) throws javax.xml.stream.XMLStreamException {
        xml.wr.writeStartElement("footnote");
        this._writeXmlAttrs(xml);
        if (isEndnote) 
            xml.wr.writeAttribute("endnote", "true");
        if (customMark != null) 
            xml.wr.writeAttribute("mark", com.pullenti.util.MiscHelper.correctXmlValue(customMark));
        if (docBlockId != null) 
            xml.wr.writeAttribute("docblockid", docBlockId);
        if (content != null) 
            content.getXml(xml);
        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), "endnote")) 
                    isEndnote = com.pullenti.unisharp.Utils.stringsEq(a.getNodeValue(), "true");
                else if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(a), "mark")) 
                    customMark = a.getNodeValue();
                else if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(a), "docblockid")) 
                    docBlockId = a.getNodeValue();
            }
        }
        for (org.w3c.dom.Node x : (new com.pullenti.unisharp.XmlNodeListWrapper(xml.getChildNodes())).arr) {
            content = com.pullenti.unitext.internal.uni.UnitextHelper.createItem(x, this);
            break;
        }
    }

    @Override
    public void addPlainTextPos(int d) {
        super.addPlainTextPos(d);
        if (content != null) 
            content.addPlainTextPos(d);
    }

    @Override
    public void correct(com.pullenti.unitext.internal.uni.LocCorrTyp typ, Object data) {
        if (content != null) 
            content.correct(typ, data);
    }

    @Override
    public boolean replaceChild(UnitextItem old, UnitextItem ne) {
        if (content != old) 
            return false;
        content = ne;
        ne.parent = this;
        return true;
    }

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

    public static UnitextFootnote _new100(String _arg1) {
        UnitextFootnote res = new UnitextFootnote();
        res.customMark = _arg1;
        return res;
    }

    public static UnitextFootnote _new285(UnitextItem _arg1, boolean _arg2) {
        UnitextFootnote res = new UnitextFootnote();
        res.content = _arg1;
        res.isEndnote = _arg2;
        return res;
    }

    public static UnitextFootnote _new330(UnitextItem _arg1, String _arg2) {
        UnitextFootnote res = new UnitextFootnote();
        res.content = _arg1;
        res.customMark = _arg2;
        return res;
    }

    public static UnitextFootnote _new514(boolean _arg1, UnitextItem _arg2) {
        UnitextFootnote res = new UnitextFootnote();
        res.isEndnote = _arg1;
        res.content = _arg2;
        return res;
    }

    public UnitextFootnote() {
        super();
    }
    
    static {
        m_FootnotesParam = GetPlaintextParam._new607(true, " ", " ", " ", " ", " ");
        m_FootnotesParam1 = GetPlaintextParam._new607(false, " ", " ", " ", " ", " ");
    }
}
