/*
 * 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.Internal.Word
{
    class DocSection
    {
        public Pullenti.Unitext.UnitextPagesection USect = new Pullenti.Unitext.UnitextPagesection();
        public bool titlePg;
        public bool Loaded = false;
        public Dictionary<Pullenti.Unitext.UnitextPagesectionItemPages, string> HeadIds = new Dictionary<Pullenti.Unitext.UnitextPagesectionItemPages, string>();
        public Dictionary<Pullenti.Unitext.UnitextPagesectionItemPages, string> FootIds = new Dictionary<Pullenti.Unitext.UnitextPagesectionItemPages, string>();
        static double _readDoubleCm(string str)
        {
            double d;
            if (!Pullenti.Util.MiscHelper.TryParseDouble(str, out d)) 
                return 0;
            d /= 20;
            d *= 2.54;
            d /= 72;
            return Math.Round(d, 2);
        }
        static string _readAttrVal(XmlNode x, string nam, string nam2 = null)
        {
            if (x.Attributes != null) 
            {
                foreach (XmlAttribute a in x.Attributes) 
                {
                    if (a.LocalName == nam) 
                        return a.Value;
                    else if (nam2 != null && a.LocalName == nam2) 
                        return a.Value;
                }
            }
            return null;
        }
        public void Load(XmlNode xml)
        {
            Loaded = true;
            foreach (XmlNode x in xml.ChildNodes) 
            {
                if (x.LocalName == "pgSz") 
                {
                    USect.Width = _readDoubleCm(_readAttrVal(x, "w", null));
                    USect.Height = _readDoubleCm(_readAttrVal(x, "h", null));
                }
                else if (x.LocalName == "pgMar") 
                {
                    USect.Left = _readDoubleCm(_readAttrVal(x, "left", null));
                    USect.Right = _readDoubleCm(_readAttrVal(x, "right", null));
                    USect.Top = _readDoubleCm(_readAttrVal(x, "top", null));
                    USect.Bottom = _readDoubleCm(_readAttrVal(x, "bottom", null));
                    USect.HeaderHeight = _readDoubleCm(_readAttrVal(x, "header", null));
                    USect.FooterHeight = _readDoubleCm(_readAttrVal(x, "footer", null));
                }
                else if (x.LocalName == "titlePg") 
                    titlePg = true;
                else if (x.LocalName == "headerReference" || x.LocalName == "footerReference") 
                {
                    string id = _readAttrVal(x, "id", null);
                    string typ = _readAttrVal(x, "type", null);
                    Pullenti.Unitext.UnitextPagesectionItemPages ptyp = Pullenti.Unitext.UnitextPagesectionItemPages.Default;
                    if (string.Compare(typ, "first", true) == 0) 
                        ptyp = Pullenti.Unitext.UnitextPagesectionItemPages.First;
                    else if (string.Compare(typ, "even", true) == 0) 
                        ptyp = Pullenti.Unitext.UnitextPagesectionItemPages.Even;
                    else if (string.Compare(typ, "odd", true) == 0) 
                        ptyp = Pullenti.Unitext.UnitextPagesectionItemPages.Odd;
                    if (x.LocalName == "headerReference") 
                    {
                        if (!HeadIds.ContainsKey(ptyp)) 
                            HeadIds.Add(ptyp, id);
                    }
                    else if (!FootIds.ContainsKey(ptyp)) 
                        FootIds.Add(ptyp, id);
                }
            }
        }
    }
}