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

namespace Pullenti.Unitext.Internal.Word
{
    class DocNumStyles
    {
        Dictionary<string, Pullenti.Unitext.Internal.Uni.UnitextGenNumStyleEx> m_NumStyles = new Dictionary<string, Pullenti.Unitext.Internal.Uni.UnitextGenNumStyleEx>();
        public Pullenti.Unitext.Internal.Uni.UnitextGenNumStyleEx GetStyle(string id)
        {
            if (id == null) 
                return null;
            if (m_NumStyles.ContainsKey(id)) 
                return m_NumStyles[id];
            return null;
        }
        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;
                }
            }
            if (nam == "val") 
            {
                if (x.Attributes != null) 
                {
                    foreach (XmlAttribute a in x.Attributes) 
                    {
                        return a.Value;
                    }
                }
            }
            return null;
        }
        void _readNumLvl(Pullenti.Unitext.Internal.Uni.UniTextGenNumLevel nlev, XmlNode xx)
        {
            foreach (XmlNode xxx in xx.ChildNodes) 
            {
                if (xxx.Attributes != null) 
                {
                    if (xxx.LocalName == "numFmt") 
                        nlev.Type = GetNumTyp(_readAttrVal(xxx, "val", null));
                    else if (xxx.LocalName == "lvlText") 
                    {
                        nlev.Format = _readAttrVal(xxx, "val", null);
                        StringBuilder tmp = new StringBuilder();
                        int max = 0;
                        for (int i = 0; i < nlev.Format.Length; i++) 
                        {
                            char ch = nlev.Format[i];
                            if (ch != '%' || (i + 1) >= nlev.Format.Length) 
                            {
                                tmp.Append(ch);
                                continue;
                            }
                            i++;
                            int n = (int)((nlev.Format[i] - '0'));
                            if (n <= max || n > 9) 
                                continue;
                            max = n;
                            tmp.AppendFormat("%{0}", n);
                        }
                        nlev.Format = tmp.ToString();
                    }
                    else if (xxx.LocalName == "start") 
                    {
                        int ii;
                        if (int.TryParse(_readAttrVal(xxx, "val", null) ?? "", out ii)) 
                            nlev.Start = ii;
                    }
                }
                if (xxx.LocalName == "pPr") 
                {
                    if (nlev.Style == null) 
                        nlev.Style = new Pullenti.Unitext.UnitextStyle();
                    DocTextStyles.ReadUnitextStyleExt(xxx, nlev.Style, true, false);
                }
            }
        }
        public void ReadAllStyles(XmlNode node)
        {
            Dictionary<string, Pullenti.Unitext.Internal.Uni.UnitextGenNumStyle> abstr = new Dictionary<string, Pullenti.Unitext.Internal.Uni.UnitextGenNumStyle>();
            foreach (XmlNode xnum in node.ChildNodes) 
            {
                if (xnum.LocalName == "abstractNum" && xnum.Attributes != null && xnum.Attributes.Count >= 1) 
                {
                    string id = _readAttrVal(xnum, "abstractNumId", null);
                    if (id == null || abstr.ContainsKey(id)) 
                        continue;
                    if (id == "14") 
                    {
                    }
                    Pullenti.Unitext.Internal.Uni.UnitextGenNumStyle nsty = new Pullenti.Unitext.Internal.Uni.UnitextGenNumStyle() { Id = id };
                    abstr.Add(id, nsty);
                    foreach (XmlNode xx in xnum.ChildNodes) 
                    {
                        if (xx.LocalName == "lvl") 
                        {
                            Pullenti.Unitext.Internal.Uni.UniTextGenNumLevel nlev = new Pullenti.Unitext.Internal.Uni.UniTextGenNumLevel();
                            nsty.Levels.Add(nlev);
                            this._readNumLvl(nlev, xx);
                        }
                        else if (xx.LocalName == "numStyleLink") 
                        {
                            string lid = _readAttrVal(xx, "val", null);
                            if (lid != null && abstr.ContainsKey(lid)) 
                                nsty.Levels.AddRange(abstr[lid].Levels);
                        }
                    }
                }
                else if (xnum.LocalName == "num") 
                {
                    string id = _readAttrVal(xnum, "numId", null);
                    string intId = null;
                    foreach (XmlNode xx in xnum.ChildNodes) 
                    {
                        if (xx.LocalName == "abstractNumId") 
                            intId = _readAttrVal(xx, "val", null);
                    }
                    if (intId == null || id == null) 
                        continue;
                    Pullenti.Unitext.Internal.Uni.UnitextGenNumStyle num0;
                    if (!abstr.TryGetValue(intId, out num0)) 
                        continue;
                    Pullenti.Unitext.Internal.Uni.UnitextGenNumStyleEx num = new Pullenti.Unitext.Internal.Uni.UnitextGenNumStyleEx() { Src = num0.Clone() };
                    num.Id = id;
                    if (!m_NumStyles.ContainsKey(id)) 
                        m_NumStyles.Add(id, num);
                    foreach (XmlNode xx in xnum.ChildNodes) 
                    {
                        if (xx.LocalName == "lvlOverride") 
                        {
                            int l;
                            if (!int.TryParse(_readAttrVal(xx, "val", null) ?? "", out l)) 
                                continue;
                            if ((l < 0) || l >= num.Src.Levels.Count) 
                                continue;
                            foreach (XmlNode xxx in xx.ChildNodes) 
                            {
                                if (xxx.LocalName == "startOverride") 
                                {
                                    int s;
                                    if (int.TryParse(_readAttrVal(xxx, "val", null) ?? "", out s)) 
                                    {
                                        if (!num.OverrideStarts.ContainsKey(l)) 
                                            num.OverrideStarts.Add(l, s);
                                    }
                                }
                                else if (xxx.LocalName == "lvl") 
                                    this._readNumLvl(num.Src.Levels[l], xxx);
                            }
                        }
                    }
                }
            }
        }
        internal static Pullenti.Unitext.Internal.Uni.UnitextGenNumStyle ReadNumberStyle(XmlNode xml)
        {
            Pullenti.Unitext.Internal.Uni.UnitextGenNumStyle res = null;
            foreach (XmlNode x in xml.ChildNodes) 
            {
                if (x.LocalName == "ilvl") 
                {
                    res = new Pullenti.Unitext.Internal.Uni.UnitextGenNumStyle();
                    int nn;
                    if (int.TryParse(_readAttrVal(x, "val", null) ?? "", out nn)) 
                        res.Lvl = nn;
                }
                else if (x.LocalName == "ilfo") 
                {
                    string vv = _readAttrVal(x, "val", null);
                    if (vv == "2") 
                        res.IsBullet = true;
                }
                else if (x.LocalName == "t" && res != null) 
                    res.Txt = _readAttrVal(x, "val", null);
            }
            return res;
        }
        internal static Pullenti.Unitext.Internal.Uni.UniTextGenNumType GetNumTyp(string ty)
        {
            if (ty == "bullet") 
                return Pullenti.Unitext.Internal.Uni.UniTextGenNumType.Bullet;
            if (ty.Contains("decimal")) 
                return Pullenti.Unitext.Internal.Uni.UniTextGenNumType.Decimal;
            if (ty == "lowerLetter") 
                return Pullenti.Unitext.Internal.Uni.UniTextGenNumType.LowerLetter;
            if (ty == "russianLower") 
                return Pullenti.Unitext.Internal.Uni.UniTextGenNumType.LowerCyrLetter;
            if (ty == "russianUpper") 
                return Pullenti.Unitext.Internal.Uni.UniTextGenNumType.UpperCyrLetter;
            if (ty == "upperLetter") 
                return Pullenti.Unitext.Internal.Uni.UniTextGenNumType.UpperLetter;
            if (ty == "lowerRoman") 
                return Pullenti.Unitext.Internal.Uni.UniTextGenNumType.LowerRoman;
            if (ty.Contains("oman")) 
                return Pullenti.Unitext.Internal.Uni.UniTextGenNumType.UpperRoman;
            return Pullenti.Unitext.Internal.Uni.UniTextGenNumType.Decimal;
        }
    }
}