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

    public int num;

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

    public com.pullenti.unitext.UnitextPagebreak pageBreak;

    public java.util.ArrayList<UnitextCorrPdfLine> footnotes = new java.util.ArrayList<UnitextCorrPdfLine>();

    @Override
    public String toString() {
        StringBuilder tmp = new StringBuilder();
        tmp.append("№").append(num).append(" ");
        if (footnotes.size() > 0) 
            tmp.append("Foots:").append(footnotes.size()).append(" ");
        tmp.append(lines.get(0).toString());
        return tmp.toString();
    }

    private static java.util.ArrayList<UnitextCorrPdfPage> _createPages(com.pullenti.unitext.UnitextContainer cnt) {
        java.util.ArrayList<UnitextCorrPdfPage> pages = new java.util.ArrayList<UnitextCorrPdfPage>();
        UnitextCorrPdfPage page = null;
        UnitextCorrPdfLine line = null;
        for (int i = 0; i < cnt.children.size(); i++) {
            com.pullenti.unitext.UnitextItem it = cnt.children.get(i);
            it.tag = i;
            if (it instanceof com.pullenti.unitext.UnitextPlaintext) {
                String txt = ((com.pullenti.unitext.UnitextPlaintext)com.pullenti.unisharp.Utils.cast(it, com.pullenti.unitext.UnitextPlaintext.class)).text;
                if (com.pullenti.unisharp.Utils.isNullOrEmpty(txt)) 
                    continue;
                txt = txt.toUpperCase();
                if (txt.startsWith("КОНСУЛЬТАНТПЛЮС") || (txt.indexOf("ПРЕДОСТАВЛЕН КОНСУЛЬТАНТПЛЮС") >= 0)) {
                    int j;
                    for (j = i + 1; j < cnt.children.size(); j++) {
                        com.pullenti.unitext.UnitextItem it1 = cnt.children.get(j);
                        if (it1 instanceof com.pullenti.unitext.UnitextPagebreak) 
                            break;
                        if (it1 instanceof com.pullenti.unitext.UnitextNewline) 
                            continue;
                        if (!(it1 instanceof com.pullenti.unitext.UnitextPlaintext)) 
                            break;
                        txt = ((com.pullenti.unitext.UnitextPlaintext)com.pullenti.unisharp.Utils.cast(it1, com.pullenti.unitext.UnitextPlaintext.class)).text;
                        if (com.pullenti.unisharp.Utils.isNullOrEmpty(txt)) 
                            continue;
                        txt = txt.toUpperCase();
                        if ((txt.indexOf("ПРАВОВАЯ ПОДДЕРЖКА") >= 0) || (txt.indexOf("CONSULTANT.RU") >= 0)) 
                            continue;
                        if ((txt.indexOf("КОНСУЛЬТАНТПЛЮС") >= 0) || (txt.indexOf("ДАТА СОХРАНЕНИЯ") >= 0)) 
                            continue;
                        break;
                    }
                    for(int jjj = i + j - i - 1, mmm = i; jjj >= mmm; jjj--) cnt.children.remove(jjj);
                    i--;
                    continue;
                }
                if (page == null) {
                    page = new UnitextCorrPdfPage();
                    pages.add(page);
                    page.num = pages.size();
                    line = null;
                    if (page.num == 19) {
                    }
                }
                if (line == null) {
                    line = new UnitextCorrPdfLine();
                    page.lines.add(line);
                }
                line.texts.add((com.pullenti.unitext.UnitextPlaintext)com.pullenti.unisharp.Utils.cast(it, com.pullenti.unitext.UnitextPlaintext.class));
                continue;
            }
            if (it instanceof com.pullenti.unitext.UnitextNewline) {
                if (line != null) 
                    line.newlines.add((com.pullenti.unitext.UnitextNewline)com.pullenti.unisharp.Utils.cast(it, com.pullenti.unitext.UnitextNewline.class));
                line = null;
                continue;
            }
            if (it instanceof com.pullenti.unitext.UnitextPagebreak) {
                if (page != null) 
                    page.pageBreak = (com.pullenti.unitext.UnitextPagebreak)com.pullenti.unisharp.Utils.cast(it, com.pullenti.unitext.UnitextPagebreak.class);
                page = null;
                continue;
            }
            if (it instanceof com.pullenti.unitext.UnitextFootnote) 
                continue;
            line = null;
        }
        return pages;
    }

    public static void correct(com.pullenti.unitext.UnitextContainer cnt) {
        java.util.ArrayList<UnitextCorrPdfPage> pages = _createPages(cnt);
        if (pages.size() == 0) 
            return;
        java.util.HashMap<String, Integer> titles = new java.util.HashMap<String, Integer>();
        for (UnitextCorrPdfPage p : pages) {
            for (int k = 0; k < 2; k++) {
                UnitextCorrPdfLine line = p.lines.get((k == 0 ? 0 : (p.lines.size() - 1)));
                String tit = line.getHeadTextWithoutNumbers();
                if (tit != null) {
                    if (!titles.containsKey(tit)) 
                        titles.put(tit, 1);
                    else 
                        titles.put(tit, titles.get(tit) + 1);
                }
                if (k == 0 && p.lines.size() == 1) 
                    break;
            }
        }
        boolean changes = false;
        int minCou = pages.size() / 4;
        if (minCou < 2) 
            minCou = 2;
        for (UnitextCorrPdfPage p : pages) {
            for (int k = 0; k < 2; k++) {
                if (p.lines.size() == 0) 
                    break;
                UnitextCorrPdfLine line = p.lines.get((k == 0 ? 0 : (p.lines.size() - 1)));
                String tit = line.getHeadTextWithoutNumbers();
                if (tit != null) {
                    int cou = 0;
                    com.pullenti.unisharp.Outargwrapper<Integer> wrapcou364 = new com.pullenti.unisharp.Outargwrapper<Integer>();
                    com.pullenti.unisharp.Utils.tryGetValue(titles, tit, wrapcou364);
                    cou = (wrapcou364.value != null ? wrapcou364.value : 0);
                    if (cou > minCou || ((cou == minCou && pages.size() == minCou))) {
                        line.removeAll();
                        p.lines.remove(line);
                        changes = true;
                        k--;
                        continue;
                    }
                }
                if (line.isPureNumber()) {
                    line.removeAll();
                    p.lines.remove(line);
                    changes = true;
                    k--;
                    continue;
                }
                if (k == 0 && p.lines.size() == 1) 
                    break;
            }
        }
        if (changes) {
            for (int i = cnt.children.size() - 1; i >= 0; i--) {
                if (cnt.children.get(i).tag == null) 
                    cnt.children.remove(i);
            }
            for (int i = cnt.children.size() - 1; i >= 0; i--) {
                cnt.children.get(i).tag = i;
            }
        }
        for (UnitextCorrPdfPage p : pages) {
            if (p.num == 57) {
            }
            p.processFootnotes();
        }
        changes = false;
        for (int i = pages.size() - 1; i >= 0; i--) {
            if (i == 17) {
            }
            if (pages.get(i).createFootnotes(cnt)) 
                changes = true;
        }
        for (int i = 0; i < cnt.children.size(); i++) {
            cnt.children.get(i).tag = i;
        }
        int total = 0;
        int hiphens = 0;
        for (UnitextCorrPdfPage p : pages) {
            for (UnitextCorrPdfLine l : p.lines) {
                if (l.getTextLength() < 40) 
                    continue;
                total++;
                if (CorrLine.isHiphen(l.getLastChar())) 
                    hiphens++;
            }
        }
        boolean hasHiph = false;
        if (hiphens > 0 && (((hiphens * 100) / total)) > 10) 
            hasHiph = true;
        for (int i = pages.size() - 1; i >= 0; i--) {
            UnitextCorrPdfPage page = pages.get(i);
            if (page.lines.size() == 0) 
                continue;
            if (i == 2) {
            }
            if (((i + 1) < pages.size()) && pages.get(i + 1).lines.size() > 0) {
                UnitextCorrPdfLine last = page.lines.get(page.lines.size() - 1);
                if (last.canUnion(pages.get(i + 1).lines.get(0))) {
                    page.pageBreak.typ = com.pullenti.unitext.WhitespaceType.ONESPACE;
                    if (hasHiph && last.isLastCharHiphen()) {
                        page.pageBreak.typ = com.pullenti.unitext.WhitespaceType.IGNORABLE;
                        if (((i + 1) < page.lines.size()) && Character.isLowerCase(page.lines.get(i + 1).getFirstChar())) 
                            last.removeLastHiph();
                    }
                    for (int k = last.newlines.size() - 1; k >= 0; k--) {
                        cnt.children.remove((int)last.newlines.get(k).tag);
                    }
                }
            }
            for (int j = page.lines.size() - 2; j >= 0; j--) {
                UnitextCorrPdfLine line = page.lines.get(j);
                if (line.canUnion(page.lines.get(j + 1))) {
                    int i0 = -1;
                    if (line.newlines.size() > 0) 
                        i0 = (int)line.newlines.get(0).tag;
                    for (int k = line.newlines.size() - 1; k >= 0; k--) {
                        cnt.children.remove((int)line.newlines.get(k).tag);
                    }
                    if (hasHiph && line.isLastCharHiphen()) {
                        if (Character.isLowerCase(page.lines.get(j + 1).getFirstChar())) 
                            line.removeLastHiph();
                    }
                    else {
                        if (i0 < 0) 
                            i0 = ((int)line.texts.get(line.texts.size() - 1).tag) + 1;
                        cnt.children.add(i0, com.pullenti.unitext.UnitextPlaintext._new57(" "));
                    }
                }
            }
        }
        for (int i = 0; i < (cnt.children.size() - 1); i++) {
            if ((cnt.children.get(i) instanceof com.pullenti.unitext.UnitextPagebreak) && (cnt.children.get(i + 1) instanceof com.pullenti.unitext.UnitextNewline)) {
                cnt.children.remove(i + 1);
                i--;
                continue;
            }
            else if ((cnt.children.get(i + 1) instanceof com.pullenti.unitext.UnitextPagebreak) && (cnt.children.get(i) instanceof com.pullenti.unitext.UnitextNewline)) {
                cnt.children.remove(i);
                i--;
                continue;
            }
        }
    }

    public void processFootnotes() {
        java.util.ArrayList<UnitextCorrPdfLine> foots = new java.util.ArrayList<UnitextCorrPdfLine>();
        int max = 0;
        for (int i = lines.size() - 1; i >= 0; i--) {
            int _num = lines.get(i).getFootnoteHeadNum();
            if (_num > 0) {
                foots.add(0, lines.get(i));
                max = 0;
            }
            else 
                max++;
            if (max > 5) 
                break;
        }
        if (foots.size() == 0) 
            return;
        for (int i = 0; i < (foots.size() - 1); i++) {
            if ((foots.get(i).getFootnoteHeadNum() + 1) != foots.get(i + 1).getFootnoteHeadNum()) {
                if (foots.get(i).getFootnoteHeadNum() != (i + 1)) 
                    return;
                if ((i + 2) == foots.size()) {
                }
                else if ((foots.get(i).getFootnoteHeadNum() + 1) == foots.get(i + 2).getFootnoteHeadNum()) {
                }
                else 
                    return;
                foots.remove(i + 1);
                i--;
            }
        }
        int fi = 0;
        int j = 0;
        max = lines.indexOf(foots.get(0));
        for (; fi < foots.size(); fi++) {
            UnitextCorrPdfLine foot = foots.get(fi);
            for (; j < max; j++) {
                UnitextCorrPdfLine li = lines.get(j);
                if (j == 18) {
                }
                if (li.attachFoot(foot)) 
                    break;
            }
            if (j >= max) 
                break;
        }
        if (fi < foots.size()) 
            return;
        com.pullenti.unisharp.Utils.addToArrayList(footnotes, foots);
    }

    public boolean createFootnotes(com.pullenti.unitext.UnitextContainer cnt) {
        if (footnotes.size() == 0) 
            return false;
        int i0 = 0;
        int i1;
        for (int f = footnotes.size() - 1; f >= 0; f--) {
            UnitextCorrPdfLine foot = footnotes.get(f);
            i0 = (int)foot.texts.get(0).tag;
            i1 = i0 + 100;
            if ((f + 1) < footnotes.size()) 
                i1 = (int)footnotes.get(f + 1).texts.get(0).tag;
            com.pullenti.unitext.UnitextFootnote fn = new com.pullenti.unitext.UnitextFootnote();
            fn.customMark = (((Integer)foot.getFootnoteHeadNum()).toString());
            foot.resFoot = fn;
            fn.content = new com.pullenti.unitext.UnitextContainer();
            boolean needSp = false;
            boolean needTruct = true;
            for (int i = i0; (i < i1) && (i < cnt.children.size()); i++) {
                com.pullenti.unitext.UnitextItem it = cnt.children.get(i);
                it.parent = fn.content;
                if (it instanceof com.pullenti.unitext.UnitextPlaintext) {
                    if (needTruct) {
                        needTruct = false;
                        com.pullenti.unitext.UnitextPlaintext pt0 = (com.pullenti.unitext.UnitextPlaintext)com.pullenti.unisharp.Utils.cast(it, com.pullenti.unitext.UnitextPlaintext.class);
                        pt0.text = pt0.text.substring(fn.customMark.length()).trim();
                    }
                    if (needSp) 
                        ((com.pullenti.unitext.UnitextContainer)com.pullenti.unisharp.Utils.cast(fn.content, com.pullenti.unitext.UnitextContainer.class)).children.add(com.pullenti.unitext.UnitextPlaintext._new57(" "));
                    needSp = false;
                    ((com.pullenti.unitext.UnitextContainer)com.pullenti.unisharp.Utils.cast(fn.content, com.pullenti.unitext.UnitextContainer.class)).children.add(it);
                }
                else if (it instanceof com.pullenti.unitext.UnitextNewline) 
                    needSp = true;
                else 
                    break;
            }
        }
        for (i1 = i0; i1 < cnt.children.size(); i1++) {
            com.pullenti.unitext.UnitextItem it = cnt.children.get(i1);
            if (it instanceof com.pullenti.unitext.UnitextPagebreak) 
                break;
        }
        for(int jjj = i0 + i1 - i0 - 1, mmm = i0; jjj >= mmm; jjj--) cnt.children.remove(jjj);
        for (int f = footnotes.size() - 1; f >= 0; f--) {
            UnitextCorrPdfLine foot = footnotes.get(f);
            i0 = (int)foot.refText.tag;
            if (foot.refPos == 0) {
                if (foot.refText.text.length() == foot.refLen) 
                    com.pullenti.unisharp.Utils.putArrayValue(cnt.children, i0, foot.resFoot);
                else {
                    foot.refText.text = com.pullenti.unisharp.Utils.trimStart(foot.refText.text.substring(foot.refLen));
                    cnt.children.add(i0, foot.resFoot);
                }
            }
            else if ((foot.refPos + foot.refLen) == foot.refText.text.length()) {
                foot.refText.text = foot.refText.text.substring(0, 0 + foot.refPos);
                cnt.children.add(i0 + 1, foot.resFoot);
            }
            else {
                String txtTail = foot.refText.text.substring(foot.refPos + foot.refLen, foot.refPos + foot.refLen + foot.refText.text.length() - foot.refPos - foot.refLen);
                foot.refText.text = foot.refText.text.substring(0, 0 + foot.refPos);
                cnt.children.add(i0 + 1, foot.resFoot);
                cnt.children.add(i0 + 2, com.pullenti.unitext.UnitextPlaintext._new57(txtTail));
            }
        }
        i0 = lines.indexOf(footnotes.get(0));
        if (i0 > 0) 
            for(int jjj = i0 + lines.size() - i0 - 1, mmm = i0; jjj >= mmm; jjj--) lines.remove(jjj);
        return true;
    }
    public UnitextCorrPdfPage() {
    }
}
