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

public class StructExpressBlock {

    public java.util.ArrayList<StructExpressLine> lines = new java.util.ArrayList<StructExpressLine>();

    public boolean isApp;

    public boolean hasStructure;

    public int getBeginChar() {
        if (lines.size() > 0) 
            return lines.get(0).beginChar;
        return 0;
    }


    public int getEndChar() {
        if (lines.size() > 0) 
            return lines.get(lines.size() - 1).endChar;
        return 0;
    }


    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        res.append("[").append(this.getBeginChar()).append("..").append(this.getEndChar()).append("]");
        if (isApp) 
            res.append(" Appendix");
        if (hasStructure) 
            res.append(" Structured");
        return res.toString();
    }

    public static java.util.ArrayList<StructExpressBlock> parse(String text) {
        java.util.ArrayList<StructExpressLine> _lines = new java.util.ArrayList<StructExpressLine>();
        for (int i = 0; i < text.length(); ) {
            com.pullenti.unisharp.Outargwrapper<Integer> wrapi672 = new com.pullenti.unisharp.Outargwrapper<Integer>(i);
            StructExpressLine li = StructExpressLine.tryParse(text, wrapi672);
            i = (wrapi672.value != null ? wrapi672.value : 0);
            if (li == null) 
                break;
            _lines.add(li);
        }
        if (_lines.size() == 0) 
            return null;
        for (int i = 0; i < _lines.size(); i++) {
            if (_lines.get(i).isStartOfApp && _lines.get(i + 1).isStartOfApp) {
                for (; i < _lines.size(); i++) {
                    if (_lines.get(i).isStartOfApp) 
                        _lines.get(i).isStartOfApp = false;
                    else 
                        break;
                }
            }
        }
        java.util.ArrayList<StructExpressBlock> res = new java.util.ArrayList<StructExpressBlock>();
        StructExpressBlock cur = new StructExpressBlock();
        res.add(cur);
        for (int i = 0; i < _lines.size(); i++) {
            StructExpressLine li = _lines.get(i);
            if (li.isStartOfApp) {
                if (cur.lines.size() > 0) {
                    cur = new StructExpressBlock();
                    res.add(cur);
                }
                cur.isApp = true;
            }
            cur.lines.add(li);
        }
        for (StructExpressBlock b : res) {
            b._analyze();
        }
        return res;
    }

    private void _analyze() {
        int clauses = 0;
        int numbered = 0;
        int illegals = 0;
        for (StructExpressLine li : lines) {
            if (com.pullenti.unisharp.Utils.stringsEq(li.keyword, "СТАТЬЯ") || com.pullenti.unisharp.Utils.stringsEq(li.keyword, "СТАТТЯ") || com.pullenti.unisharp.Utils.stringsEq(li.keyword, "ГЛАВА")) 
                clauses++;
            if (li.number != null) 
                numbered++;
            if (li.illegalChars >= 2) 
                illegals++;
        }
        if (clauses > 1) 
            hasStructure = true;
        else if (clauses == 1 && numbered > 3) 
            hasStructure = true;
        else if (numbered > 5 && numbered > (lines.size() / 3) && (illegals < 2)) 
            hasStructure = true;
        else if (numbered > 20 && numbered > (lines.size() / 10)) 
            hasStructure = true;
    }
    public StructExpressBlock() {
    }
}
