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

public class CorrLine {

    public int length;

    public boolean isPureText;

    public int firstInd;

    public int lastInd;

    public String getText() {
        return m_Text;
    }

    public String setText(String value) {
        m_Text = com.pullenti.unisharp.Utils.trimEnd(value);
        length = value.length();
        m_CanBeEmptyLineOfTable = -1;
        m_CanHasHorLineOfTable = -1;
        return value;
    }


    private String m_Text;

    public int newLines = 0;

    public boolean pageBreakAfter = false;

    public boolean merge = false;

    public boolean cantFollowAny;

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        if (merge) 
            res.append("<-Merge ");
        res.append("[").append(length).append((isPureText ? " txt" : "")).append("]");
        if (pageBreakAfter) 
            res.append(" PageBreak");
        else 
            res.append(" ").append(newLines).append("nls");
        if (getText().length() < 100) 
            res.append(" '").append(this.getText()).append("'");
        else 
            res.append(" '").append(this.getText().substring(0, 0 + 100)).append("...");
        return res.toString();
    }

    public static java.util.ArrayList<CorrLine> parseList(com.pullenti.unitext.UnitextContainer cnt) {
        java.util.ArrayList<CorrLine> res = new java.util.ArrayList<CorrLine>();
        CorrLine line = null;
        StringBuilder tmp = new StringBuilder();
        for (int i = 0; i < cnt.children.size(); i++) {
            com.pullenti.unitext.UnitextItem ch = cnt.children.get(i);
            if (ch instanceof com.pullenti.unitext.UnitextPagebreak) {
                if (line != null) 
                    line.pageBreakAfter = true;
                continue;
            }
            if (ch instanceof com.pullenti.unitext.UnitextNewline) {
                if (line != null) {
                    if (ch.tag != null) 
                        line.cantFollowAny = true;
                    line.newLines += ((com.pullenti.unitext.UnitextNewline)com.pullenti.unisharp.Utils.cast(ch, com.pullenti.unitext.UnitextNewline.class)).count;
                    continue;
                }
            }
            if (line == null || line.newLines > 0 || line.pageBreakAfter) {
                if (line != null) {
                    line.setText(tmp.toString());
                    tmp.setLength(0);
                }
                line = null;
            }
            if (line == null) {
                line = _new337(true);
                tmp.setLength(0);
                res.add(line);
                line.firstInd = i;
            }
            line.lastInd = i;
            if (ch instanceof com.pullenti.unitext.UnitextPlaintext) {
                tmp.append(((com.pullenti.unitext.UnitextPlaintext)com.pullenti.unisharp.Utils.cast(ch, com.pullenti.unitext.UnitextPlaintext.class)).text);
                continue;
            }
            if (ch instanceof com.pullenti.unitext.UnitextFootnote) {
            }
            else 
                line.isPureText = false;
        }
        if (line != null) 
            line.setText(tmp.toString());
        return res;
    }

    public static boolean isHiphen(char ch) {
        if ((ch == '-' || ch == '–' || ch == '¬') || ch == '-') 
            return true;
        if (ch == ((char)0x00AD)) 
            return true;
        if ((ch == '-' || ch == '—' || ch == '–') || ch == '−' || ch == '-') 
            return true;
        return false;
    }

    public static boolean isTableChar(char ch) {
        if (isHiphen(ch)) 
            return true;
        if (ch == '|' || ch == '_') 
            return true;
        int cod = (int)ch;
        if (cod >= 0x2500 && cod <= 0x2595) 
            return true;
        return false;
    }

    public boolean canFollowed(CorrLine li) {
        if (!li.isPureText || !isPureText) 
            return false;
        if (cantFollowAny) 
            return false;
        if (newLines > 2) 
            return false;
        if (com.pullenti.unisharp.Utils.isNullOrEmpty(this.getText()) || com.pullenti.unisharp.Utils.isNullOrEmpty(li.getText())) 
            return false;
        char lastCh = this.getText().charAt(this.getText().length() - 1);
        char firstCh = (char)0;
        int wsBefore = 0;
        for (char ch : li.getText().toCharArray()) {
            if (!com.pullenti.unisharp.Utils.isWhitespace(ch)) {
                firstCh = ch;
                break;
            }
            else 
                wsBefore++;
        }
        if (((int)firstCh) == 0) 
            return false;
        if (wsBefore > 10) {
            int wsb = 0;
            for (char ch : getText().toCharArray()) {
                if (!com.pullenti.unisharp.Utils.isWhitespace(ch)) 
                    break;
                else 
                    wsb++;
            }
            if (wsb < ((int)((0.8 * ((double)wsBefore))))) 
                return false;
        }
        if (lastCh == '.' || lastCh == ';' || lastCh == ':') {
            if (newLines > 1 || pageBreakAfter) 
                return false;
            if (!Character.isLetter(firstCh)) 
                return false;
            if (!Character.isLowerCase(firstCh)) 
                return false;
            if (lastCh != '.') 
                return false;
            return true;
        }
        if (Character.isLetter(firstCh)) {
            if (Character.isLowerCase(firstCh)) 
                return true;
        }
        if (newLines > 1 || pageBreakAfter) 
            return false;
        if (isHiphen(lastCh) || lastCh == ',') {
            if (!Character.isLetterOrDigit(firstCh)) 
                return false;
            return true;
        }
        if (Character.isLetter(lastCh) && Character.isLowerCase(lastCh)) {
            int wsBefore0 = 0;
            for (char ch : getText().toCharArray()) {
                if (!com.pullenti.unisharp.Utils.isWhitespace(ch)) 
                    break;
                else 
                    wsBefore0++;
            }
            if (wsBefore == wsBefore0) {
                if (li.getText().charAt(li.getText().length() - 1) == '.') {
                    if (Character.isLowerCase(firstCh) && Character.isLowerCase(firstCh)) 
                        return true;
                }
            }
        }
        return false;
    }

    private int m_CanBeEmptyLineOfTable = -1;

    public boolean canBeEmptyLineOfTable() {
        if (!isPureText) 
            return false;
        if (m_CanBeEmptyLineOfTable >= 0) 
            return m_CanBeEmptyLineOfTable > 0;
        int cou = 0;
        for (char ch : getText().toCharArray()) {
            if (isTableChar(ch)) 
                cou++;
            else if (!com.pullenti.unisharp.Utils.isWhitespace(ch)) {
                m_CanBeEmptyLineOfTable = 0;
                return false;
            }
        }
        m_CanBeEmptyLineOfTable = (cou > 1 ? 1 : 0);
        return m_CanBeEmptyLineOfTable > 0;
    }


    private int m_CanHasHorLineOfTable = -1;

    public boolean canHasHorLineOfTable() {
        if (!isPureText) 
            return false;
        if (m_CanHasHorLineOfTable >= 0) 
            return m_CanHasHorLineOfTable > 0;
        for (int i = 0; i < (getText().length() - 2); i++) {
            if (isTableChar(this.getText().charAt(i)) && this.getText().charAt(i + 1) == this.getText().charAt(i) && this.getText().charAt(i + 2) == this.getText().charAt(i)) {
                m_CanHasHorLineOfTable = 1;
                return true;
            }
        }
        m_CanHasHorLineOfTable = 0;
        return false;
    }


    public static CorrLine _new337(boolean _arg1) {
        CorrLine res = new CorrLine();
        res.isPureText = _arg1;
        return res;
    }
    public CorrLine() {
    }
}
