/*
 * 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 UnilayRectangle = require("./UnilayRectangle");

/**
 * Страница тексто-графического слоя
 */
class UnilayPage {
    
    constructor() {
        this.number = 0;
        this.width = 0;
        this.height = 0;
        this.imageContent = null;
        this.rects = new Array();
        this.topNumber = 0;
        this.bottomNumber = 0;
        this.pageTitle = null;
        this.tag = null;
    }
    
    toString() {
        return ("Page " + this.number + " (" + this.width + "x" + this.height + ")" + (this.imageContent === null ? "" : " has image"));
    }
    
    getXml(xml) {
        xml.writeStartElement("page");
        if (this.number > 0) 
            xml.writeAttributeString("number", this.number.toString());
        if (this.topNumber > 0) 
            xml.writeAttributeString("topnumber", this.topNumber.toString());
        if (this.bottomNumber > 0) 
            xml.writeAttributeString("bottomnumber", this.bottomNumber.toString());
        if (this.width > 0) 
            xml.writeAttributeString("width", this.width.toString());
        if (this.height > 0) 
            xml.writeAttributeString("height", this.height.toString());
        if (this.imageContent !== null) 
            xml.writeElementString("image", Utils.encodeBase64(this.imageContent));
        for (const r of this.rects) {
            r.getXml(xml);
        }
        xml.writeEndElement();
    }
    
    restore(xml) {
        let d = 0;
        if (xml.attributes !== null) {
            for (const a of xml.attributes) {
                if (a.local_name === "number") {
                    let i = 0;
                    let wrapi565 = new RefOutArgWrapper();
                    let inoutres566 = Utils.tryParseInt(a.value, wrapi565);
                    i = wrapi565.value;
                    if (inoutres566) 
                        this.number = i;
                }
                else if (a.local_name === "topnumber") {
                    let i = 0;
                    let wrapi567 = new RefOutArgWrapper();
                    let inoutres568 = Utils.tryParseInt(a.value, wrapi567);
                    i = wrapi567.value;
                    if (inoutres568) 
                        this.topNumber = i;
                }
                else if (a.local_name === "bottomnumber") {
                    let i = 0;
                    let wrapi569 = new RefOutArgWrapper();
                    let inoutres570 = Utils.tryParseInt(a.value, wrapi569);
                    i = wrapi569.value;
                    if (inoutres570) 
                        this.bottomNumber = i;
                }
                else if (a.local_name === "width") {
                    let i = 0;
                    let wrapi571 = new RefOutArgWrapper();
                    let inoutres572 = Utils.tryParseInt(a.value, wrapi571);
                    i = wrapi571.value;
                    if (inoutres572) 
                        this.width = i;
                }
                else if (a.local_name === "height") {
                    let i = 0;
                    let wrapi573 = new RefOutArgWrapper();
                    let inoutres574 = Utils.tryParseInt(a.value, wrapi573);
                    i = wrapi573.value;
                    if (inoutres574) 
                        this.height = i;
                }
            }
        }
        for (const x of xml.child_nodes) {
            if (x.local_name === "rect") {
                let r = UnilayRectangle._new575(this);
                r.restore(x);
                this.rects.push(r);
            }
            else if (x.local_name === "image") 
                this.imageContent = Utils.decodeBase64(x.inner_text);
        }
    }
    
    /**
     * Найти по координате накрывающий её прямоугольник
     * @param x 
     * @param y 
     * @return 
     */
    findRect(x, y) {
        for (const r of this.rects) {
            if ((r.left <= x && x <= r.right && r.top <= y) && y <= r.bottom) 
                return r;
        }
        return null;
    }
    
    // Объединить результат распознавания с распознаванием страницы другим движком,
    // взяв наилучшие Rects по их качеству Quality.
    // page - страница с таким же изображением</param>
    mergeRectsByQuality(page) {
        for (const r of page.rects) {
            r.tag = null;
        }
        let maxLine = 0;
        for (const r of this.rects) {
            if (r.lineNumber > maxLine) 
                maxLine = r.lineNumber;
            let rr = page.findRect(((r.left + r.right)) / (2), ((r.top + r.bottom)) / (2));
            if (rr === null) 
                continue;
            rr.tag = r;
            if ((r.quality.value()) >= (rr.quality.value())) 
                continue;
            r.text = rr.text;
            r.quality = rr.quality;
            r.left = rr.left;
            r.top = rr.top;
            r.right = rr.right;
            r.bottom = rr.bottom;
        }
    }
    
    static _new158(_arg1) {
        let res = new UnilayPage();
        res.imageContent = _arg1;
        return res;
    }
}


module.exports = UnilayPage