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

public class RichTextTable extends RichTextItem {

    public java.util.ArrayList<Integer> columnIds = new java.util.ArrayList<Integer>();

    @Override
    public String toString() {
        return ("Table: " + columnIds.size() + " Columns, " + children.size() + " Rows");
    }

    public RichTextRow getLastRow() {
        if (children == null || children.size() == 0) 
            return null;
        return (RichTextRow)com.pullenti.unisharp.Utils.cast(children.get(children.size() - 1), RichTextRow.class);
    }


    public RichTextCell getLastCell() {
        RichTextRow r = getLastRow();
        if (r == null) 
            return null;
        if (r.children == null || r.children.size() == 0) 
            return null;
        return (RichTextCell)com.pullenti.unisharp.Utils.cast(r.children.get(r.children.size() - 1), RichTextCell.class);
    }


    public void correct() {
        if (getLastRow() != null && !getLastRow().endOf && ((this.getLastCell() == null || !this.getLastCell().endOf))) 
            children.remove(children.size() - 1);
        for (RichTextItem r : children) {
            RichTextRow rr = (RichTextRow)com.pullenti.unisharp.Utils.cast(r, RichTextRow.class);
            if (rr == null) 
                continue;
            if (rr.children != null && rr.children.size() > 1) {
                RichTextCell la = (RichTextCell)com.pullenti.unisharp.Utils.cast(rr.children.get(rr.children.size() - 1), RichTextCell.class);
                if (!la.endOf && la.text == null && la.children == null) 
                    rr.children.remove(rr.children.size() - 1);
            }
            for (RichTextRow.CellInfo ci : rr.cellsInfo) {
                if (ci.id > 0 && !columnIds.contains(ci.id)) 
                    columnIds.add(ci.id);
            }
            if (rr.cellsInfo.size() == rr.children.size()) {
                for (int ii = 0; ii < rr.cellsInfo.size(); ii++) {
                    ((RichTextCell)com.pullenti.unisharp.Utils.cast(rr.children.get(ii), RichTextCell.class)).columnId = rr.cellsInfo.get(ii).id;
                    ((RichTextCell)com.pullenti.unisharp.Utils.cast(rr.children.get(ii), RichTextCell.class)).mergeToTop = rr.cellsInfo.get(ii).vertMergeNext;
                    ((RichTextCell)com.pullenti.unisharp.Utils.cast(rr.children.get(ii), RichTextCell.class)).mergeBottoms = rr.cellsInfo.get(ii).vertMergeFirst;
                }
            }
        }
        java.util.Collections.sort(columnIds);
        java.util.HashMap<Integer, RichTextCell> vertMerge = new java.util.HashMap<Integer, RichTextCell>();
        for (RichTextItem r : children) {
            RichTextRow rr = (RichTextRow)com.pullenti.unisharp.Utils.cast(r, RichTextRow.class);
            if (rr == null) 
                continue;
            java.util.ArrayList<RichTextCell> cells = new java.util.ArrayList<RichTextCell>();
            for (RichTextItem c : rr.children) {
                if (c instanceof RichTextCell) 
                    cells.add((RichTextCell)com.pullenti.unisharp.Utils.cast(c, RichTextCell.class));
            }
            for (RichTextCell cel : cells) {
                cel.lastGrid = columnIds.indexOf(cel.columnId);
            }
            for (int ii = 0; ii < cells.size(); ii++) {
                if (ii == 0) 
                    cells.get(ii).fistGrid = 0;
                else if (cells.get(ii - 1).lastGrid >= 0) 
                    cells.get(ii).fistGrid = cells.get(ii - 1).lastGrid + 1;
                if (ii == (cells.size() - 1)) 
                    cells.get(ii).lastGrid = columnIds.size() - 1;
            }
            for (int ii = 0; ii < cells.size(); ii++) {
                RichTextCell cel = cells.get(ii);
                if (cel.lastGrid > cel.fistGrid && cel.fistGrid >= 0) 
                    cel.colsSpan = (cel.lastGrid - cel.fistGrid) + 1;
                if (cel.mergeBottoms && cel.lastGrid >= 0) {
                    if (!vertMerge.containsKey(cel.lastGrid)) 
                        vertMerge.put(cel.lastGrid, cel);
                    else 
                        vertMerge.put(cel.lastGrid, cel);
                }
                else if (cel.mergeToTop && vertMerge.containsKey(cel.lastGrid)) {
                    vertMerge.get(cel.lastGrid).rowsSpan++;
                    rr.children.remove(cel);
                }
            }
        }
    }
    public RichTextTable() {
        super();
    }
}
