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

namespace Pullenti.Unitext.Internal.Word
{
    /// <summary>
    /// Это блок данных
    /// </summary>
    class DocxPart
    {
        public string Name;
        public XmlNode Xml;
        public byte[] Data;
        public bool IsName(string name)
        {
            if (string.Compare(name, Name, true) == 0) 
                return true;
            if (Name.StartsWith("/")) 
            {
                if (string.Compare(name, Name.Substring(1), true) == 0) 
                    return true;
            }
            return false;
        }
        public bool IsNameStarts(string name)
        {
            if (Name.StartsWith(name, StringComparison.OrdinalIgnoreCase)) 
                return true;
            return false;
        }
        public Pullenti.Unitext.Internal.Misc.MyZipEntry ZipEntry;
        public override string ToString()
        {
            return Name;
        }
        static bool _startOfIns(byte[] dat, int i)
        {
            if ((i + 7) >= dat.Length) 
                return false;
            if (((dat[i] == '<' && dat[i + 1] == 'w' && dat[i + 2] == ':') && dat[i + 3] == 'i' && dat[i + 4] == 'n') && dat[i + 5] == 's') 
            {
                if (dat[i + 6] == ' ' || dat[i + 6] == '>') 
                    return true;
            }
            if (((dat[i] == '<' && dat[i + 1] == '/' && dat[i + 2] == 'w') && dat[i + 3] == ':' && dat[i + 4] == 'i') && dat[i + 5] == 'n' && dat[i + 6] == 's') 
            {
                if (dat[i + 7] == ' ' || dat[i + 7] == '>') 
                    return true;
            }
            return false;
        }
        public XmlNode GetXmlNode(bool correct = false)
        {
            if (Xml != null) 
                return Xml;
            if (Data != null) 
                return null;
            XmlDocument xml = new XmlDocument();
            byte[] dat = ZipEntry.GetData();
            if (dat == null) 
                return null;
            bool hasIns = false;
            for (int i = 0; i < (dat.Length - 6); i++) 
            {
                if (_startOfIns(dat, i)) 
                {
                    hasIns = true;
                    break;
                }
            }
            if (hasIns) 
            {
                List<byte> buf = new List<byte>(dat.Length);
                for (int i = 0; i < dat.Length; i++) 
                {
                    if (!_startOfIns(dat, i)) 
                        buf.Add(dat[i]);
                    else 
                        for (; i < dat.Length; i++) 
                        {
                            if (dat[i] == '>') 
                                break;
                        }
                }
                dat = buf.ToArray();
            }
            using (MemoryStream documentXml = new MemoryStream(dat)) 
            {
                xml.PreserveWhitespace = true;
                xml.Load(documentXml);
                return xml.DocumentElement;
            }
        }
        static string _getTagName(string xml, int i)
        {
            for (int j = i; j < xml.Length; j++) 
            {
                if (!char.IsLetter(xml[j]) && xml[j] != ':') 
                {
                    if (j == i) 
                        return null;
                    return xml.Substring(i, j - i);
                }
            }
            return null;
        }
        public byte[] GetBytes()
        {
            if (Data != null) 
                return Data;
            if (Xml != null) 
                return null;
            // всё-равно эти картинки не поддержаны браузером, а то на больших документах памяти не хватает
            if (ZipEntry.Name.EndsWith(".emf", StringComparison.OrdinalIgnoreCase)) 
                return null;
            try 
            {
                return ZipEntry.GetData();
            }
            catch(Exception ex) 
            {
                return null;
            }
        }
    }
}