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

public class DocTable {

    public int cols = 0;

    public java.util.ArrayList<DocTableRow> rows = new java.util.ArrayList<DocTableRow>();

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

    public boolean hideBorders = false;

    public boolean tabHeaderPaginate = false;

    private com.pullenti.unitext.UnitextStyledFragment m_Style;

    public void read(DocxToText own, com.pullenti.unitext.UnitextStyledFragment sfrag, java.util.ArrayList<org.w3c.dom.Node> stackNodes) {
        if (sfrag != null) {
            m_Style = com.pullenti.unitext.UnitextStyledFragment._new449(com.pullenti.unitext.UnitextStyledFragmentType.TABLE, sfrag);
            sfrag.children.add(m_Style);
        }
        org.w3c.dom.Node xml = stackNodes.get(stackNodes.size() - 1);
        for (org.w3c.dom.Node x : (new com.pullenti.unisharp.XmlNodeListWrapper(xml.getChildNodes())).arr) {
            if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(x), "tblGrid")) {
                for (org.w3c.dom.Node xx : (new com.pullenti.unisharp.XmlNodeListWrapper(x.getChildNodes())).arr) {
                    if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(xx), "gridCol")) {
                        cols++;
                        if (xx.getAttributes() != null) {
                            for (org.w3c.dom.Node a : (new com.pullenti.unisharp.XmlAttrListWrapper(xx.getAttributes())).arr) {
                                if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(a), "w")) {
                                    int n;
                                    com.pullenti.unisharp.Outargwrapper<Integer> wrapn450 = new com.pullenti.unisharp.Outargwrapper<Integer>();
                                    boolean inoutres451 = com.pullenti.unisharp.Utils.parseInteger((String)com.pullenti.unisharp.Utils.notnull(a.getNodeValue(), ""), 0, null, wrapn450);
                                    n = (wrapn450.value != null ? wrapn450.value : 0);
                                    if (inoutres451) 
                                        colWidth.add(n);
                                }
                            }
                        }
                    }
                }
            }
            else if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(x), "tblPr")) {
                if (m_Style != null) {
                    com.pullenti.unitext.UnitextStyle st0 = new com.pullenti.unitext.UnitextStyle();
                    own.m_Styles.readUnitextStyle(x, st0, false);
                    st0.removeInheritAttrs(m_Style);
                    m_Style.setStyle(own.m_Styles.registerStyle(st0));
                }
                int cou = 0;
                for (org.w3c.dom.Node xx : (new com.pullenti.unisharp.XmlNodeListWrapper(x.getChildNodes())).arr) {
                    if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(xx), "tblBorders")) {
                        for (org.w3c.dom.Node xxx : (new com.pullenti.unisharp.XmlNodeListWrapper(xx.getChildNodes())).arr) {
                            if (((com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(xxx), "top") || com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(xxx), "left") || com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(xxx), "right")) || com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(xxx), "bottom") || com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(xxx), "start")) || com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(xxx), "end")) {
                                if (xxx.getAttributes() != null) {
                                    for (org.w3c.dom.Node a : (new com.pullenti.unisharp.XmlAttrListWrapper(xxx.getAttributes())).arr) {
                                        if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(a), "color")) {
                                            if (com.pullenti.unisharp.Utils.stringsEq(a.getNodeValue(), "FFFFFF")) 
                                                cou++;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                if (cou == 4) 
                    hideBorders = true;
            }
            else if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(x), "tr")) {
                DocTableRow tr = new DocTableRow();
                rows.add(tr);
                int firstSpan = 0;
                for (org.w3c.dom.Node xx : (new com.pullenti.unisharp.XmlNodeListWrapper(x.getChildNodes())).arr) {
                    if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(xx), "tc")) {
                        DocTableCell tc = new DocTableCell();
                        tr.cells.add(tc);
                        stackNodes.add(xx);
                        tc.read(own, m_Style, stackNodes);
                        stackNodes.remove(stackNodes.size() - 1);
                        if (firstSpan > 0) {
                            tc.colSpan += firstSpan;
                            firstSpan = 0;
                        }
                    }
                    else if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(xx), "sdt")) {
                        for (org.w3c.dom.Node xxx : (new com.pullenti.unisharp.XmlNodeListWrapper(xx.getChildNodes())).arr) {
                            if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(xxx), "sdtContent")) {
                                for (org.w3c.dom.Node xxxx : (new com.pullenti.unisharp.XmlNodeListWrapper(xxx.getChildNodes())).arr) {
                                    if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(xxxx), "tc")) {
                                        DocTableCell tc = new DocTableCell();
                                        tr.cells.add(tc);
                                        stackNodes.add(xxxx);
                                        tc.read(own, m_Style, stackNodes);
                                        stackNodes.remove(stackNodes.size() - 1);
                                        if (firstSpan > 0) {
                                            tc.colSpan += firstSpan;
                                            firstSpan = 0;
                                        }
                                        if (tc.uni instanceof com.pullenti.unitext.UnitextContainer) 
                                            ((com.pullenti.unitext.UnitextContainer)com.pullenti.unisharp.Utils.cast(tc.uni, com.pullenti.unitext.UnitextContainer.class)).typ = com.pullenti.unitext.UnitextContainerType.CONTENTCONTROL;
                                        else {
                                            com.pullenti.unitext.UnitextContainer ccc = com.pullenti.unitext.UnitextContainer._new452(com.pullenti.unitext.UnitextContainerType.CONTENTCONTROL, m_Style);
                                            ccc.children.add(tc.uni);
                                            tc.uni.parent = ccc;
                                            tc.uni = ccc;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(xx), "trPr")) {
                        for (org.w3c.dom.Node xxx : (new com.pullenti.unisharp.XmlNodeListWrapper(xx.getChildNodes())).arr) {
                            if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(xxx), "gridBefore") && xxx.getAttributes() != null && xxx.getAttributes().getLength() == 1) {
                                int cou;
                                com.pullenti.unisharp.Outargwrapper<Integer> wrapcou453 = new com.pullenti.unisharp.Outargwrapper<Integer>();
                                boolean inoutres454 = com.pullenti.unisharp.Utils.parseInteger(xxx.getAttributes().item(0).getTextContent(), 0, null, wrapcou453);
                                cou = (wrapcou453.value != null ? wrapcou453.value : 0);
                                if (inoutres454) {
                                    if (cou > 0) 
                                        firstSpan = cou;
                                }
                            }
                            else if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(xxx), "tblHeader")) 
                                tabHeaderPaginate = true;
                        }
                    }
                }
            }
        }
        if (colWidth.size() == cols) {
            int nn = 0;
            for (int w : colWidth) {
                nn += w;
            }
            if (nn > 0) {
                for (int ii = 0; ii < colWidth.size(); ii++) {
                    com.pullenti.unisharp.Utils.putArrayValue(colWidth, ii, (colWidth.get(ii) * 100) / nn);
                    if (colWidth.get(ii) == 0) 
                        com.pullenti.unisharp.Utils.putArrayValue(colWidth, ii, 1);
                }
            }
            else 
                colWidth.clear();
        }
        else 
            colWidth.clear();
        for (int i = 0; i < rows.size(); i++) {
            DocTableRow r = rows.get(i);
            int grNum = 1;
            for (int j = 0; j < r.cells.size(); j++) {
                DocTableCell c = r.cells.get(j);
                c.gridNum = grNum;
                grNum += c.colSpan;
                if (c.mergeVert != 1) 
                    continue;
                DocTableCell spanCell = null;
                for (int ii = i - 1; ii >= 0; ii--) {
                    DocTableRow rr = rows.get(ii);
                    int grrNum = 1;
                    for (int jj = 0; jj < rr.cells.size(); jj++) {
                        DocTableCell cc = rr.cells.get(jj);
                        if (cc.mergeVert == 2 && ((c.gridNum == cc.gridNum || c.gridNum == grrNum))) {
                            cc.rowSpan++;
                            spanCell = cc;
                            break;
                        }
                        grrNum += cc.colSpan;
                    }
                    if (spanCell != null) 
                        break;
                }
                if (spanCell == null) {
                }
            }
        }
    }

    public com.pullenti.unitext.UnitextTable createUni(int maxRows) {
        if (rows.size() == 0) 
            return null;
        com.pullenti.unitext.UnitextTable tab = new com.pullenti.unitext.UnitextTable();
        tab.m_StyledFrag = m_Style;
        tab.hideBorders = hideBorders;
        tab.tableHeaderPaginate = tabHeaderPaginate;
        for (int ii = 0; ii < colWidth.size(); ii++) {
            tab.setColWidth(ii, (((Integer)colWidth.get(ii)).toString() + "%"));
        }
        int rn = 0;
        for (DocTableRow r : rows) {
            int cn = 0;
            for (DocTableCell c : r.cells) {
                if (c.mergeVert == 1) 
                    continue;
                for (; ; cn++) {
                    if (tab.getCell(rn, cn) == null) 
                        break;
                }
                com.pullenti.unitext.UnitextTablecell cel = tab.addCell(rn, (rn + c.rowSpan) - 1, cn, (cn + c.colSpan) - 1, c.uni, false);
                cel.m_StyledFrag = c.frag;
                if (c.uni != null && c.uni.m_StyledFrag == c.frag) 
                    c.uni.m_StyledFrag = null;
                cn += c.colSpan;
            }
            rn++;
            if (maxRows > 0 && tab.getRowsCount() >= maxRows) 
                break;
        }
        return tab;
    }
    public DocTable() {
    }
}
