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

/**
 * Ячейка таблицы
 */
public class UnitextTablecell extends UnitextItem {

    private int _colbegin;

    public int getColBegin() {
        return _colbegin;
    }

    public int setColBegin(int value) {
        _colbegin = value;
        return _colbegin;
    }


    private int _colend;

    public int getColEnd() {
        return _colend;
    }

    public int setColEnd(int value) {
        _colend = value;
        return _colend;
    }


    private int _rowbegin;

    public int getRowBegin() {
        return _rowbegin;
    }

    public int setRowBegin(int value) {
        _rowbegin = value;
        return _rowbegin;
    }


    private int _rowend;

    public int getRowEnd() {
        return _rowend;
    }

    public int setRowEnd(int value) {
        _rowend = value;
        return _rowend;
    }


    /**
     * Содержимое (может быть null)
     * 
     */
    public UnitextItem content;

    /**
     * Это признак восстановления слипшихся строк таблицы
     */
    public boolean isPsevdo = false;

    /**
     * Ячейка принадлежит заголовку таблицы
     */
    public boolean isHeader = false;

    @Override
    public UnitextItem clone() {
        UnitextTablecell res = new UnitextTablecell();
        res._cloneFrom(this);
        res.setColBegin(this.getColBegin());
        res.setColEnd(this.getColEnd());
        res.setRowBegin(this.getRowBegin());
        res.setRowEnd(this.getRowEnd());
        if (content != null) 
            res.content = content.clone();
        res.isPsevdo = isPsevdo;
        res.isHeader = isHeader;
        return res;
    }

    @Override
    public boolean isInline() {
        return false;
    }


    @Override
    public String getInnerTag() {
        return "tcell";
    }


    @Override
    public UnitextItem findById(String _id) {
        if (com.pullenti.unisharp.Utils.stringsEq(id, _id)) 
            return this;
        UnitextItem res;
        if (content != null) {
            if ((((res = content.findById(_id)))) != null) 
                return res;
        }
        return null;
    }

    @Override
    public UnitextStyledFragment getStyledFragment(int textPos) {
        UnitextStyledFragment res = super.getStyledFragment(textPos);
        if (res == null) 
            return null;
        if (res.typ == UnitextStyledFragmentType.TABLECELL && res.children.size() == 1 && res.children.get(0).typ == UnitextStyledFragmentType.PARAGRAPH) 
            return res.children.get(0);
        return res;
    }

    @Override
    public UnitextItem optimize(boolean isContent, CreateDocumentParam pars) {
        if (content != null) 
            content = content.optimize(true, pars);
        if (content != null && content.isWhitespaces()) 
            content = null;
        UnitextList li = (UnitextList)com.pullenti.unisharp.Utils.cast(content, UnitextList.class);
        if ((li != null && li.items.size() == 1 && li.items.get(0).content == null) && li.items.get(0).prefix != null) 
            content = li.items.get(0).prefix;
        return this;
    }

    @Override
    public String toString() {
        return ("Cell" + (isPsevdo ? "?" : "") + " " + this.getColBegin() + (this.getColBegin() < this.getColEnd() ? ("-" + this.getColEnd()) : "") + " " + (this.getRowBegin() < this.getRowEnd() ? (" (rows " + this.getRowBegin() + "-" + this.getRowEnd() + ")") : "") + " " + (content == null ? "" : content.toString()));
    }

