/*
 * 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 Paragraph implements Comparable<Paragraph> {

    private WordDocument owner;

    private int _offset;

    public int getOffset() {
        return _offset;
    }

    public int setOffset(int value) {
        _offset = value;
        return _offset;
    }


    public int getPos() {
        return getFileCharacterPosition().characterIndex + getOffset();
    }


    private int _length;

    public int getLength() {
        return _length;
    }

    public int setLength(int value) {
        _length = value;
        return _length;
    }


    private WordDocument.FileCharacterPosition _filecharacterposition;

    public WordDocument.FileCharacterPosition getFileCharacterPosition() {
        return _filecharacterposition;
    }

    public WordDocument.FileCharacterPosition setFileCharacterPosition(WordDocument.FileCharacterPosition value) {
        _filecharacterposition = value;
        return _filecharacterposition;
    }


    private PapxInFkps _papxinfkps;

    public PapxInFkps getPapxInFkps() {
        return _papxinfkps;
    }

    public PapxInFkps setPapxInFkps(PapxInFkps value) {
        _papxinfkps = value;
        return _papxinfkps;
    }


    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        res.append("Pos:").append((this.getFileCharacterPosition().characterIndex + this.getOffset())).append(", Len:").append(this.getLength());
        if (isInTable()) 
            res.append(" InTable (").append(this.getTableDepth()).append(")");
        if (isList()) 
            res.append(" IsList (").append(this.getListLevel()).append(")");
        if (isTableCellEnd()) 
            res.append(" TableCellEnd");
        if (isTableRowEnd()) 
            res.append(" TableRowEnd");
        res.append(" ").append(this.getStyles(FormattingLevel.ALL));
        if (getPapxInFkps() != null && getPapxInFkps().grpprlInPapx != null && getPapxInFkps().grpprlInPapx.grpprl != null) {
            for (Prl p : getPapxInFkps().grpprlInPapx.grpprl) {
                res.append(" ").append(((String)com.pullenti.unisharp.Utils.notnull(SinglePropertyModifiers.getSprmName(p.sprm.sprm), "")));
            }
        }
        for (Prl p : getFileCharacterPosition().prls) {
            res.append(" ").append(((String)com.pullenti.unisharp.Utils.notnull(SinglePropertyModifiers.getSprmName(p.sprm.sprm), "")));
        }
        return res.toString();
    }

    public boolean isInTable() {
        byte[] data = this.getProperty(SinglePropertyModifiers.sprmPFInTable);
        return data != null && data[0] != ((byte)0);
    }


    public int getTableDepth() {
        byte[] data = this.getProperty(SinglePropertyModifiers.sprmPItap);
        if (data == null) 
            return 0;
        else 
            return (int)java.nio.ByteBuffer.wrap(data, 0, 4).order(java.nio.ByteOrder.LITTLE_ENDIAN).getInt();
    }


    public boolean isTableRowEnd() {
        int _pos = (getFileCharacterPosition().characterIndex + getOffset() + getLength()) - 1;
        if ((_pos < owner.text.length()) && owner.text.charAt(_pos) == '\u0007') {
            byte[] data = this.getProperty(SinglePropertyModifiers.sprmPFTtp);
            if (data != null && data[0] != ((byte)0)) 
                return true;
        }
        else {
            byte[] data = this.getProperty(SinglePropertyModifiers.sprmPFInnerTtp);
            if (data != null && data[0] != ((byte)0)) 
                return true;
        }
        return false;
    }


    public boolean isTableCellEnd() {
        int _pos = (getFileCharacterPosition().characterIndex + getOffset() + getLength()) - 1;
        if ((_pos < owner.text.length()) && owner.text.charAt(_pos) == '\u0007') 
            return true;
        else {
            byte[] data = this.getProperty(SinglePropertyModifiers.sprmPFInnerTableCell);
            return data != null && data[0] != ((byte)0);
        }
    }


    public boolean isList() {
        byte[] data = this.getProperty(SinglePropertyModifiers.sprmPIlfo);
        return data != null && java.nio.ByteBuffer.wrap(data, 0, 2).order(java.nio.ByteOrder.LITTLE_ENDIAN).getShort() != ((short)0);
    }


    public int getListLevel() {
        byte[] data = this.getProperty(SinglePropertyModifiers.sprmPIlvl);
        return Byte.toUnsignedInt((data != null ? data[0] : (byte)0));
    }


    public StyleDefinition getStyle() {
        return owner.getStyleDefinitionsMap().get(this.getPapxInFkps().grpprlInPapx.istd);
    }


    public Paragraph(WordDocument _owner, int __offset, int __length, WordDocument.FileCharacterPosition fcp, PapxInFkps _papxInFkps) {
        this.owner = _owner;
        this.setOffset(__offset);
        this.setLength(__length);
        this.setFileCharacterPosition(fcp);
        this.setPapxInFkps(_papxInFkps);
    }

    public byte[] getProperty(short sprm) {
        if (getPapxInFkps() != null && getPapxInFkps().grpprlInPapx != null && getPapxInFkps().grpprlInPapx.grpprl != null) {
            for (Prl p : getPapxInFkps().grpprlInPapx.grpprl) {
                if (Integer.compareUnsigned(p.sprm.sprm, sprm) == 0) 
                    return p.operand;
            }
        }
        for (Prl p : getFileCharacterPosition().prls) {
            if (Integer.compareUnsigned(p.sprm.sprm, sprm) == 0) 
                return p.operand;
        }
        return null;
    }

    public StyleCollection getStyles(FormattingLevel level) {
        if (level != FormattingLevel.PARAGRAPH && level != FormattingLevel.PARAGRAPHSTYLE) 
            return null;
        java.util.ArrayList<Prl[]> prls = new java.util.ArrayList<Prl[]>();
        prls.add(this.getPapxInFkps().grpprlInPapx.grpprl);
        if (level == FormattingLevel.PARAGRAPHSTYLE) 
            this.getStyle().expandStyles(prls);
        return new StyleCollection(prls);
    }

    @Override
    public int compareTo(Paragraph other) {
        if (getPos() < other.getPos()) 
            return -1;
        if (getPos() > other.getPos()) 
            return 1;
        if (getLength() < other.getLength()) 
            return -1;
        if (getLength() > other.getLength()) 
            return 1;
        return 0;
    }
    public Paragraph() {
    }
}
