/*
 * 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 MiscHelper = require("./../util/MiscHelper");
const UnitextItem = require("./UnitextItem");
const UnitextPagesectionItem = require("./UnitextPagesectionItem");

/**
 * Информация о страницах и колонтитулах.
 * Сегменты страниц
 */
class UnitextPagesection extends UnitextItem {
    
    constructor() {
        super();
        this.width = 0;
        this.height = 0;
        this.left = 0;
        this.top = 0;
        this.right = 0;
        this.bottom = 0;
        this.headerHeight = 0;
        this.footerHeight = 0;
        this.items = new Array();
    }
    
    clone() {
        let res = new UnitextPagesection();
        res._cloneFrom(this);
        res.width = this.width;
        res.height = this.height;
        res.left = this.left;
        res.right = this.right;
        res.top = this.top;
        res.bottom = this.bottom;
        res.headerHeight = this.headerHeight;
        res.footerHeight = this.footerHeight;
        for (const it of this.items) {
            res.items.push(Utils.as(it.clone(), UnitextPagesectionItem));
        }
        for (const it of res.items) {
            it.parent = res;
        }
        if (this.m_StyledFrag !== null) 
            res.m_StyledFrag = this.m_StyledFrag.clone();
        return res;
    }
    
    toString() {
        return ("Section " + this.id + ": " + MiscHelper.outDouble(this.width) + "x" + MiscHelper.outDouble(this.height));
    }
    
    getXml(xml) {
        xml.writeStartElement("section");
        this._writeXmlAttrs(xml);
        if (this.width > 0) 
            xml.writeAttributeString("width", MiscHelper.outDouble(this.width));
        if (this.height > 0) 
            xml.writeAttributeString("height", MiscHelper.outDouble(this.height));
        if (this.left > 0) 
            xml.writeAttributeString("left", MiscHelper.outDouble(this.left));
        if (this.top > 0) 
            xml.writeAttributeString("top", MiscHelper.outDouble(this.top));
        if (this.right > 0) 
            xml.writeAttributeString("right", MiscHelper.outDouble(this.right));
        if (this.bottom > 0) 
            xml.writeAttributeString("bottom", MiscHelper.outDouble(this.bottom));
        if (this.headerHeight > 0) 
            xml.writeAttributeString("header", MiscHelper.outDouble(this.headerHeight));
        if (this.footerHeight > 0) 
            xml.writeAttributeString("footer", MiscHelper.outDouble(this.footerHeight));
        for (const it of this.items) {
            it.getXml(xml);
        }
        xml.writeEndElement();
    }
    
    _parseDouble(str) {
        let d = 0;
        let wrapd617 = new RefOutArgWrapper();
        let inoutres618 = MiscHelper.tryParseDouble(str, wrapd617);
        d = wrapd617.value;
        if (inoutres618) 
            return d;
        return 0;
    }
    
    fromXml(xml) {
        super.fromXml(xml);
        for (const a of xml.attributes) {
            if (a.local_name === "width") 
                this.width = this._parseDouble(a.value);
            else if (a.local_name === "height") 
                this.height = this._parseDouble(a.value);
            else if (a.local_name === "left") 
                this.left = this._parseDouble(a.value);
            else if (a.local_name === "top") 
                this.top = this._parseDouble(a.value);
            else if (a.local_name === "right") 
                this.right = this._parseDouble(a.value);
            else if (a.local_name === "bottom") 
                this.bottom = this._parseDouble(a.value);
            else if (a.local_name === "header") 
                this.headerHeight = this._parseDouble(a.value);
            else if (a.local_name === "footer") 
                this.footerHeight = this._parseDouble(a.value);
        }
        for (const x of xml.child_nodes) {
            if (x.local_name === "item") {
                let it = new UnitextPagesectionItem();
                it.parent = this;
                it.fromXml(x);
                this.items.push(it);
            }
        }
    }
    
    getAllItems(res, lev) {
        if (res !== null) 
            res.push(this);
        for (const it of this.items) {
            it.parent = this;
            it.getAllItems(res, lev + 1);
        }
    }
}


module.exports = UnitextPagesection