# 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

import datetime
import math
from pullenti.unisharp.Utils import Utils
from pullenti.unisharp.Misc import RefOutArgWrapper

from pullenti.util.MiscHelper import MiscHelper
from pullenti.unitext.UnitextStyle import UnitextStyle

class DocTextStyle:
    
    def __init__(self) -> None:
        self.name = None;
        self.aliases = None;
        self.ustyle = UnitextStyle()
        self.num_id = None;
        self.num_lvl = 0
        self.is_default = False
        self.xls_format = None;
        self.xls_border = None;
        self.xml0_ = None;
        self.based_on = None;
        self.loaded = False
    
    @property
    def is_heading(self) -> bool:
        if (self.name is not None): 
            if (Utils.startsWithString(self.name, "head", True)): 
                return True
        if (self.aliases is not None): 
            if (Utils.startsWithString(self.aliases, "head", True)): 
                return True
            if ("аголовок" in self.aliases): 
                return True
        return False
    
    def calc_heading_level(self) -> int:
        if (not self.is_heading): 
            return 0
        if (self.aliases is not None): 
            for i in range(len(self.aliases) - 1, -1, -1):
                if (not str.isdigit(self.aliases[i])): 
                    if ((i + 1) < len(self.aliases)): 
                        return int(self.aliases[i + 1:])
                    break
        if (self.name is not None): 
            for i in range(len(self.name) - 1, -1, -1):
                if (not str.isdigit(self.name[i])): 
                    if ((i + 1) < len(self.name)): 
                        return int(self.name[i + 1:])
                    break
        return 0
    
    def __str__(self) -> str:
        return self.name
    
    def correct_xls_text(self, val : str) -> str:
        if (Utils.isNullOrEmpty(val) or self.xls_format is None): 
            return val
        if (val == "6965606.333333333"): 
            pass
        if (self.xls_format == "22"): 
            ii = val.find('.')
            dat = val
            tim = None
            if (ii > 0): 
                dat = val[0:0+ii]
                tim = val[ii + 1:]
            lo = 0
            wraplo459 = RefOutArgWrapper(0)
            inoutres460 = Utils.tryParseInt(dat, wraplo459)
            lo = wraplo459.value
            if (not inoutres460): 
                return val
            dt = datetime.datetime(1900, 1, 1, 0, 0, 0)
            dt = (dt + datetime.timedelta(days=(lo) - 2))
            res = "{0}.{1}.{2}".format(dt.year, "{:02d}".format(dt.month), "{:02d}".format(dt.day))
            if (tim is None): 
                return res
            d = 0
            wrapd457 = RefOutArgWrapper(0)
            inoutres458 = MiscHelper.try_parse_double(val, wrapd457)
            d = wrapd457.value
            if (inoutres458): 
                d *= (86400)
                dt = (dt + datetime.timedelta(seconds=math.floor(d)))
                res = "{0} {1}:{2}:{3}".format(res, "{:02d}".format(dt.hour), "{:02d}".format(dt.minute), "{:02d}".format(dt.second))
            return res
        if ("yyyy" in self.xls_format): 
            d = 0
            wrapd461 = RefOutArgWrapper(0)
            inoutres462 = MiscHelper.try_parse_double(val, wrapd461)
            d = wrapd461.value
            if (inoutres462): 
                dt = datetime.datetime(1899, 12, 30, 0, 0, 0)
                dt = (dt + datetime.timedelta(days=math.floor(d)))
                d = (d - (math.floor(d)))
                d *= (86400)
                dt = (dt + datetime.timedelta(seconds=math.floor(d)))
                return MiscHelper.out_date_time(dt, False)
        if ((self.xls_format == "18" or self.xls_format == "19" or self.xls_format == "20") or self.xls_format == "21"): 
            d = 0
            wrapd463 = RefOutArgWrapper(0)
            inoutres464 = MiscHelper.try_parse_double(val, wrapd463)
            d = wrapd463.value
            if (inoutres464): 
                d *= (86400)
                dt = datetime.datetime(1900, 1, 1, 0, 0, 0)
                dt = (dt + datetime.timedelta(seconds=math.floor(d)))
                res = "{0}:{1}".format("{:02d}".format(dt.hour), "{:02d}".format(dt.minute))
                if (self.xls_format == "19" or self.xls_format == "21"): 
                    res = "{0}:{1}".format(res, "{:02d}".format(dt.second))
                return res
        if ((self.xls_format == "14" or self.xls_format == "15" or self.xls_format == "6") or self.xls_format == "17" or "yy" in self.xls_format): 
            lo = 0
            wraplo465 = RefOutArgWrapper(0)
            inoutres466 = Utils.tryParseInt(val, wraplo465)
            lo = wraplo465.value
            if (not inoutres466): 
                return val
            dt = datetime.datetime(1900, 1, 1, 0, 0, 0)
            dt = (dt + datetime.timedelta(days=(lo) - 2))
            return "{0}.{1}.{2}".format(dt.year, "{:02d}".format(dt.month), "{:02d}".format(dt.day))
        if (self.xls_format == "9" or self.xls_format == "10"): 
            d = 0
            wrapd467 = RefOutArgWrapper(0)
            inoutres468 = MiscHelper.try_parse_double(val, wrapd467)
            d = wrapd467.value
            if (inoutres468): 
                return "{0}%".format(round(d * (100), 2))
        if ("##" in self.xls_format): 
            delim = '.'
            nn = 0
            if ("#.##" in self.xls_format): 
                nn = 2
                if ("#.###" in self.xls_format): 
                    nn = 3
            elif ("#,##" in self.xls_format): 
                delim = ','
                nn = 2
                if ("#,###" in self.xls_format): 
                    nn = 3
            if (nn > 0): 
                d = 0
                wrapd469 = RefOutArgWrapper(0)
                inoutres470 = MiscHelper.try_parse_double(val, wrapd469)
                d = wrapd469.value
                if (inoutres470): 
                    d = round(d, nn)
                    val1 = MiscHelper.out_double(d)
                    ii = val1.find('.')
                    if (ii < 0): 
                        val1 += (".00" if nn == 2 else ".000")
                    else: 
                        kk = len(val1) - ii
                        while kk < nn: 
                            val1 += "0"
                            kk += 1
                    if (delim == ','): 
                        val1 = val1.replace('.', ',')
                    return val1
        return val