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

    /**
     * Префикс (может быть null)
     */
    public UnitextItem prefix;

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

    /**
     * Вложенный список
     */
    public UnitextList sublist;

    private static GetPlaintextParam m_Pars;

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        if (prefix != null) 
            res.append(prefix.toString()).append(": ");
        if (content != null) 
            res.append(content.toString());
        if (sublist != null) 
            res.append(" + ").append(sublist.toString());
        return res.toString();
    }

    @Override
    public UnitextItem clone() {
        UnitextListitem res = new UnitextListitem();
        res._cloneFrom(this);
        if (prefix != null) 
            res.prefix = prefix.clone();
        if (content != null) 
            res.content = content.clone();
        if (sublist != null) 
            res.sublist = (UnitextList)com.pullenti.unisharp.Utils.cast(sublist.clone(), UnitextList.class);
        return res;
    }

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


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


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

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

    @Override
    public void getPlaintext(StringBuilder res, GetPlaintextParam pars) {
        if (pars != null && pars.setPositions) 
            beginChar = res.length();
        if (pars == null) 
            pars = UnitextItem.m_DefParams;
        if (prefix != null) {
            prefix.getPlaintext(res, pars);
            res.append(' ');
        }
        if (content != null) 
            content.getPlaintext(res, pars);
        res.append((pars.newLine != null ? pars.newLine : ""));
        if (sublist != null) 
            sublist.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;
        res.append("\r\n<LI");
        if (id != null) {
            res.append(" id=\"").append(id).append("\"");
            if (par != null && par.styles.containsKey(id)) 
                res.append(" style=\"").append(par.styles.get(id)).append("\"");
        }
        res.append(">");
        if (prefix != null) {
            prefix.getHtml(res, par);
            res.append(' ');
        }
        if (content != null) 
            content.getHtml(res, par);
        if (sublist != null) 
            sublist.getHtml(res, par);
        res.append("</LI>");
        par.callAfter(this, res);
    }

    @Override
    public void getXml(com.pullenti.unisharp.XmlWriterWrapper xml) throws javax.xml.stream.XMLStreamException {
        xml.wr.writeStartElement("listitem");
        this._writeXmlAttrs(xml);
        if (prefix != null) {
            xml.wr.writeStartElement("prefix");
            prefix.getXml(xml);
            xml.wr.writeEndElement();
        }
        if (content != null) 
            content.getXml(xml);
        if (sublist != null) 
            sublist.getXml(xml);
        xml.wr.writeEndElement();
    }

    @Override
    public void fromXml(org.w3c.dom.Node xml) throws NumberFormatException {
        super.fromXml(xml);
        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), "prefix")) {
                for (org.w3c.dom.Node xx : (new com.pullenti.unisharp.XmlNodeListWrapper(x.getChildNodes())).arr) {
                    prefix = com.pullenti.unitext.internal.uni.UnitextHelper.createItem(xx, this);
                    break;
                }
            }
            else {
                UnitextItem it = com.pullenti.unitext.internal.uni.UnitextHelper.createItem(x, this);
                if (it instanceof UnitextList) 
                    sublist = (UnitextList)com.pullenti.unisharp.Utils.cast(it, UnitextList.class);
                else if (it != null) 
                    content = it;
            }
        }
    }

    @Override
    public UnitextItem optimize(boolean isContent, CreateDocumentParam pars) {
        if (prefix != null) 
            prefix = prefix.optimize(true, pars);
        if (content != null) 
            content = content.optimize(true, pars);
        if (content != null && content.isWhitespaces()) 
            content = null;
        if (sublist != null) {
            if (sublist.items.size() == 1) {
            }
            sublist.parent = this;
            UnitextItem oo = sublist.optimize(false, pars);
            sublist = (UnitextList)com.pullenti.unisharp.Utils.cast(oo, UnitextList.class);
            if (sublist == null && oo != null) {
                if (content == null) 
                    content = oo;
                else {
                    UnitextContainer cnt = new UnitextContainer();
                    cnt.children.add(content);
                    cnt.children.add(new UnitextNewline());
                    cnt.children.add(oo);
                    content = cnt.optimize(isContent, pars);
                }
            }
        }
        return this;
    }

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

    @Override
    public void correct(com.pullenti.unitext.internal.uni.LocCorrTyp typ, Object data) {
        if (prefix != null) 
            prefix.correct(typ, data);
        if (content != null) 
            content.correct(typ, data);
        if (sublist != null) 
            sublist.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;
        if (prefix != null) {
            prefix.setDefaultTextPos(cp, res);
            prefix.parent = this;
        }
        if (content != null) {
            content.setDefaultTextPos(cp, res);
            content.parent = this;
        }
        cp.value++;
        if (res != null) 
            res.append('\n');
        if (sublist != null) {
            sublist.setDefaultTextPos(cp, res);
            sublist.parent = this;
        }
        endChar = cp.value - 1;
    }

    public static UnitextListitem _new372(UnitextItem _arg1) {
        UnitextListitem res = new UnitextListitem();
        res.content = _arg1;
        return res;
    }

    public UnitextListitem() {
        super();
    }
    
    static {
        m_Pars = new GetPlaintextParam();
    }
}
