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

    public int beginInd;

    public int endInd;

    public com.pullenti.unitext.UnitextTable result;

    public static CorrTable tryParse(java.util.ArrayList<CorrLine> lines, int i, com.pullenti.unisharp.Outargwrapper<Integer> nexti) {
        nexti.value = i;
        if (i >= lines.size()) 
            return null;
        if (!lines.get(i).canBeEmptyLineOfTable()) 
            return null;
        int width = lines.get(i).getText().length();
        int j;
        for (j = i + 1; j < lines.size(); j++) {
            if (!lines.get(j).isPureText) 
                break;
            else if (lines.get(j).length != lines.get(i).length || lines.get(j).getText().length() != width) 
                break;
        }
        int height = j - i;
        nexti.value = j - 1;
        if ((width < 30) || (height < 4)) 
            return null;
        java.util.ArrayList<Integer> verts = new java.util.ArrayList<Integer>();
        for (j = 0; j < width; j++) {
            int r;
            for (r = 0; r < height; r++) {
                if (lines.get(i + r).canHasHorLineOfTable()) 
                    continue;
                if (j >= lines.get(i + r).getText().length()) 
                    continue;
                char ch = lines.get(i + r).getText().charAt(j);
                if (CorrLine.isTableChar(ch) && !CorrLine.isHiphen(ch)) {
                    verts.add(j);
                    break;
                }
            }
        }
        if (verts.size() < 3) 
            return null;
        java.util.ArrayList<Integer> colBegs = new java.util.ArrayList<Integer>();
        java.util.ArrayList<Integer> colEnds = new java.util.ArrayList<Integer>();
        for (j = 0; j < verts.size(); j++) {
            int b = 0;
            if (j > 0) 
                b = verts.get(j - 1) + 1;
            if ((verts.get(j) - 1) >= b) {
                colBegs.add(b);
                colEnds.add(verts.get(j) - 1);
            }
        }
        if (colBegs.size() < 2) 
            return null;
        java.util.ArrayList<Integer> rnums = new java.util.ArrayList<Integer>();
        int rnum = 0;
        for (j = 0; j < height; j++) {
            rnums.add(rnum);
            int w;
            for (w = 0; w < width; w++) {
                if (!verts.contains(w)) {
                    if (w >= lines.get(i + j).getText().length()) 
                        continue;
                    char ch = lines.get(i + j).getText().charAt(w);
                    if (CorrLine.isTableChar(ch)) {
                        if (CorrLine.isHiphen(ch)) {
                            if (w > 0 && lines.get(i + j).getText().charAt(w - 1) == ch) {
                            }
                            else if (((w + 1) < width) && lines.get(i + j).getText().charAt(w + 1) == ch) {
                            }
                            else 
                                continue;
                        }
                        rnum++;
                        break;
                    }
                }
            }
        }
        if (rnum < 3) 
            return null;
        CorrTable tab = _new346(i, (i + height) - 1);
        tab.result = new com.pullenti.unitext.UnitextTable();
        StringBuilder tmp = new StringBuilder();
        for (int h = 0; h < rnums.size(); h++) {
            for (int c = 0; c < colBegs.size(); c++) {
                int w0 = colBegs.get(c);
                int w1 = colEnds.get(c);
                int h0 = h;
                int ww;
                for (; h0 < rnums.size(); h0++) {
                    for (ww = w0; ww <= w1; ww++) {
                        char ch = lines.get(i + h0).getText().charAt(ww);
                        if (!CorrLine.isTableChar(ch)) 
                            break;
                    }
                    if (ww < w1) 
                        break;
                }
                if (h0 >= rnums.size()) 
                    continue;
                com.pullenti.unitext.UnitextTablecell cel0 = tab.result.getCell(rnums.get(h0), c);
                if (cel0 != null) 
                    continue;
                int cc;
                for (cc = c + 1; cc < colBegs.size(); cc++) {
                    for (ww = colEnds.get(cc - 1); ww <= colEnds.get(cc); ww++) {
                        char ch = lines.get(i + h0).getText().charAt(ww);
                        if (CorrLine.isTableChar(ch) && !CorrLine.isHiphen(ch)) 
                            break;
                    }
                    if (ww < colEnds.get(cc)) 
                        break;
                    w1 = colEnds.get(cc);
                }
                cc--;
                int h1;
                for (h1 = h0 + 1; h1 < rnums.size(); h1++) {
                    for (ww = w0; ww <= w1; ww++) {
                        char ch = lines.get(i + h1).getText().charAt(ww);
                        if (CorrLine.isTableChar(ch)) {
                            if (CorrLine.isHiphen(ch)) {
                                if (ww > 0 && lines.get(i + h1).getText().charAt(ww - 1) == ch) 
                                    break;
                                if (((ww + 1) < width) && lines.get(i + h1).getText().charAt(ww + 1) == ch) 
                                    break;
                            }
                            else 
                                break;
                        }
                    }
                    if (ww < w1) 
                        break;
                }
                h1--;
                tmp.setLength(0);
                for (int hh = h0; hh <= h1; hh++) {
                    for (ww = w0 - 1; ww <= w1; ww++) {
                        if (ww < 0) 
                            continue;
                        char ch = lines.get(i + hh).getText().charAt(ww);
                        if (CorrLine.isTableChar(ch)) {
                            if (!CorrLine.isHiphen(ch)) 
                                continue;
                        }
                        if (com.pullenti.unisharp.Utils.isWhitespace(ch)) {
                            if (tmp.length() > 0 && tmp.charAt(tmp.length() - 1) == ' ') 
                                continue;
                            ch = ' ';
                        }
                        tmp.append(ch);
                    }
                    if ((hh < h1) && tmp.length() > 0) 
                        tmp.append(' ');
                    for (int jj = tmp.length() - 1; jj >= 0; jj--) {
                        if (tmp.charAt(jj) != ' ') {
                            if (CorrLine.isHiphen(tmp.charAt(jj))) 
                                tmp.setLength(jj);
                            break;
                        }
                    }
                }
                com.pullenti.unitext.UnitextTablecell cel = tab.result.addCell(rnums.get(h0), rnums.get(h1), c, cc, com.pullenti.unitext.UnitextPlaintext._new57(tmp.toString().trim()), false);
                c = cc;
            }
        }
        tab.result.optimize(false, null);
        return tab;
    }

    public static CorrTable _new346(int _arg1, int _arg2) {
        CorrTable res = new CorrTable();
        res.beginInd = _arg1;
        res.endInd = _arg2;
        return res;
    }
    public CorrTable() {
    }
}
