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

// для корректировки beginchar и endchar у стилевых фрагментов
public class ChangeTextPosInfo {

    private com.pullenti.unitext.UnitextItem[] bMap = null;

    private com.pullenti.unitext.UnitextItem[] eMap = null;

    private com.pullenti.unitext.UnitextStyledFragment m_Root;

    public ChangeTextPosInfo(com.pullenti.unitext.UnitextStyledFragment fr, com.pullenti.unitext.UnitextItem root) {
        if (fr.endChar == 0) 
            return;
        bMap = new com.pullenti.unitext.UnitextItem[fr.endChar + 1];
        eMap = new com.pullenti.unitext.UnitextItem[fr.endChar + 1];
        java.util.ArrayList<com.pullenti.unitext.UnitextItem> its = new java.util.ArrayList<com.pullenti.unitext.UnitextItem>();
        root.getAllItems(its, 0);
        for (com.pullenti.unitext.UnitextItem it : its) {
            if (it.endChar > 0 && it.beginChar <= it.endChar && (it.endChar < bMap.length)) {
                bMap[it.beginChar] = it;
                eMap[it.endChar] = it;
            }
        }
        m_Root = fr;
    }

    private int[] bPos;

    private int[] ePos;

    public void restore(int newLen) {
        if (bMap == null) 
            return;
        bPos = new int[bMap.length];
        ePos = new int[bMap.length];
        int p = 0;
        for (int i = 0; i < bMap.length; i++) {
            com.pullenti.unitext.UnitextItem it = bMap[i];
            if (it != null) {
                if (it.beginChar < p) {
                }
                p = it.beginChar;
            }
            bPos[i] = p;
            p++;
        }
        p = newLen - 1;
        for (int i = eMap.length - 1; i >= 0; i--) {
            com.pullenti.unitext.UnitextItem it = eMap[i];
            if (it != null) {
                if (p > 0 && it.endChar > p) {
                }
                p = it.endChar;
            }
            ePos[i] = p;
            p--;
        }
        this._restore(m_Root);
        com.pullenti.unitext.UnitextStyledFragment ch = m_Root;
        for (int kk = 0; kk < 2; kk++) {
            if ((ch.endChar + 1) < newLen) 
                ch.endChar = newLen - 1;
            if (ch.children.size() == 0) 
                break;
            ch = ch.children.get(ch.children.size() - 1);
        }
    }

    private void _restore(com.pullenti.unitext.UnitextStyledFragment fr) {
        if (fr.beginChar >= 0 && (fr.beginChar < bPos.length)) {
            if (bPos[fr.beginChar] < 0) {
            }
            fr.beginChar = bPos[fr.beginChar];
        }
        if (fr.endChar >= 0 && (fr.endChar < ePos.length)) {
            if (ePos[fr.endChar] < 0) {
            }
            fr.endChar = ePos[fr.endChar];
        }
        for (int i = 0; i < fr.children.size(); i++) {
            com.pullenti.unitext.UnitextStyledFragment ch = fr.children.get(i);
            this._restore(ch);
            if (ch.endChar > fr.endChar) 
                fr.endChar = ch.endChar;
            if (ch.beginChar < fr.beginChar) 
                fr.beginChar = ch.beginChar;
            if (i > 0) {
            }
        }
        for (int i = 0; i < fr.children.size(); i++) {
            com.pullenti.unitext.UnitextStyledFragment ch = fr.children.get(i);
            if (ch.beginChar > ch.endChar) {
                if (i > 0 && ((i + 1) < fr.children.size()) && (fr.children.get(i - 1).endChar + 1) == fr.children.get(i + 1).beginChar) {
                    fr.children.remove(i);
                    i--;
                    continue;
                }
            }
        }
        for (int i = 0; i < (fr.children.size() - 1); i++) {
            com.pullenti.unitext.UnitextStyledFragment ch = fr.children.get(i);
            com.pullenti.unitext.UnitextStyledFragment ch2 = fr.children.get(i + 1);
            if ((ch.endChar > ch2.beginChar && ch.text != null && ch.text.trim().length() == 0) && ch2.text != null && ch2.text.trim().length() == 0) {
                String txt0 = ch.text + ch2.text;
                for (int j = i + 2; j < fr.children.size(); j++) {
                    ch2 = fr.children.get(j);
                    if ((ch.endChar + 1) == ch2.beginChar) {
                        for(int jjj = i + 1 + j - i - 1 - 1, mmm = i + 1; jjj >= mmm; jjj--) fr.children.remove(jjj);
                        ch.text = txt0;
                        break;
                    }
                    if (ch2.text == null || ch2.text.trim().length() > 0) 
                        break;
                    txt0 += ch2.text;
                }
            }
        }
    }
    public ChangeTextPosInfo() {
    }
}
