/*
 * 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.internal.html;

public class HtmlSection {

    public java.util.ArrayList<HtmlNode> nodes = new java.util.ArrayList<HtmlNode>();

    public HtmlNode title;

    public int level;

    public java.util.ArrayList<HtmlSection> children = new java.util.ArrayList<HtmlSection>();

    @Override
    public String toString() {
        StringBuilder tmp = new StringBuilder();
        tmp.append("H").append(level).append(": ");
        if (children.size() > 0) 
            tmp.append("Childs=").append(children.size()).append(" ");
        if (nodes.size() > 0) 
            tmp.append("Nodes=").append(nodes.size()).append(" ");
        if (title != null) 
            tmp.append(title);
        return tmp.toString();
    }

    public void _generate(com.pullenti.unitext.UnitextDocblock blk, int lev, UnitextHtmlGen hg) {
        com.pullenti.unitext.internal.uni.UnitextGen gen;
        if (title != null) {
            blk.head = new com.pullenti.unitext.UnitextContainer();
            gen = new com.pullenti.unitext.internal.uni.UnitextGen();
            hg.getUniText(title, gen, null, 0);
            com.pullenti.unitext.UnitextItem hh = gen.finish(true, null);
            if (hh instanceof com.pullenti.unitext.UnitextContainer) 
                blk.head = (com.pullenti.unitext.UnitextContainer)com.pullenti.unisharp.Utils.cast(hh, com.pullenti.unitext.UnitextContainer.class);
            else if (hh != null) 
                blk.head.children.add(hh);
            blk.head.children.add(new com.pullenti.unitext.UnitextNewline());
            blk.head.typ = com.pullenti.unitext.UnitextContainerType.HEAD;
        }
        blk.body = new com.pullenti.unitext.UnitextContainer();
        if (nodes.size() > 0) {
            gen = new com.pullenti.unitext.internal.uni.UnitextGen();
            for (HtmlNode ch : nodes) {
                hg.getUniText(ch, gen, null, 0);
            }
            com.pullenti.unitext.UnitextItem hh = gen.finish(true, null);
            if (hh != null && children.size() > 0) {
                com.pullenti.unitext.UnitextDocblock pr = com.pullenti.unitext.UnitextDocblock._new38("Preamble");
                pr.body = hh;
                ((com.pullenti.unitext.UnitextContainer)com.pullenti.unisharp.Utils.cast(blk.body, com.pullenti.unitext.UnitextContainer.class)).children.add(pr);
            }
            else if (hh instanceof com.pullenti.unitext.UnitextContainer) 
                blk.body = (com.pullenti.unitext.UnitextContainer)com.pullenti.unisharp.Utils.cast(hh, com.pullenti.unitext.UnitextContainer.class);
            else if (hh != null) 
                ((com.pullenti.unitext.UnitextContainer)com.pullenti.unisharp.Utils.cast(blk.body, com.pullenti.unitext.UnitextContainer.class)).children.add(hh);
        }
        for (HtmlSection ch : children) {
            com.pullenti.unitext.UnitextDocblock chblk = new com.pullenti.unitext.UnitextDocblock();
            ((com.pullenti.unitext.UnitextContainer)com.pullenti.unisharp.Utils.cast(blk.body, com.pullenti.unitext.UnitextContainer.class)).children.add(chblk);
            if (lev == 0) 
                chblk.typname = "Section";
            else if (lev == 1) 
                chblk.typname = "Subsection";
            else 
                chblk.typname = "Chapter";
            ch._generate(chblk, lev + 1, hg);
        }
    }

    public static HtmlSectionItem create(HtmlNode n) {
        HtmlSectionItem ttt = new HtmlSectionItem();
        ttt.stack.add(new HtmlSection());
        _create(n, ttt);
        if (ttt.stack.size() > 1) 
            for(int jjj = 0 + ttt.stack.size() - 1 - 1, mmm = 0; jjj >= mmm; jjj--) ttt.stack.remove(jjj);
        if (ttt.stack.get(0).children.size() == 0) 
            return null;
        if (ttt.stack.get(0).children.size() == 1) {
            if (ttt.stack.get(0).nodes.size() > 0) 
                com.pullenti.unisharp.Utils.addToArrayList(ttt.head, ttt.stack.get(0).nodes);
            com.pullenti.unisharp.Utils.putArrayValue(ttt.stack, 0, ttt.stack.get(0).children.get(0));
        }
        if (ttt.stack.get(0).children.size() < 2) 
            return null;
        return ttt;
    }

    private static void _create(HtmlNode n, HtmlSectionItem st) {
        int curLev = st.stack.size();
        for (HtmlNode ch : n.children) {
            if (!com.pullenti.unisharp.Utils.isNullOrEmpty(ch.tagName) && ch.tagName.charAt(0) == 'H') {
                int le = 0;
                com.pullenti.unisharp.Outargwrapper<Integer> wraple40 = new com.pullenti.unisharp.Outargwrapper<Integer>();
                boolean inoutres41 = com.pullenti.unisharp.Utils.parseInteger(ch.tagName.substring(1), 0, null, wraple40);
                le = (wraple40.value != null ? wraple40.value : 0);
                if (inoutres41) {
                    if (le > 0) {
                        if (le < st.stack.size()) 
                            for(int jjj = 0 + st.stack.size() - le - 1, mmm = 0; jjj >= mmm; jjj--) st.stack.remove(jjj);
                        while (le > st.stack.size()) {
                            if (st.stack.get(0).children.size() == 0) 
                                break;
                            HtmlSection al = st.stack.get(0).children.get(st.stack.get(0).children.size() - 1);
                            st.stack.add(0, al);
                        }
                        if (le == st.stack.size()) {
                            HtmlSection s = _new39(le, ch);
                            st.stack.get(0).children.add(s);
                            st.stack.add(0, s);
                            st.hasBody = true;
                            for (HtmlNode p = n; p != null; p = p.parent) {
                                p.misc = p;
                            }
                            continue;
                        }
                        else {
                        }
                    }
                }
            }
            if (ch.text != null && ch.text.startsWith("Автор:")) {
            }
            _create(ch, st);
            if (ch.misc != null) 
                continue;
            if (n.misc != null) {
                if (st.stack.size() > 1) 
                    st.stack.get(0).nodes.add(ch);
                else if (st.hasBody) 
                    st.tail.add(ch);
                else 
                    st.head.add(ch);
            }
        }
        if (curLev < st.stack.size()) 
            for(int jjj = 0 + st.stack.size() - curLev - 1, mmm = 0; jjj >= mmm; jjj--) st.stack.remove(jjj);
    }

    public static HtmlSection _new39(int _arg1, HtmlNode _arg2) {
        HtmlSection res = new HtmlSection();
        res.level = _arg1;
        res.title = _arg2;
        return res;
    }
    public HtmlSection() {
    }
}
