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

    private WordDocument owner;

    private int _offset;

    public int getOffset() {
        return _offset;
    }

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


    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;
    }


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


    private Chpx _chpx;

    public Chpx getChpx() {
        return _chpx;
    }

    public Chpx setChpx(Chpx value) {
        _chpx = value;
        return _chpx;
    }


    @Override
    public String toString() {
        return ("Pos:" + this.getPos() + " Len:" + this.getLength() + " Styles:" + this.getStyles(FormattingLevel.CHARACTER).toString());
    }

    public StyleDefinition getStyle() {
        byte[] istdData = this.getProperty(SinglePropertyModifiers.sprmCIstd);
        if (istdData != null) {
            short istd = java.nio.ByteBuffer.wrap(istdData, 0, 2).order(java.nio.ByteOrder.LITTLE_ENDIAN).getShort();
            if (owner.getStyleDefinitionsMap().containsKey(istd)) 
                return owner.getStyleDefinitionsMap().get(istd);
        }
        return null;
    }


    public CharacterFormatting(WordDocument _owner, int __offset, int __length, WordDocument.FileCharacterPosition fcp, Chpx __chpx) {
        this.owner = _owner;
        this.setOffset(__offset);
        this.setLength(__length);
        this.setFileCharacterPosition(fcp);
        this.setChpx(__chpx);
    }

    public byte[] getProperty(short sprm) {
        for (Prl p : getChpx().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) {
        java.util.ArrayList<Prl[]> prls = new java.util.ArrayList<Prl[]>();
        prls.add(this.getChpx().grpprl);
        if (level == FormattingLevel.CHARACTERSTYLE) {
            StyleDefinition def = this.getStyle();
            if (def != null) 
                def.expandStyles(prls);
        }
        return new StyleCollection(prls);
    }
    public CharacterFormatting() {
    }
}
