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

    public int number;

    public int number2;

    public String headText;

    public String text;

    public int i0;

    public int i1;

    public boolean corr = false;

    public void removeHeadText() {
        if (com.pullenti.unisharp.Utils.isNullOrEmpty(headText)) 
            return;
        if (m_Top) 
            text = headText + "\r\n" + (((text != null ? text : "")));
        else 
            text = (((text != null ? text : ""))) + "\r\n" + headText;
    }

    @Override
    public String toString() {
        return ("N=" + number + " '" + ((headText != null ? headText : "")) + "'");
    }

    public boolean isNumberDist1(CorrPageText next) {
        if (next.number == 0 || number == 0) 
            return false;
        if (next.number == (number + 1)) 
            return true;
        if (number2 > 0 && next.number == (number2 + 1)) 
            return true;
        if (next.number2 > 0 && next.number2 == (number + 1)) 
            return true;
        if (next.number2 > 0 && number2 > 0 && next.number2 == (number2 + 1)) 
            return true;
        return false;
    }

    private boolean m_Top;

    public CorrPageText(String txt, boolean top) {
        m_Top = top;
        if (txt == null) 
            txt = "";
        int _i0;
        int _i1;
        int i;
        if (top) {
            _i0 = 0;
            for (_i1 = _i0; _i1 < txt.length(); _i1++) {
                if (((int)txt.charAt(_i1)) == 0xD || ((int)txt.charAt(_i1)) == 0xA) 
                    break;
            }
            if (_i1 > _i0) {
                headText = txt.substring(0, 0 + _i1).trim();
                if (_i1 < txt.length()) 
                    text = txt.substring(_i1).trim();
                else 
                    text = "";
            }
            else 
                text = txt;
        }
        else {
            txt = txt.trim();
            _i1 = txt.length() - 1;
            for (_i0 = _i1; _i0 >= 0; _i0--) {
                if (((int)txt.charAt(_i0)) == 0xD || ((int)txt.charAt(_i0)) == 0xA) 
                    break;
            }
            if (_i1 > _i0) {
                if (_i0 < 0) {
                    headText = txt;
                    text = "";
                }
                else {
                    headText = txt.substring(_i0 + 1).trim();
                    text = txt.substring(0, 0 + _i0).trim();
                }
            }
            else 
                text = txt;
        }
        if (!com.pullenti.unisharp.Utils.isNullOrEmpty(headText) && Character.isDigit(headText.charAt(0))) {
            for (i = 0; i < headText.length(); i++) {
                if (!Character.isDigit(headText.charAt(i))) 
                    break;
            }
            if (i == headText.length() || headText.charAt(i) == ' ') {
                int num;
                com.pullenti.unisharp.Outargwrapper<Integer> wrapnum338 = new com.pullenti.unisharp.Outargwrapper<Integer>();
                boolean inoutres339 = com.pullenti.unisharp.Utils.parseInteger(headText.substring(0, 0 + i), 0, null, wrapnum338);
                num = (wrapnum338.value != null ? wrapnum338.value : 0);
                if (inoutres339) {
                    number = num;
                    headText = headText.substring(i).trim();
                }
            }
        }
        if (!com.pullenti.unisharp.Utils.isNullOrEmpty(headText) && Character.isDigit(headText.charAt(headText.length() - 1))) {
            while (true) {
                for (i = headText.length() - 1; i >= 0; i--) {
                    if (!Character.isDigit(headText.charAt(i))) 
                        break;
                }
                if ((i < 0) || headText.charAt(i) == ' ') {
                    int num;
                    com.pullenti.unisharp.Outargwrapper<Integer> wrapnum340 = new com.pullenti.unisharp.Outargwrapper<Integer>();
                    boolean inoutres341 = com.pullenti.unisharp.Utils.parseInteger(headText.substring(i + 1), 0, null, wrapnum340);
                    num = (wrapnum340.value != null ? wrapnum340.value : 0);
                    if (inoutres341) {
                        if (number > 0) 
                            number2 = num;
                        else 
                            number = num;
                        headText = headText.substring(0, 0 + i + 1).trim();
                        if (com.pullenti.unisharp.Utils.endsWithString(headText, "СТР", true)) 
                            headText = headText.substring(0, 0 + headText.length() - 3).trim();
                        if (com.pullenti.unisharp.Utils.endsWithString(headText, "СТР.", true)) 
                            headText = headText.substring(0, 0 + headText.length() - 4).trim();
                        String from = null;
                        if (com.pullenti.unisharp.Utils.endsWithString(headText, "ИЗ", true)) 
                            from = headText.substring(0, 0 + headText.length() - 2).trim();
                        else if (headText.endsWith("/") || headText.endsWith("\\")) 
                            from = headText.substring(0, 0 + headText.length() - 1).trim();
                        if (from != null && from.length() > 0 && Character.isDigit(from.charAt(from.length() - 1))) {
                            headText = from;
                            number = 0;
                            continue;
                        }
                    }
                }
                break;
            }
        }
        if (top && !com.pullenti.unisharp.Utils.isNullOrEmpty(text) && Character.isDigit(text.charAt(0))) {
            for (i = 0; i < text.length(); i++) {
                if (!Character.isDigit(text.charAt(i))) 
                    break;
            }
            if (i >= text.length() || text.charAt(i) == '\r' || text.charAt(i) == '\n') {
                int num;
                com.pullenti.unisharp.Outargwrapper<Integer> wrapnum342 = new com.pullenti.unisharp.Outargwrapper<Integer>();
                boolean inoutres343 = com.pullenti.unisharp.Utils.parseInteger(text.substring(0, 0 + i), 0, null, wrapnum342);
                num = (wrapnum342.value != null ? wrapnum342.value : 0);
                if (inoutres343) {
                    number = num;
                    if (i >= text.length()) 
                        text = "";
                    else 
                        text = text.substring(i).trim();
                }
            }
        }
        if (!top && !com.pullenti.unisharp.Utils.isNullOrEmpty(text) && Character.isDigit(text.charAt(text.length() - 1))) {
            for (i = text.length() - 1; i >= 0; i--) {
                if (!Character.isDigit(text.charAt(i))) 
                    break;
            }
            if ((i < 0) || text.charAt(i) == '\r' || text.charAt(i) == '\n') {
                int num;
                com.pullenti.unisharp.Outargwrapper<Integer> wrapnum344 = new com.pullenti.unisharp.Outargwrapper<Integer>();
                boolean inoutres345 = com.pullenti.unisharp.Utils.parseInteger(text.substring(i + 1), 0, null, wrapnum344);
                num = (wrapnum344.value != null ? wrapnum344.value : 0);
                if (inoutres345) {
                    number = num;
                    if (i <= 0) 
                        text = "";
                    else 
                        text = text.substring(0, 0 + i).trim();
                }
            }
        }
        if (com.pullenti.unisharp.Utils.isNullOrEmpty(headText)) 
            headText = null;
    }

    public static String process(java.util.ArrayList<CorrPageText> pts) {
        int cou = 0;
        int i;
        for (CorrPageText p : pts) {
            p.corr = false;
        }
        StringBuilder res = new StringBuilder();
        for (i = 1; i < (pts.size() - 1); i++) {
            if (pts.get(i - 1).number > 0 && ((pts.get(i).number == 0 || pts.get(i).number != (pts.get(i - 1).number + 1)))) {
                int j;
                boolean ok1 = false;
                boolean ok2 = false;
                int err = 0;
                for (j = i + 1; j < pts.size(); j++) {
                    if (pts.get(j).number > 0) {
                        if (pts.get(j).number == (pts.get(i - 1).number + (((j - i) + 1)))) {
                            ok1 = true;
                            break;
                        }
                        if (((j + 2) < pts.size()) && pts.get(j + 1).number == (pts.get(j).number + 1) && pts.get(j + 2).number == (pts.get(j).number + 2)) {
                            ok2 = true;
                            break;
                        }
                        if ((++err) > 10) 
                            break;
                    }
                }
                if (ok1 || ok2) {
                    for (int k = i; k < j; k++) {
                        pts.get(k).number = 0;
                    }
                }
            }
        }
        boolean noCorrFirst = false;
        if (pts.size() > 0 && pts.get(0).number > 1900) {
            pts.get(0).number = 0;
            noCorrFirst = true;
        }
        if (pts.size() > 2) {
            if (pts.get(0).number != 1 && pts.get(1).number > 0 && pts.get(2).number == (pts.get(1).number + 1)) {
                if (pts.get(0).number != (pts.get(1).number - 1)) {
                    pts.get(0).number = pts.get(1).number - 1;
                    noCorrFirst = true;
                }
            }
        }
        int firstPage = 0;
        if (pts.size() > 4 && pts.get(0).number == 0) {
            for (i = 0; i < (pts.size() - 5); i++) {
                if (pts.get(i).number != 0) {
                    boolean ok1 = true;
                    for (int j = i + 1; (j < pts.size()) && (j < (i + 4)); j++) {
                        if (!pts.get(j).isNumberDist1(pts.get(j + 1))) {
                            ok1 = false;
                            break;
                        }
                    }
                    if (ok1) {
                        firstPage = i;
                        for (--i; i >= 0; i--) {
                            pts.get(i).number = pts.get(i + 1).number - 1;
                        }
                    }
                    break;
                }
            }
        }
        int errcou = 0;
        for (i = 0; i < (pts.size() - 1); i++) {
            if (pts.get(i + 1).number <= 0) {
                if ((i + 1) < pts.size()) 
                    continue;
                errcou++;
                continue;
            }
            if (pts.get(i).number <= 0) {
                if (i <= firstPage) 
                    continue;
                errcou++;
                continue;
            }
            if (pts.get(i).isNumberDist1(pts.get(i + 1))) 
                cou++;
            else if (i == 0) {
            }
            else 
                errcou++;
        }
        boolean ok = false;
        if (cou == 0) {
            if ((pts.size() == 3 && pts.get(1).number == 2 && pts.get(0).number == 0) && pts.get(2).number == 0) 
                ok = true;
            else if (pts.size() == 2 && pts.get(1).number == 2 && ((pts.get(0).number == 1 || pts.get(0).number == 0))) 
                ok = true;
            else 
                ok = false;
        }
        else if (errcou == 0) 
            ok = true;
        java.util.HashMap<String, Integer> colstat = new java.util.HashMap<String, Integer>();
        for (i = 1; i < pts.size(); i++) {
            if (pts.get(i).headText != null) {
                if (colstat.containsKey(pts.get(i).headText)) 
                    colstat.put(pts.get(i).headText, colstat.get(pts.get(i).headText) + 1);
                else 
                    colstat.put(pts.get(i).headText, 1);
            }
        }
        int lev = pts.size() / 2;
        if (lev < 2) 
            lev = 2;
        for (i = 1; i < pts.size(); i++) {
            String h = pts.get(i).headText;
            if (h == null) 
                continue;
            int c = colstat.get(h);
            if (c >= lev) 
                pts.get(i).corr = true;
            else 
                pts.get(i).removeHeadText();
        }
        for (java.util.Map.Entry<String, Integer> kp : colstat.entrySet()) {
            if (kp.getValue() >= lev && kp.getKey().length() > 5) {
                if (res.length() > 0) 
                    res.append("\r\n");
                res.append(kp.getKey());
            }
        }
        if (cou > (errcou * 3)) {
            for (CorrPageText p : pts) {
                if (p.number > 0) {
                    if (noCorrFirst && p == pts.get(0)) 
                        continue;
                    if (!p.corr) 
                        p.removeHeadText();
                    p.corr = true;
                }
            }
        }
        return (res.length() == 0 ? null : res.toString());
    }

    public static CorrPageText _new358(String _arg1, boolean _arg2, int _arg3, int _arg4) {
        CorrPageText res = new CorrPageText(_arg1, _arg2);
        res.i0 = _arg3;
        res.i1 = _arg4;
        return res;
    }
    public CorrPageText() {
    }
}
