/*
 * 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 UnitextCorrPdfLine {

    public java.util.ArrayList<com.pullenti.unitext.UnitextPlaintext> texts = new java.util.ArrayList<com.pullenti.unitext.UnitextPlaintext>();

    public java.util.ArrayList<com.pullenti.unitext.UnitextNewline> newlines = new java.util.ArrayList<com.pullenti.unitext.UnitextNewline>();

    public void removeAll() {
        for (com.pullenti.unitext.UnitextPlaintext txt : texts) {
            txt.tag = null;
        }
        for (com.pullenti.unitext.UnitextNewline nl : newlines) {
            nl.tag = null;
        }
    }

    public boolean isPureNumber() {
        if (texts.size() > 1) 
            return false;
        int num = 0;
        for (char ch : texts.get(0).text.toCharArray()) {
            if (Character.isDigit(ch)) 
                num++;
            else if (ch != ' ' && ch != '.') 
                return false;
        }
        return num > 0;
    }


    public String getHeadTextWithoutNumbers() {
        StringBuilder tmp = new StringBuilder();
        for (com.pullenti.unitext.UnitextPlaintext t : texts) {
            tmp.append(t.text.trim());
        }
        for (int i = 0; i < tmp.length(); i++) {
            if (!Character.isDigit(tmp.charAt(i))) {
                if (i > 0) 
                    tmp.delete(0, 0 + i);
                break;
            }
        }
        for (int i = tmp.length() - 1; i >= 0; i--) {
            if (!Character.isDigit(tmp.charAt(i))) {
                tmp.setLength(i + 1);
                break;
            }
        }
        if (tmp.length() == 0) 
            return null;
        if (Character.isDigit(tmp.charAt(0))) 
            return null;
        return tmp.toString().trim();
    }


    public char getLastChar() {
        for (int i = texts.size() - 1; i >= 0; i--) {
            for (int j = texts.get(i).text.length() - 1; j >= 0; j--) {
                char ch = texts.get(i).text.charAt(j);
                if (!com.pullenti.unisharp.Utils.isWhitespace(ch)) 
                    return ch;
            }
        }
        return ' ';
    }


    public boolean isLastCharHiphen() {
        return CorrLine.isHiphen(this.getLastChar());
    }


    public void removeLastHiph() {
        for (int i = texts.size() - 1; i >= 0; i--) {
            for (int j = texts.get(i).text.length() - 1; j >= 0; j--) {
                char ch = texts.get(i).text.charAt(j);
                if (CorrLine.isHiphen(ch)) {
                    texts.get(i).text = texts.get(i).text.substring(0, 0 + j);
                    return;
                }
                if (!com.pullenti.unisharp.Utils.isWhitespace(ch)) 
                    return;
            }
        }
    }

    public char getFirstChar() {
        for (int i = 0; i < texts.size(); i++) {
            for (char ch : texts.get(i).text.toCharArray()) {
                if (!com.pullenti.unisharp.Utils.isWhitespace(ch)) 
                    return ch;
            }
        }
        return ' ';
    }


    public char getSecondChar() {
        int cou = 0;
        for (int i = 0; i < texts.size(); i++) {
            for (char ch : texts.get(i).text.toCharArray()) {
                if (!com.pullenti.unisharp.Utils.isWhitespace(ch)) {
                    cou++;
                    if (cou > 1) 
                        return ch;
                }
            }
        }
        return ' ';
    }


    public int getTextLength() {
        int len = 0;
        for (com.pullenti.unitext.UnitextPlaintext t : texts) {
            for (char ch : t.text.toCharArray()) {
                if (!com.pullenti.unisharp.Utils.isWhitespace(ch)) 
                    len += 1;
            }
        }
        return len;
    }


    public int getNewlinesCount() {
        int cou = 0;
        for (com.pullenti.unitext.UnitextNewline nl : newlines) {
            cou += nl.count;
        }
        return cou;
    }


    private static boolean _isOpenBracket(char ch) {
        return "\"'`’<{([«“„”".indexOf(ch) >= 0;
    }

    private static boolean _isCloseBracket(char ch) {
        return "\"'`’>})]»”“".indexOf(ch) >= 0;
    }

    public int getFootnoteHeadNum() {
        String txt = texts.get(0).text;
        for (int i = 0; i < txt.length(); i++) {
            if (!com.pullenti.unisharp.Utils.isWhitespace(txt.charAt(i))) {
                if (!Character.isDigit(txt.charAt(i))) 
                    return 0;
                int num = (((int)txt.charAt(i)) - ((int)'0'));
                for (i = i + 1; i < txt.length(); i++) {
                    if (Character.isDigit(txt.charAt(i))) 
                        num = (num * 10) + ((((int)txt.charAt(i)) - ((int)'0')));
                    else 
                        break;
                }
                if (num > 900) 
                    return 0;
                if (i < txt.length()) {
                    for (; i < txt.length(); i++) {
                        if (!com.pullenti.unisharp.Utils.isWhitespace(txt.charAt(i))) 
                            break;
                    }
                    if (i >= txt.length()) {
                        if (texts.size() > 1 && texts.get(0).typ != com.pullenti.unitext.UnitextPlaintextType.SIMPLE) 
                            return num;
                        return 0;
                    }
                    if (Character.isLetter(txt.charAt(i))) {
                        if (!Character.isUpperCase(txt.charAt(i))) 
                            return 0;
                    }
                    else if (!_isOpenBracket(txt.charAt(i))) 
                        return 0;
                    if (getTextLength() < 8) 
                        return 0;
                    return num;
                }
                if (texts.get(0).typ != com.pullenti.unitext.UnitextPlaintextType.SIMPLE && texts.size() > 1) {
                    if (getTextLength() < 8) 
                        return 0;
                    return num;
                }
                break;
            }
        }
        return 0;
    }


    public UnitextCorrPdfLine refLine;

    public com.pullenti.unitext.UnitextPlaintext refText;

    public int refPos;

    public int refLen;

    public com.pullenti.unitext.UnitextFootnote resFoot;

    public boolean canUnion(UnitextCorrPdfLine next) {
        if (getTextLength() < 30) 
            return false;
        char last = getLastChar();
        char first = next.getFirstChar();
        if (CorrLine.isHiphen(last) || last == ',') 
            return true;
        if (last == '.' || last == ';' || last == ':') 
            return false;
        if (Character.isLetter(first) && Character.isLowerCase(first)) {
            if (Character.isLetter(last) && Character.isUpperCase(last)) 
                return false;
            char ch1 = next.getSecondChar();
            if (ch1 == ')' || ch1 == '.') 
                return false;
            return true;
        }
        if (_isOpenBracket(first)) {
            if (first == '(') 
                return true;
            char sec = next.getSecondChar();
            if (Character.isLetter(sec) && Character.isLowerCase(sec)) 
                return true;
        }
        return false;
    }

    public boolean attachFoot(UnitextCorrPdfLine foot) {
        String num = ((Integer)foot.getFootnoteHeadNum()).toString();
        if (com.pullenti.unisharp.Utils.stringsEq(num, "17")) {
        }
        for (int i = 0; i < texts.size(); i++) {
            String txt = texts.get(i).text;
            for (int j = 0; j < txt.length(); j++) {
                if (txt.charAt(j) == num.charAt(0)) {
                    int k;
                    for (k = 0; (k < num.length()) && ((k + j) < txt.length()); k++) {
                        if (num.charAt(k) != txt.charAt(j + k)) 
                            break;
                    }
                    if (k < num.length()) 
                        continue;
                    int j1 = (j + k) - 1;
                    char ch0 = ' ';
                    char ch00 = ' ';
                    boolean doubt = false;
                    if (j == 0) {
                        if (i == 0) 
                            continue;
                        String txt0 = texts.get(i - 1).text;
                        ch0 = txt0.charAt(txt0.length() - 1);
                        if (txt0.length() > 1) 
                            ch00 = txt0.charAt(txt0.length() - 2);
                    }
                    else {
                        ch0 = txt.charAt(j - 1);
                        if (j > 1) 
                            ch00 = txt.charAt(j - 2);
                    }
                    if (com.pullenti.unisharp.Utils.isWhitespace(ch0) || Character.isDigit(ch0)) 
                        continue;
                    if (Character.isLetter(ch0)) {
                    }
                    else if (ch0 == '.' || ch0 == ';' || _isCloseBracket(ch0)) {
                        if (!Character.isLetter(ch00)) 
                            continue;
                    }
                    else if (ch0 == ',') 
                        doubt = true;
                    else 
                        continue;
                    ch0 = ' ';
                    if (j1 == (txt.length() - 1)) {
                        if (i == (texts.size() - 1)) 
                            ch0 = ' ';
                        else 
                            ch0 = texts.get(i + 1).text.charAt(0);
                    }
                    else 
                        ch0 = txt.charAt(j1 + 1);
                    if (Character.isLetterOrDigit(ch0)) 
                        continue;
                    if (doubt && ch0 != ' ' && texts.get(i).typ == com.pullenti.unitext.UnitextPlaintextType.SIMPLE) 
                        continue;
                    foot.refLine = this;
                    foot.refText = texts.get(i);
                    foot.refPos = j;
                    foot.refLen = (j1 + 1) - j;
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public String toString() {
        StringBuilder tmp = new StringBuilder();
        int num = getFootnoteHeadNum();
        if (num > 0) 
            tmp.append("[").append(num).append("] ");
        if (refText != null) 
            tmp.append(" -> (").append(refText.text).append(")[").append(refPos).append("] ");
        for (com.pullenti.unitext.UnitextPlaintext t : texts) {
            tmp.append(t.text);
        }
        return tmp.toString();
    }
    public UnitextCorrPdfLine() {
    }
}