    @Override
    public void getPlaintext(StringBuilder res, GetPlaintextParam pars) {
        boolean nullPars = pars == null;
        int pos0 = res.length();
        if (pars != null && pars.setPositions) 
            beginChar = res.length();
        if (pars == null) 
            pars = UnitextItem.m_DefParams;
        if (content != null) {
            int i0 = res.length();
            content.getPlaintext(res, pars);
            for (int i = res.length() - 1; i >= i0; i--) {
                if ((com.pullenti.unisharp.Utils.isWhitespace(res.charAt(i)) || res.charAt(i) == ((char)7) || res.charAt(i) == '\t') || res.charAt(i) == '\f') 
                    res.setLength(i);
                else 
                    break;
            }
        }
        if (com.pullenti.unisharp.Utils.isNullOrEmpty(pars.tableCellEnd)) 
            res.append((pars.newLine != null ? pars.newLine : ""));
        else {
            if (getColBegin() < getColEnd()) {
                for (int i = 0; i < ((this.getColEnd() - this.getColBegin())); i++) {
                    res.append('\t');
                }
            }
            if (getRowBegin() < getRowEnd()) {
                for (int i = 0; i < ((this.getRowEnd() - this.getRowBegin())); i++) {
                    res.append('\f');
                }
            }
            if (pars == null) 
                res.append((char)7);
            else 
                res.append((pars.tableCellEnd != null ? pars.tableCellEnd : ""));
        }
        if (pars != null && pars.setPositions) 
            endChar = res.length() - 1;
        if (ignorable && ((nullPars || !pars.keepIgnored))) {
            for (int i = pos0; i < res.length(); i++) {
                res.setCharAt(i, ' ');
            }
        }
    }

    @Override
    public void getHtml(StringBuilder res, GetHtmlParam par) {
        if (!par.callBefore(this, res)) 
            return;
        res.append("<").append((isHeader ? "th" : "td"));
        if (getRowEnd() > getRowBegin()) 
            res.append(" rowspan=\"").append(((this.getRowEnd() - this.getRowBegin()) + 1)).append("\"");
        if (getColEnd() > getColBegin()) 
            res.append(" colspan=\"").append(((this.getColEnd() - this.getColBegin()) + 1)).append("\"");
        boolean hasValign = false;
        if (id != null) {
            res.append(" id=\"").append(id).append("\"");
            if (par != null && par.styles.containsKey(id)) {
                String sty = par.styles.get(id);
                if ((sty.indexOf("vartical-align") >= 0)) 
                    hasValign = true;
                res.append(" style=\"").append(sty).append("\"");
            }
            else if (par != null && par.outStyles && this.getStyledFragment(-1) != null) {
                UnitextStyledFragment fr = this.getStyledFragment(-1);
                if ((fr != null && ((fr.typ == UnitextStyledFragmentType.TABLECELL || fr.typ == UnitextStyledFragmentType.PARAGRAPH)) && fr.styleId > 0) && fr.getStyle() != null) {
                    if (fr.getStyle().attrs.containsKey("vertical-align")) 
                        hasValign = true;
                    res.append(" style=\"");
                    fr.getStyle().getHtml(res);
                    if (fr.parent != null && fr.parent.typ == UnitextStyledFragmentType.TABLECELL && fr.parent.getStyle() != null) 
                        fr.parent.getStyle().getHtml(res);
                    res.append("\"");
                }
            }
        }
        if (!hasValign) 
            res.append(" valign=\"top\"");
        res.append(">");
        if (content == null) 
            res.append("&nbsp;");
        else 
            content.getHtml(res, par);
        res.append((isHeader ? "</th>" : "</td>"));
        par.callAfter(this, res);
    }

    @Override
    public void getXml(com.pullenti.unisharp.XmlWriterWrapper xml) throws javax.xml.stream.XMLStreamException {
        xml.wr.writeStartElement("cell");
        this._writeXmlAttrs(xml);
        if (isPsevdo) 
            xml.wr.writeAttribute("psevdo", "true");
        if (isHeader) 
            xml.wr.writeAttribute("header", "true");
        xml.wr.writeAttribute("row", ((Integer)this.getRowBegin()).toString());
        if (getRowEnd() > getRowBegin()) 
            xml.wr.writeAttribute("rowspan", ((Integer)((this.getRowEnd() - this.getRowBegin()) + 1)).toString());
        xml.wr.writeAttribute("col", ((Integer)this.getColBegin()).toString());
        if (getColEnd() > getColBegin()) 
            xml.wr.writeAttribute("colspan", ((Integer)((this.getColEnd() - this.getColBegin()) + 1)).toString());
        if (content != null) 
            content.getXml(xml);
        xml.wr.writeEndElement();
    }

