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

using System;
using System.Collections;
using System.Collections.Generic;
using System.Xml;

namespace Pullenti.Unitext
{
    /// <summary>
    /// Элемент сегмента страниц
    /// </summary>
    public class UnitextPagesectionItem : UnitextItem
    {
        /// <summary>
        /// Footer или Header
        /// </summary>
        public bool IsFooter;
        /// <summary>
        /// К каким страницам относится
        /// </summary>
        public UnitextPagesectionItemPages Pages = UnitextPagesectionItemPages.Default;
        /// <summary>
        /// Содержимое
        /// </summary>
        public UnitextItem Content;
        public override string ToString()
        {
            return string.Format("{0} pages={1}", (IsFooter ? "Footer" : "Header"), Pages.ToString().ToLower());
        }
        public override UnitextItem Clone()
        {
            UnitextPagesectionItem res = new UnitextPagesectionItem();
            res._cloneFrom(this);
            res.IsFooter = IsFooter;
            res.Pages = Pages;
            if (Content != null) 
            {
                res.Content = Content.Clone();
                res.Content.Parent = res;
            }
            return res;
        }
        public override void GetXml(XmlWriter xml)
        {
            xml.WriteStartElement("sectionitem");
            this._writeXmlAttrs(xml);
            xml.WriteAttributeString("type", (IsFooter ? "footer" : "header"));
            xml.WriteAttributeString("pages", Pages.ToString().ToLower());
            if (Content != null) 
            {
                xml.WriteStartElement("content");
                Content.GetXml(xml);
                xml.WriteEndElement();
                if (Content.m_StyledFrag != null) 
                    Content.m_StyledFrag.GetXml(xml, null, false);
            }
            xml.WriteEndElement();
        }
        public override void FromXml(XmlNode xml)
        {
            UnitextDocument doc = null;
            for (UnitextItem it = (UnitextItem)this; it != null; it = it.Parent) 
            {
                if (it is UnitextDocument) 
                    doc = it as UnitextDocument;
            }
            base.FromXml(xml);
            foreach (XmlAttribute a in xml.Attributes) 
            {
                if (a.LocalName == "type") 
                    IsFooter = a.Value == "footer";
                else if (a.LocalName == "pages") 
                {
                    try 
                    {
                        Pages = (UnitextPagesectionItemPages)Enum.Parse(typeof(UnitextPagesectionItemPages), a.Value, true);
                    }
                    catch(Exception ex408) 
                    {
                    }
                }
            }
            foreach (XmlNode x in xml.ChildNodes) 
            {
                if (x.LocalName == "content") 
                {
                    foreach (XmlNode xx in x.ChildNodes) 
                    {
                        Content = Pullenti.Unitext.Internal.Uni.UnitextHelper.CreateItem(xx, this);
                        if (Content != null) 
                            Content.Parent = this;
                        break;
                    }
                }
                else if (x.LocalName == "stylefrag" && Content != null) 
                {
                    Content.m_StyledFrag = new UnitextStyledFragment();
                    Content.m_StyledFrag.Doc = doc;
                    Content.m_StyledFrag.FromXml(x);
                }
            }
        }
        public override void GetAllItems(List<UnitextItem> res, int lev)
        {
            if (res != null) 
                res.Add(this);
            if (Content != null) 
            {
                Content.Parent = this;
                Content.GetAllItems(res, lev + 1);
            }
        }
    }
}