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

// Используется для встраивания узлов в HTML-тексты
public class HtmlCorrector {

    public HtmlCorrector(String _html) {
        m_Html = _html;
        m_Impls = new java.util.HashMap<String, HtmlNode>();
        this._parse();
        if (m_Text == null) 
            m_Text = "";
    }

    private String m_Html;

    private HtmlNode m_Root;

    private java.util.HashMap<String, HtmlNode> m_Impls;

    private void _parse() {
        StringBuilder tmp = new StringBuilder(m_Html);
        m_Root = HtmlParser.parse(tmp, true);
        if (m_Root != null) {
            if (com.pullenti.unisharp.Utils.stringsEq(m_Root.tagName, "HTML")) 
                m_Root.tagName = "DIV";
            if (!com.pullenti.unisharp.Utils.isNullOrEmpty(m_Root.text) && m_Root.tagName == null) {
                HtmlNode hh = HtmlNode._new1("span");
                hh.children.add(m_Root);
                m_Root.parent = hh;
                m_Root = hh;
            }
            this._createText();
        }
    }

    private void _createText() {
        com.pullenti.unitext.GetPlaintextParam pars = com.pullenti.unitext.GetPlaintextParam._new2("\n", "  ", "\n\n");
        m_Text = m_Root.getPlaintext(pars);
    }

    public String getPlainText() {
        return m_Text;
    }


    private String m_Text;

    public String getHtmlText() {
        if (m_Root == null) 
            return "";
        StringBuilder res = new StringBuilder();
        m_Root.getHtml(res);
        return res.toString();
    }


    public boolean setText(int beginChar, int endChar, String newText) {
        if (m_Root == null || m_Text == null) 
            return false;
        if (beginChar > m_Text.length() || endChar > m_Text.length() || beginChar > endChar) 
            return false;
        HtmlNode nod = m_Root.implantateNode(beginChar, endChar, "span");
        if (nod == null) 
            return false;
        if (com.pullenti.unisharp.Utils.isNullOrEmpty(newText)) {
            if (nod.parent != null && nod.parent.children.contains(nod)) {
                nod.parent.children.remove(nod);
                this._createText();
                return true;
            }
            return false;
        }
        if (nod.children != null && nod.children.size() > 0) 
            nod.children.clear();
        nod.text = newText;
        nod.tagName = null;
        this._createText();
        return true;
    }

    public boolean implantateNode(int beginChar, int endChar, String tagName, String id, java.util.HashMap<String, String> attrs) {
        if ((m_Root == null || com.pullenti.unisharp.Utils.isNullOrEmpty(tagName) || (beginChar < 0)) || beginChar > endChar || endChar >= m_Text.length()) 
            return false;
        if (id == null) 
            return false;
        if (m_Impls.containsKey(id)) 
            this.removeNode(id);
        for (; (beginChar < endChar) && (beginChar < m_Text.length()); beginChar++) {
            if (!com.pullenti.unisharp.Utils.isWhitespace(m_Text.charAt(beginChar))) 
                break;
        }
        for (; endChar > beginChar; endChar--) {
            if (!com.pullenti.unisharp.Utils.isWhitespace(m_Text.charAt(endChar))) 
                break;
        }
        HtmlNode nod = m_Root.implantateNode(beginChar, endChar, tagName);
        if (nod == null) 
            return false;
        nod.attrs = new java.util.HashMap<String, String>();
        nod.attrs.put("ID", id);
        if (attrs != null) {
            for (java.util.Map.Entry<String, String> kp : attrs.entrySet()) {
                if (!nod.attrs.containsKey(kp.getKey().toUpperCase())) 
                    nod.attrs.put(kp.getKey().toUpperCase(), kp.getValue());
            }
        }
        m_Impls.put(id, nod);
        return true;
    }

    public boolean replaceText(String oldText, String newText, String tagName, String id, java.util.HashMap<String, String> attrs) {
        if ((m_Root == null || com.pullenti.unisharp.Utils.isNullOrEmpty(tagName))) 
            return false;
        if (id == null) 
            return false;
        if (m_Impls.containsKey(id)) 
            this.removeNode(id);
        int bChar = m_Text.indexOf(oldText);
        int eChar = (bChar + oldText.length()) - 1;
        HtmlNode nod = m_Root.replaceTextRange(bChar, eChar, newText, tagName, null);
        if (nod == null) 
            return false;
        nod.attrs = new java.util.HashMap<String, String>();
        nod.attrs.put("ID", id);
        if (attrs != null) {
            for (java.util.Map.Entry<String, String> kp : attrs.entrySet()) {
                if (!nod.attrs.containsKey(kp.getKey().toUpperCase())) 
                    nod.attrs.put(kp.getKey().toUpperCase(), kp.getValue());
            }
        }
        m_Impls.put(id, nod);
        return true;
    }

    public boolean removeNode(String id) {
        if (m_Impls.containsKey(id)) {
            m_Impls.get(id).remove();
            m_Impls.remove(id);
            if (m_Impls.size() == 0) 
                this._parse();
            return true;
        }
        else 
            return false;
    }

    public HtmlNode findNode(String tagName, String attrName, String attrValue) {
        if (m_Root == null) 
            return null;
        return m_Root.findSubnode(tagName, attrName, attrValue);
    }

    public void clearAll() {
        for (java.util.Map.Entry<String, HtmlNode> kp : m_Impls.entrySet()) {
            kp.getValue().remove();
        }
        m_Impls.clear();
        this._parse();
    }
    public HtmlCorrector() {
    }
}