    @Override
    public void fromXml(org.w3c.dom.Node xml) throws NumberFormatException {
        super.fromXml(xml);
        if (xml.getAttributes() != null) {
            for (org.w3c.dom.Node a : (new com.pullenti.unisharp.XmlAttrListWrapper(xml.getAttributes())).arr) {
                if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(a), "row")) 
                    setRowBegin(this.setRowEnd(com.pullenti.unisharp.Utils.parseInteger(a.getNodeValue(), 0, null)));
                else if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(a), "col")) 
                    setColBegin(this.setColEnd(com.pullenti.unisharp.Utils.parseInteger(a.getNodeValue(), 0, null)));
                else if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(a), "rowspan")) 
                    setRowEnd((this.getRowBegin() + com.pullenti.unisharp.Utils.parseInteger(a.getNodeValue(), 0, null)) - 1);
                else if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(a), "colspan")) 
                    setColEnd((this.getColBegin() + com.pullenti.unisharp.Utils.parseInteger(a.getNodeValue(), 0, null)) - 1);
                else if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(a), "psevdo")) 
                    isPsevdo = true;
                else if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(a), "header")) 
                    isHeader = true;
            }
        }
        for (org.w3c.dom.Node x : (new com.pullenti.unisharp.XmlNodeListWrapper(xml.getChildNodes())).arr) {
            content = com.pullenti.unitext.internal.uni.UnitextHelper.createItem(x, this);
            if (content != null) 
                break;
        }
    }

    @Override
    public void getAllItems(java.util.ArrayList<UnitextItem> res, int lev) {
        if (res != null) 
            res.add(this);
        if (content != null) {
            content.parent = this;
            content.getAllItems(res, lev + 1);
        }
    }

    @Override
    public void addPlainTextPos(int d) {
        super.addPlainTextPos(d);
        if (content != null) 
            content.addPlainTextPos(d);
    }

    @Override
    public void correct(com.pullenti.unitext.internal.uni.LocCorrTyp typ, Object data) {
        if (content != null) 
            content.correct(typ, data);
    }

    @Override
    public boolean replaceChild(UnitextItem old, UnitextItem ne) {
        if (content != old) 
            return false;
        content = ne;
        ne.parent = this;
        return true;
    }

    @Override
    public void setDefaultTextPos(com.pullenti.unisharp.Outargwrapper<Integer> cp, StringBuilder res) {
        beginChar = cp.value;
        if (content != null) {
            content.setDefaultTextPos(cp, res);
            content.parent = this;
        }
        endChar = cp.value;
        cp.value++;
        if (res != null) 
            res.append(((char)7));
    }

    public static UnitextTablecell _new83(int _arg1, int _arg2, int _arg3, int _arg4, Object _arg5) {
        UnitextTablecell res = new UnitextTablecell();
        res.setColBegin(_arg1);
        res.setColEnd(_arg2);
        res.setRowBegin(_arg3);
        res.setRowEnd(_arg4);
        res.tag = _arg5;
        return res;
    }

    public static UnitextTablecell _new638(boolean _arg1, int _arg2, int _arg3, int _arg4, int _arg5) {
        UnitextTablecell res = new UnitextTablecell();
        res.isPsevdo = _arg1;
        res.setColBegin(_arg2);
        res.setColEnd(_arg3);
        res.setRowBegin(_arg4);
        res.setRowEnd(_arg5);
        return res;
    }

    public static UnitextTablecell _new642(UnitextItem _arg1, int _arg2, int _arg3, int _arg4, int _arg5) {
        UnitextTablecell res = new UnitextTablecell();
        res.content = _arg1;
        res.setColBegin(_arg2);
        res.setColEnd(_arg3);
        res.setRowBegin(_arg4);
        res.setRowEnd(_arg5);
        return res;
    }
    public UnitextTablecell() {
        super();
    }
}
