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

namespace Pullenti.Unitext.Internal.Word
{
    class DocTextStyle
    {
        public string Name;
        public string Aliases;
        public bool IsHeading
        {
            get
            {
                if (Name != null) 
                {
                    if (Name.StartsWith("head", StringComparison.OrdinalIgnoreCase)) 
                        return true;
                }
                if (Aliases != null) 
                {
                    if (Aliases.StartsWith("head", StringComparison.OrdinalIgnoreCase)) 
                        return true;
                    if (Aliases.Contains("аголовок")) 
                        return true;
                }
                return false;
            }
        }
        public int CalcHeadingLevel()
        {
            if (!IsHeading) 
                return 0;
            if (Aliases != null) 
            {
                for (int i = Aliases.Length - 1; i >= 0; i--) 
                {
                    if (!char.IsDigit(Aliases[i])) 
                    {
                        if ((i + 1) < Aliases.Length) 
                            return int.Parse(Aliases.Substring(i + 1));
                        break;
                    }
                }
            }
            if (Name != null) 
            {
                for (int i = Name.Length - 1; i >= 0; i--) 
                {
                    if (!char.IsDigit(Name[i])) 
                    {
                        if ((i + 1) < Name.Length) 
                            return int.Parse(Name.Substring(i + 1));
                        break;
                    }
                }
            }
            return 0;
        }
        public Pullenti.Unitext.UnitextStyle UStyle = new Pullenti.Unitext.UnitextStyle();
        public string NumId;
        public int NumLvl;
        public bool IsDefault;
        public string XlsFormat;
        public Pullenti.Unitext.Internal.Misc.BorderInfo XlsBorder;
        public XmlNode Xml;
        public string BasedOn;
        public bool Loaded;
        public override string ToString()
        {
            return Name;
        }
        public string CorrectXlsText(string val)
        {
            if (string.IsNullOrEmpty(val) || XlsFormat == null) 
                return val;
            if (val == "6965606.333333333") 
            {
            }
            if (XlsFormat == "22") 
            {
                int ii = val.IndexOf('.');
                string dat = val;
                string tim = null;
                if (ii > 0) 
                {
                    dat = val.Substring(0, ii);
                    tim = val.Substring(ii + 1);
                }
                long lo;
                if (!long.TryParse(dat, out lo)) 
                    return val;
                DateTime dt = new DateTime(1900, 1, 1);
                dt = dt.AddDays(((int)lo) - 2);
                string res = string.Format("{0}.{1}.{2}", dt.Year, dt.Month.ToString("D02"), dt.Day.ToString("D02"));
                if (tim == null) 
                    return res;
                double d;
                if (Pullenti.Util.MiscHelper.TryParseDouble(val, out d)) 
                {
                    d *= 86400;
                    dt = dt.AddSeconds((int)d);
                    res = string.Format("{0} {1}:{2}:{3}", res, dt.Hour.ToString("D02"), dt.Minute.ToString("D02"), dt.Second.ToString("D02"));
                }
                return res;
            }
            if (XlsFormat.Contains("yyyy")) 
            {
                double d;
                if (Pullenti.Util.MiscHelper.TryParseDouble(val, out d)) 
                {
                    DateTime dt = new DateTime(1899, 12, 30);
                    dt = dt.AddDays((int)d);
                    d = d - ((int)d);
                    d *= 86400;
                    dt = dt.AddSeconds((int)d);
                    return Pullenti.Util.MiscHelper.OutDateTime(dt, false);
                }
            }
            if ((XlsFormat == "18" || XlsFormat == "19" || XlsFormat == "20") || XlsFormat == "21") 
            {
                double d;
                if (Pullenti.Util.MiscHelper.TryParseDouble(val, out d)) 
                {
                    d *= 86400;
                    DateTime dt = new DateTime(1900, 1, 1);
                    dt = dt.AddSeconds((int)d);
                    string res = string.Format("{0}:{1}", dt.Hour.ToString("D02"), dt.Minute.ToString("D02"));
                    if (XlsFormat == "19" || XlsFormat == "21") 
                        res = string.Format("{0}:{1}", res, dt.Second.ToString("D02"));
                    return res;
                }
            }
            if ((XlsFormat == "14" || XlsFormat == "15" || XlsFormat == "6") || XlsFormat == "17" || XlsFormat.Contains("yy")) 
            {
                long lo;
                if (!long.TryParse(val, out lo)) 
                    return val;
                DateTime dt = new DateTime(1900, 1, 1);
                dt = dt.AddDays(((int)lo) - 2);
                return string.Format("{0}.{1}.{2}", dt.Year, dt.Month.ToString("D02"), dt.Day.ToString("D02"));
            }
            if (XlsFormat == "9" || XlsFormat == "10") 
            {
                double d;
                if (Pullenti.Util.MiscHelper.TryParseDouble(val, out d)) 
                    return string.Format("{0}%", Math.Round(d * 100, 2));
            }
            if (XlsFormat.Contains("##")) 
            {
                char delim = '.';
                int nn = 0;
                if (XlsFormat.Contains("#.##")) 
                {
                    nn = 2;
                    if (XlsFormat.Contains("#.###")) 
                        nn = 3;
                }
                else if (XlsFormat.Contains("#,##")) 
                {
                    delim = ',';
                    nn = 2;
                    if (XlsFormat.Contains("#,###")) 
                        nn = 3;
                }
                if (nn > 0) 
                {
                    double d;
                    if (Pullenti.Util.MiscHelper.TryParseDouble(val, out d)) 
                    {
                        d = Math.Round(d, nn);
                        string val1 = Pullenti.Util.MiscHelper.OutDouble(d);
                        int ii = val1.IndexOf('.');
                        if (ii < 0) 
                            val1 += (nn == 2 ? ".00" : ".000");
                        else 
                        {
                            int kk = val1.Length - ii;
                            for (; kk < nn; kk++) 
                            {
                                val1 += "0";
                            }
                        }
                        if (delim == ',') 
                            val1 = val1.Replace('.', ',');
                        return val1;
                    }
                }
            }
            return val;
        }
    }
}