/*
 * 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 UnitextHyperlink extends UnitextItem {

    public UnitextHyperlink() {
        super();
    }

    /**
     * Сама ссылка
     */
    public String href;

    /**
     * Содержимое ссылки
     * 
     */
    public UnitextItem content;

    /**
     * Это внутренняя ссылка, Href - якорь на элемент СТП (совпадает с Id элемента)
     */
    public boolean isInternal;

    /**
     * Используйте произвольным образом, сериализуется
     */
    public String data;

    @Override
    public String toString() {
        return ("Hyperlink " + ((href != null ? href : "")) + " (" + ((String)com.pullenti.unisharp.Utils.notnull(this.getPlaintextString(null), "")) + ")");
    }

    @Override
    public UnitextItem clone() {
        UnitextHyperlink res = new UnitextHyperlink();
        res._cloneFrom(this);
        res.href = href;
        res.data = data;
        res.isInternal = isInternal;
        if (content != null) 
            res.content = content.clone();
        return res;
    }

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


    @Override
    public boolean isInline() {
        if (content == null) 
            return true;
        return content.isInline();
    }


    @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 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 void getPlaintext(StringBuilder res, GetPlaintextParam pars) {
        int pos0 = res.length();
        if (pars != null && pars.setPositions) 
            beginChar = res.length();
        if (content != null) 
            content.getPlaintext(res, pars);
        if (pars == null) 
            pars = UnitextItem.m_DefParams;
        if (!com.pullenti.unisharp.Utils.isNullOrEmpty(pars.hyperlinksTemplate) && href != null && !isInternal) {
            StringBuilder tmp = new StringBuilder();
            for (int i = beginChar; i < res.length(); i++) {
                tmp.append(res.charAt(i));
            }
            String txt = tmp.toString();
            if (tmp.length() == 0) 
                res.append(href);
            else if (com.pullenti.unisharp.Utils.stringsCompare(href, txt, true) != 0) 
                res.append(pars.hyperlinksTemplate.replace("%1", href));
        }
        if (pars != null && pars.setPositions) 
            endChar = res.length() - 1;
        if (pars != null && !pars.keepIgnored && ((pars.ignoreLevel > 0 || ignorable))) {
            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;
        String tit = null;
        if (htmlTitle != null || href != null) {
            StringBuilder tmp = new StringBuilder();
            com.pullenti.util.MiscHelper.correctHtmlValue(tmp, (htmlTitle != null ? htmlTitle : href), true, false, false);
            tit = tmp.toString();
        }
        if (isInternal) 
            res.append("<i>");
        if (href != null) {
            res.append("<a href=\"");
            if (isInternal) 
                res.append('#');
            com.pullenti.util.MiscHelper.correctHtmlValue(res, href, true, false, false);
            res.append("\" title=\"").append(((tit != null ? tit : ""))).append("\"").append(((par.hyperlinksTargetBlank || href.startsWith("http") ? " target=\"_blank\"" : "")));
        }
        else 
            res.append("<span style=\"text-decoration:underline\" title=\"").append(((tit != null ? tit : ""))).append("\"");
        if (id != null) 
            res.append(" id=\"").append(id).append("\"");
        res.append(">");
        if (content != null) 
            content.getHtml(res, par);
        if (href != null) 
            res.append("</a>");
        else 
            res.append("</span>");
        if (isInternal) 
            res.append("</i>");
        par.callAfter(this, res);
    }

    @Override
    public void getXml(com.pullenti.unisharp.XmlWriterWrapper xml) throws javax.xml.stream.XMLStreamException {
        xml.wr.writeStartElement("hyperlink");
        this._writeXmlAttrs(xml);
        if (href != null) {
            xml.wr.writeAttribute("href", com.pullenti.util.MiscHelper.correctXmlValue(href));
            if (isInternal) 
                xml.wr.writeAttribute("internal", "true");
        }
        if (data != null) 
            xml.wr.writeAttribute("data", com.pullenti.util.MiscHelper.correctXmlValue(data));
        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), "href")) 
                    href = a.getNodeValue();
                else if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(a), "internal")) 
                    isInternal = com.pullenti.unisharp.Utils.stringsEq(a.getNodeValue(), "true");
                else if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(a), "data")) 
                    data = a.getNodeValue();
            }
        }
        for (org.w3c.dom.Node x : (new com.pullenti.unisharp.XmlNodeListWrapper(xml.getChildNodes())).arr) {
            content = com.pullenti.unitext.internal.uni.UnitextHelper.createItem(x, this);
            break;
        }
    }

    @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 UnitextItem optimize(boolean isContent, CreateDocumentParam pars) {
        if (content != null) 
            content = content.optimize(true, pars);
        if (pars != null && href != null) {
            if (((href.length() > 2 && href.charAt(0) == '<' && href.charAt(2) == '>')) || ((href.length() > 3 && href.charAt(0) == '<' && href.charAt(3) == '>'))) {
                String _tag = href.substring(0, 0 + (href.charAt(2) == '>' ? 3 : 4));
                UnitextPlaintext pt = (UnitextPlaintext)com.pullenti.unisharp.Utils.cast(content, UnitextPlaintext.class);
                if (pt != null && pt.text.startsWith(_tag)) {
                    UnitextFootnote foot = new UnitextFootnote();
                    if (href.charAt(2) == '>') 
                        foot.customMark = href.substring(1, 1 + 1);
                    else 
                        foot.customMark = href.substring(1, 1 + 2);
                    String txt = href.substring(_tag.length()).trim();
                    if ((txt.indexOf("\\\"") >= 0)) 
                        txt = txt.replace("\\\"", "\"");
                    foot.content = UnitextPlaintext._new57(txt);
                    return foot;
                }
                if (content instanceof UnitextFootnote) 
                    return content;
            }
        }
        return this;
    }

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

    public static UnitextHyperlink _new59(String _arg1) {
        UnitextHyperlink res = new UnitextHyperlink();
        res.href = _arg1;
        return res;
    }

    public static UnitextHyperlink _new509(boolean _arg1, String _arg2) {
        UnitextHyperlink res = new UnitextHyperlink();
        res.isInternal = _arg1;
        res.href = _arg2;
        return res;
    }
}
