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

const Utils = require("./../unisharp/Utils");
const RefOutArgWrapper = require("./../unisharp/RefOutArgWrapper");
const StringBuilder = require("./../unisharp/StringBuilder");

const FileFormat = require("./FileFormat");
const MiscHelper = require("./../util/MiscHelper");
const UnitextItem = require("./UnitextItem");
const FileFormatsHelper = require("./../util/FileFormatsHelper");

/**
 * Картинка
 */
class UnitextImage extends UnitextItem {
    
    constructor() {
        super();
        this.content = null;
        this.width = null;
        this.height = null;
        this.rect = null;
        this.htmlSrcUri = null;
    }
    
    clone() {
        let res = new UnitextImage();
        res._cloneFrom(this);
        res.content = this.content;
        res.width = this.width;
        res.height = this.height;
        res.rect = this.rect;
        res.htmlSrcUri = this.htmlSrcUri;
        return res;
    }
    
    get innerTag() {
        return "img";
    }
    
    toString() {
        let tmp = new StringBuilder();
        tmp.append("Image");
        if (this.width !== null || this.height !== null) 
            tmp.append(" ").append(((this.width != null ? this.width : "?"))).append(" x ").append(((this.height != null ? this.height : "?")));
        if (this.content !== null) {
            let frm = FileFormatsHelper.analizeFormat(null, this.content);
            if (frm !== FileFormat.UNKNOWN) 
                tmp.append(" ").append(frm.toString().toUpperCase());
            tmp.append(" ").append(this.content.length).append("bytes");
        }
        else if (this.htmlSrcUri !== null) 
            tmp.append(" ").append(this.htmlSrcUri);
        return tmp.toString();
    }
    
    getPlaintext(res, pars) {
        if (pars !== null && pars.setPositions) 
            this.beginChar = res.length;
        let templ = (pars === null ? "[Картинка%1]" : pars.imageTemplate);
        res.append(Utils.replaceString(templ, "%1", (this.content === null ? "" : this.content.length.toString())));
        if (pars !== null && pars.setPositions) 
            this.endChar = this.beginChar;
    }
    
    getHtml(res, par) {
        if (par !== null && !par.callBefore(this, res)) 
            return;
        res.append("<img");
        if (this.id !== null) 
            res.append(" id=\"").append(this.id).append("\"");
        res.append(" style=\"max-width:100%;");
        if (this.width !== null && this.width.endsWith("pt")) {
            let nn = 0;
            let wrapnn608 = new RefOutArgWrapper();
            let inoutres609 = Utils.tryParseInt(this.width.substring(0, 0 + this.width.length - 2), wrapnn608);
            nn = wrapnn608.value;
            if (inoutres609) {
                if (nn > 1000) 
                    res.append("width:100%;");
            }
        }
        let over = false;
        if (this.content === null) 
            res.append("border:2pt solid red;");
        else if (this.content.length > 10000000 || ((par !== null && this.content.length > par.maxImageSize)) || ((res.length > 40000000 && this.content.length > 10000))) {
            res.append("border:2pt solid red;");
            over = true;
        }
        let wi = 0;
        let hi = 0;
        if (this.width !== null) {
            for (let i = 0; i < this.width.length; i++) {
                if (!Utils.isDigit(this.width[i])) 
                    break;
                else 
                    wi = Utils.parseInt(this.width.substring(0, 0 + i + 1));
            }
        }
        if (this.height !== null) {
            for (let i = 0; i < this.height.length; i++) {
                if (!Utils.isDigit(this.height[i])) 
                    break;
                else 
                    hi = Utils.parseInt(this.height.substring(0, 0 + i + 1));
            }
        }
        if ((wi < 500) && (hi < 500)) {
            if (this.width !== null) 
                res.append("width:").append(this.width).append(";");
            if (this.height !== null) 
                res.append("height:").append(this.height).append(";");
        }
        res.append("\"");
        if (this.content !== null) {
            if (over) 
                res.append(" alt=\"Image too large to show here (").append((Utils.intDiv(this.content.length, 1024))).append("Kb)\"");
            else {
                let frm = FileFormatsHelper.analizeFormat(null, this.content);
                if (frm === FileFormat.JPG2000) 
                    res.append(" title=\"Image format JPEG 2000 not supported\"");
                else {
                    let base64 = Utils.encodeBase64(this.content);
                    if (base64.indexOf('\n') >= 0) 
                        base64 = Utils.replaceString(base64, "\n", "");
                    let str = null;
                    if (frm !== FileFormat.UNKNOWN) {
                        str = FileFormatsHelper.getFormatExt(frm);
                        if (str.startsWith(".")) 
                            str = str.substring(1);
                    }
                    if (str === null) 
                        str = "png";
                    if (str === "tif") 
                        str = "tiff";
                    let src = ("data:image/" + str + ";base64,") + base64;
                    res.append(" src=\"").append(src).append("\"");
                }
            }
        }
        else 
            res.append(" src=\"").append(((this.htmlSrcUri != null ? this.htmlSrcUri : "undefined"))).append("\"");
        res.append("/>");
        if (par !== null) 
            par.callAfter(this, res);
    }
    
    getXml(xml) {
        xml.writeStartElement("image");
        this._writeXmlAttrs(xml);
        if (this.width !== null) 
            xml.writeAttributeString("width", this.width);
        if (this.height !== null) 
            xml.writeAttributeString("height", this.height);
        if (this.htmlSrcUri !== null) 
            xml.writeAttributeString("uri", MiscHelper.correctXmlValue(this.htmlSrcUri));
        if (this.content !== null) {
            try {
                let dat = Utils.encodeBase64(this.content);
                let tmp = new StringBuilder();
                let i = 0;
                for (const ch of dat) {
                    if (Utils.isWhitespace(ch)) 
                        continue;
                    tmp.append(ch);
                    if ((++i) >= 100) {
                        i = 0;
                        tmp.append("\r\n");
                    }
                }
                xml.writeString(tmp.toString());
            } catch (ex) {
                ;
            }
        }
        xml.writeEndElement();
    }
    
    fromXml(xml) {
        super.fromXml(xml);
        if (xml.attributes !== null) {
            for (const a of xml.attributes) {
                if (a.local_name === "width") 
                    this.width = a.value;
                else if (a.local_name === "height") 
                    this.height = a.value;
                else if (a.local_name === "uri") 
                    this.htmlSrcUri = a.value;
            }
        }
        try {
            let txt = xml.inner_text;
            if (txt !== null) 
                this.content = Utils.decodeBase64(txt);
        } catch (ex610) {
        }
    }
    
    setDefaultTextPos(cp, res) {
        this.beginChar = (this.endChar = cp.value);
        cp.value++;
        if (res !== null) 
            res.append(' ');
    }
    
    static _new96(_arg1) {
        let res = new UnitextImage();
        res.id = _arg1;
        return res;
    }
    
    static _new277(_arg1) {
        let res = new UnitextImage();
        res.content = _arg1;
        return res;
    }
    
    static _new354(_arg1, _arg2, _arg3) {
        let res = new UnitextImage();
        res.content = _arg1;
        res.rect = _arg2;
        res.pageSectionId = _arg3;
        return res;
    }
    
    static _new355(_arg1) {
        let res = new UnitextImage();
        res.pageSectionId = _arg1;
        return res;
    }
}


module.exports = UnitextImage