/*
 * 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.Text;
using System.Xml;

namespace Pullenti.Unitext
{
    /// <summary>
    /// Разрыв страниц
    /// </summary>
    public class UnitextPagebreak : UnitextItem
    {
        /// <summary>
        /// Предположительный тип (вычисляется в ходе анализа)
        /// </summary>
        public WhitespaceType Typ = WhitespaceType.Original;
        /// <summary>
        /// Для PDF это номер страницы, который заканчивает этот разделитель (1 - первая страница)
        /// </summary>
        public int PageNumber;
        public UnitextPagebreak() : base()
        {
        }
        public override string ToString()
        {
            string res = "PageBreak";
            if (PageNumber > 0) 
                res = string.Format("PageBreak N={0}", PageNumber);
            if (Typ == WhitespaceType.Ignorable) 
                res += " - ignore";
            else if (Typ == WhitespaceType.Onespace) 
                res += " - space";
            return res;
        }
        public override UnitextItem Clone()
        {
            UnitextPagebreak res = new UnitextPagebreak();
            res._cloneFrom(this);
            res.Typ = Typ;
            res.PageNumber = PageNumber;
            return res;
        }
        public override void GetPlaintext(StringBuilder res, GetPlaintextParam pars)
        {
            if (pars != null && pars.SetPositions) 
                BeginChar = res.Length;
            if (Typ == WhitespaceType.Ignorable) 
            {
            }
            else if (Typ == WhitespaceType.Onespace) 
                res.Append(' ');
            else 
            {
                res.Append(((pars ?? UnitextItem.m_DefParams)).NewLine ?? "");
                res.Append(((pars ?? UnitextItem.m_DefParams)).PageBreak ?? "");
            }
            if (pars != null && pars.SetPositions) 
                EndChar = res.Length - 1;
        }
        public override void GetHtml(StringBuilder res, GetHtmlParam par)
        {
            if (Typ == WhitespaceType.Ignorable) 
                return;
            if (Typ == WhitespaceType.Onespace) 
            {
                res.Append(' ');
                return;
            }
            if (!par.CallBefore(this, res)) 
                return;
            int k = 0;
            for (UnitextItem p = Parent; p != null && (k < 10); p = p.Parent,k++) 
            {
                UnitextContainer cnt = p as UnitextContainer;
                if (cnt != null) 
                {
                    if (cnt.Typ != UnitextContainerType.Undefined && cnt.Typ != UnitextContainerType.Head) 
                    {
                        res.Append("\r\n<BR/>");
                        par.CallAfter(this, res);
                        return;
                    }
                }
                UnitextDocblock db = p as UnitextDocblock;
                if (db != null) 
                {
                    if (string.Compare(db.Typname ?? "", "Document", true) == 0 || string.Compare(db.Typname ?? "", "Subdocument", true) == 0 || string.Compare(db.Typname ?? "", "Appendix", true) == 0) 
                    {
                    }
                    else if (db.Typname != null) 
                    {
                        res.Append("\r\n<BR/>");
                        par.CallAfter(this, res);
                        return;
                    }
                }
            }
            if (par != null) 
                par._outFootnotes(res);
            res.Append("\r\n<HR");
            if (PageNumber > 0) 
                res.AppendFormat(" title=\"End of page {0}\"", PageNumber);
            res.Append("/><br/></br>");
            par.CallAfter(this, res);
        }
        public override void GetXml(XmlWriter xml)
        {
            xml.WriteStartElement("pagebreak");
            this._writeXmlAttrs(xml);
            if (Typ != WhitespaceType.Original) 
                xml.WriteAttributeString("typ", Typ.ToString().ToLower());
            if (PageNumber > 0) 
                xml.WriteAttributeString("pagenum", PageNumber.ToString());
            xml.WriteEndElement();
        }
        public override void FromXml(XmlNode xml)
        {
            base.FromXml(xml);
            if (xml.Attributes != null) 
            {
                foreach (XmlAttribute a in xml.Attributes) 
                {
                    if (a.LocalName == "typ") 
                    {
                        try 
                        {
                            Typ = (WhitespaceType)Enum.Parse(typeof(WhitespaceType), a.Value, true);
                        }
                        catch(Exception ex407) 
                        {
                        }
                    }
                    else if (a.LocalName == "pagenum") 
                        PageNumber = int.Parse(a.Value);
                }
            }
        }
        public override bool IsWhitespaces
        {
            get
            {
                return true;
            }
        }
        public override bool IsInline
        {
            get
            {
                if (Typ != WhitespaceType.Original) 
                    return true;
                return false;
            }
            set
            {
            }
        }
        internal override void SetDefaultTextPos(ref int cp, StringBuilder res)
        {
            BeginChar = (EndChar = cp);
            cp++;
            if (res != null) 
                res.Append('\f');
        }
    }
}