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

    @Override
    public UnitextItem optimize(boolean isContent, CreateDocumentParam pars) {
        for (UnitextListitem it : items) {
            it.parent = this;
            it.optimize(false, pars);
            if ((it.content instanceof UnitextList) && it.sublist == null && ((UnitextList)com.pullenti.unisharp.Utils.cast(it.content, UnitextList.class)).level == (level + 1)) {
                it.sublist = (UnitextList)com.pullenti.unisharp.Utils.cast(it.content, UnitextList.class);
                it.content = null;
                continue;
            }
            if (it.sublist != null) 
                continue;
            UnitextContainer cnt = (UnitextContainer)com.pullenti.unisharp.Utils.cast(it.content, UnitextContainer.class);
            if (cnt != null && (cnt.children.get(cnt.children.size() - 1) instanceof UnitextList) && ((UnitextList)com.pullenti.unisharp.Utils.cast(cnt.children.get(cnt.children.size() - 1), UnitextList.class)).level == (level + 1)) {
                it.sublist = (UnitextList)com.pullenti.unisharp.Utils.cast(cnt.children.get(cnt.children.size() - 1), UnitextList.class);
                cnt.children.remove(cnt.children.size() - 1);
                it.content = it.content.optimize(true, pars);
            }
        }
        if (items.size() == 0) 
            return null;
        if (unorderPrefix == null) {
            String pre = null;
            boolean ok = true;
            for (UnitextListitem it : items) {
                if (it.prefix != null) {
                    if (pre == null) 
                        pre = com.pullenti.unitext.internal.uni.UnitextHelper.getPlaintext(it.prefix, null);
                    else if (com.pullenti.unisharp.Utils.stringsNe(pre, com.pullenti.unitext.internal.uni.UnitextHelper.getPlaintext(it.prefix, null))) {
                        ok = false;
                        break;
                    }
                }
            }
            if ((ok && pre != null && pre.length() == 1) && !Character.isLetterOrDigit(pre.charAt(0))) {
                unorderPrefix = pre;
                for (UnitextListitem it : items) {
                    it.prefix = null;
                }
            }
        }
        else 
            for (UnitextListitem it : items) {
                it.prefix = null;
            }
        if (items.size() == 1 && (!(parent instanceof UnitextListitem))) {
            if (this._isAllListItemsSingle()) {
                UnitextContainer ccc = new UnitextContainer();
                ccc.id = id;
                ccc.m_StyledFrag = m_StyledFrag;
                for (UnitextList li = this; li != null; li = li.items.get(0).sublist) {
                    if (ccc.children.size() > 0) 
                        ccc.children.add(new UnitextNewline());
                    UnitextListitem it = li.items.get(0);
                    if (ccc.id == null) 
                        ccc.id = it.id;
                    if (it.prefix != null && it.content != null) {
                        ccc.children.add(it.prefix);
                        ccc.children.add(UnitextPlaintext._new613(" ", it.prefix.endChar, it.prefix.endChar));
                        ccc.children.add(it.content);
                    }
                    else if (it.prefix != null) 
                        ccc.children.add(it.prefix);
                    else if (it.content != null) 
                        ccc.children.add(it.content);
                }
                if (parent instanceof UnitextContainer) {
                    UnitextContainer par = (UnitextContainer)com.pullenti.unisharp.Utils.cast(parent, UnitextContainer.class);
                    int i = par.getChildIndexOf(this);
                    if (i >= 0) {
                        if (((i + 1) < par.children.size()) && (((par.children.get(i + 1) instanceof UnitextNewline) || (par.children.get(i + 1) instanceof UnitextPagebreak)))) {
                        }
                        else 
                            ccc.children.add(new UnitextNewline());
                    }
                }
                for (UnitextItem ch : ccc.children) {
                    ch.parent = ccc;
                }
                return ccc.optimize(true, pars);
            }
        }
        if (pars != null && pars.correctParams.transformListsToContainers) {
            UnitextContainer cnt = new UnitextContainer();
            cnt.id = id;
            cnt.m_StyledFrag = m_StyledFrag;
            for (UnitextListitem it : items) {
                if (it.prefix != null) {
                    cnt.children.add(it.prefix);
                    it.prefix.parent = cnt;
                }
                if (it.content != null) {
                    cnt.children.add(it.content);
                    it.content = cnt;
                }
                if (it.content != null || it.prefix != null) {
                    UnitextNewline nl = new UnitextNewline();
                    cnt.children.add(nl);
                    nl.parent = cnt;
                }
                if (it.sublist != null) {
                    UnitextItem cnt1 = it.sublist.optimize(false, pars);
                    if (cnt1 != null) {
                        cnt.children.add(cnt1);
                        cnt1.parent = cnt;
                    }
                }
            }
            return cnt.optimize(false, pars);
        }
        return this;
    }

    private boolean _isAllListItemsSingle() {
        if (items.size() > 1) 
            return false;
        UnitextListitem it = items.get(0);
        if (it.sublist == null) 
            return true;
        return it.sublist._isAllListItemsSingle();
    }

    /**
     * Элементы списка
     * 
     */
    public java.util.ArrayList<UnitextListitem> items = new java.util.ArrayList<UnitextListitem>();

    /**
     * Для ненумерованных списков это общий префикс
     */
    public String unorderPrefix;

    /**
     * Уровень вложенности (0 - первый)
     */
    public int level = 0;

    @Override
    public String toString() {
        return ((unorderPrefix == null ? "" : ("Unordered (" + unorderPrefix + ")")) + "List (" + items.size() + " items, " + level + " level)");
    }

    @Override
    public UnitextItem clone() {
        UnitextList res = new UnitextList();
        res._cloneFrom(this);
        for (UnitextListitem it : items) {
            res.items.add((UnitextListitem)com.pullenti.unisharp.Utils.cast(it.clone(), UnitextListitem.class));
        }
        res.unorderPrefix = unorderPrefix;
        res.level = level;
        return res;
    }

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


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

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


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


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

    @Override
    public void getPlaintext(StringBuilder res, GetPlaintextParam pars) {
        if (pars != null && pars.setPositions) 
            beginChar = res.length();
        for (UnitextListitem it : items) {
            for (int ii = 0; ii < level; ii++) {
                res.append('\t');
            }
            if (it == items.get(0) && pars != null && pars.setPositions) 
                beginChar = res.length();
            if (unorderPrefix != null) 
                res.append(unorderPrefix).append(" ");
            it.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 ((parent instanceof UnitextContainer) && ((UnitextContainer)com.pullenti.unisharp.Utils.cast(parent, UnitextContainer.class)).typ == UnitextContainerType.HEAD && items.size() == 1) {
            UnitextListitem it = items.get(0);
            if (it.prefix != null) {
                it.prefix.getHtml(res, par);
                res.append("&nbsp;");
            }
            if (it.content != null) 
                it.content.getHtml(res, par);
            if (it.sublist != null) 
                it.sublist.getHtml(res, par);
        }
        else {
            res.append("\r\n<UL");
            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(">");
            for (UnitextListitem it : items) {
                it.getHtml(res, par);
            }
            res.append("\r\n</UL>");
        }
        par.callAfter(this, res);
    }

    @Override
    public void getXml(com.pullenti.unisharp.XmlWriterWrapper xml) throws javax.xml.stream.XMLStreamException {
        xml.wr.writeStartElement("list");
        this._writeXmlAttrs(xml);
        if (unorderPrefix != null) 
            xml.wr.writeAttribute("pref", com.pullenti.util.MiscHelper.correctXmlValue(unorderPrefix));
        if (level > 0) 
            xml.wr.writeAttribute("level", ((Integer)level).toString());
        for (UnitextListitem it : items) {
            it.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), "pref")) 
                    unorderPrefix = a.getNodeValue();
                else if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(a), "level")) {
                    int n;
                    com.pullenti.unisharp.Outargwrapper<Integer> wrapn614 = new com.pullenti.unisharp.Outargwrapper<Integer>();
                    boolean inoutres615 = com.pullenti.unisharp.Utils.parseInteger(a.getNodeValue(), 0, null, wrapn614);
                    n = (wrapn614.value != null ? wrapn614.value : 0);
                    if (inoutres615) 
                        level = n;
                }
            }
        }
        for (org.w3c.dom.Node x : (new com.pullenti.unisharp.XmlNodeListWrapper(xml.getChildNodes())).arr) {
            UnitextItem it = com.pullenti.unitext.internal.uni.UnitextHelper.createItem(x, this);
            if (it instanceof UnitextListitem) 
                items.add((UnitextListitem)com.pullenti.unisharp.Utils.cast(it, UnitextListitem.class));
        }
    }

    @Override
    public void addPlainTextPos(int d) {
        super.addPlainTextPos(d);
        for (UnitextListitem it : items) {
            it.addPlainTextPos(d);
        }
    }

    @Override
    public void correct(com.pullenti.unitext.internal.uni.LocCorrTyp typ, Object data) {
        for (UnitextListitem it : items) {
            it.correct(typ, data);
        }
    }

    @Override
    public void setDefaultTextPos(com.pullenti.unisharp.Outargwrapper<Integer> cp, StringBuilder res) {
        beginChar = cp.value;
        for (UnitextListitem it : items) {
            it.setDefaultTextPos(cp, res);
            it.parent = this;
        }
        endChar = cp.value - 1;
    }

    /**
     * Удалить список из дерева, заменив его на UnitextContainer, развернув его элементы
     * @param newlineLen размер в символах перехода на новую строку
     */
    public boolean transformListToContainer(int newlineLen) {
        if (parent == null) 
            return false;
        if (parent instanceof UnitextContainer) {
            UnitextContainer cnt0 = (UnitextContainer)com.pullenti.unisharp.Utils.cast(parent, UnitextContainer.class);
            int i0 = cnt0.getChildIndexOf(this);
            if (i0 < 0) 
                return false;
        }
        else if (parent instanceof UnitextListitem) 
            return false;
        UnitextContainer cnt = new UnitextContainer();
        cnt.beginChar = beginChar;
        cnt.endChar = endChar;
        cnt.parent = parent;
        for (UnitextListitem it : items) {
            if (it.prefix != null) {
                cnt.children.add(it.prefix);
                it.prefix.parent = cnt;
            }
            if (it.content != null) {
                cnt.children.add(it.content);
                it.content = cnt;
            }
            if (it.content != null || it.prefix != null) {
                UnitextNewline nl = new UnitextNewline();
                nl.parent = cnt;
                nl.beginChar = cnt.children.get(cnt.children.size() - 1).endChar + 1;
                nl.endChar = (nl.beginChar + newlineLen) - 1;
                cnt.children.add(nl);
            }
            if (it.sublist != null) {
                cnt.children.add(it.sublist);
                it.sublist.parent = cnt;
            }
        }
        if (parent instanceof UnitextContainer) {
            UnitextContainer cnt0 = (UnitextContainer)com.pullenti.unisharp.Utils.cast(parent, UnitextContainer.class);
            int i0 = cnt0.getChildIndexOf(this);
            cnt0.children.remove(i0);
            com.pullenti.unisharp.Utils.insertToArrayList(cnt0.children, cnt.children, i0);
            for (UnitextItem it : cnt.children) {
                it.parent = cnt0;
            }
        }
        else if (parent instanceof UnitextTablecell) 
            ((UnitextTablecell)com.pullenti.unisharp.Utils.cast(parent, UnitextTablecell.class)).content = cnt;
        else if (parent instanceof UnitextDocument) 
            ((UnitextDocument)com.pullenti.unisharp.Utils.cast(parent, UnitextDocument.class)).content = cnt;
        return true;
    }

    public static UnitextList _new63(int _arg1) {
        UnitextList res = new UnitextList();
        res.level = _arg1;
        return res;
    }

    public static UnitextList _new374(int _arg1, Object _arg2) {
        UnitextList res = new UnitextList();
        res.level = _arg1;
        res.tag = _arg2;
        return res;
    }

    public static UnitextList _new375(int _arg1, Object _arg2, String _arg3) {
        UnitextList res = new UnitextList();
        res.level = _arg1;
        res.tag = _arg2;
        res.pageSectionId = _arg3;
        return res;
    }
    public UnitextList() {
        super();
    }
}
