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

namespace Pullenti.Unitext.Internal.Pdf
{
    public class PdfArray : PdfObject
    {
        List<PdfObject> m_Items = new List<PdfObject>();
        public int ItemsCount
        {
            get
            {
                return m_Items.Count;
            }
        }
        public PdfObject GetItem(int ind, bool keepRef = false)
        {
            if ((ind < 0) || ind >= m_Items.Count) 
                return null;
            if ((m_Items[ind] is PdfReference) && !keepRef) 
            {
                PdfObject obj = SourceFile.GetObject(m_Items[ind].Id);
                if (obj == null) 
                    return null;
                m_Items[ind] = obj;
            }
            return m_Items[ind];
        }
        public void RemoveItem(int ind)
        {
            if (ind >= 0 && (ind < m_Items.Count)) 
                m_Items.RemoveAt(ind);
        }
        public override bool IsSimple(int lev)
        {
            if (m_Items.Count > 10 || lev > 5) 
                return false;
            for (int i = 0; i < m_Items.Count; i++) 
            {
                PdfObject it = this.GetItem(i, false);
                if (it != null && !it.IsSimple(lev + 1)) 
                    return false;
            }
            return true;
        }
        public void Add(PdfObject obj)
        {
            m_Items.Add(obj);
        }
        public void Clear()
        {
            m_Items.Clear();
        }
        public override string ToStringEx(int lev)
        {
            if (lev > 10) 
                return string.Format("[...{0}]", ItemsCount);
            StringBuilder res = new StringBuilder();
            res.Append("[");
            int i;
            for (i = 0; i < m_Items.Count; i++) 
            {
                if (i > 0) 
                    res.Append(", ");
                if (res.Length > 100) 
                    break;
                PdfObject it = this.GetItem(i, false);
                if (it == null) 
                    res.Append("NULL");
                else if (!it.IsSimple(lev + 1)) 
                    break;
                else 
                {
                    string str = it.ToStringEx(lev + 1);
                    if (str.Length < 20) 
                    {
                        res.Append(str);
                        continue;
                    }
                    else 
                        break;
                }
            }
            if (i < m_Items.Count) 
                res.AppendFormat("... {0}", m_Items.Count);
            res.Append("]");
            return res.ToString();
        }
        public override void OutHtml(StringBuilder res)
        {
            res.Append("<b>[</b> ");
            for (int i = 0; i < ItemsCount; i++) 
            {
                PdfObject o = this.GetItem(i, false);
                if (o == null) 
                {
                    res.Append("null");
                    continue;
                }
                if (o.Id > 0) 
                    res.AppendFormat("<a href=\"obj{0}\">", o.Id);
                Pullenti.Util.MiscHelper.CorrectHtmlValue(res, o.ToString(), false, false, false);
                if (o.Id > 0) 
                    res.Append("</a>");
                if ((i + 1) < ItemsCount) 
                {
                    res.Append(", ");
                    if ((o is PdfDictionary) || (o is PdfArray)) 
                        res.Append("<br/>");
                }
            }
            res.Append("<b> ]</b>");
            this._outHtmlRefs(res);
        }
        internal void PostParse(PdfStream stream)
        {
            m_Items.Clear();
            int p0 = (int)stream.Position;
            byte ch = (byte)stream.ReadByte();
            if (ch != '[') 
                return;
            while (true) 
            {
                int i = stream.PeekSolidByte();
                if (i < 0) 
                    break;
                ch = (byte)i;
                if (ch == ']') 
                {
                    stream.Position += 1;
                    return;
                }
                PdfObject obj = stream.ParseObject(SourceFile, false);
                if (obj == null) 
                    break;
                m_Items.Add(obj);
            }
        }
        public override double GetDouble()
        {
            if (m_Items.Count < 1) 
                return 0;
            PdfObject it = this.GetItem(0, false);
            if (it == null) 
                return 0;
            return it.GetDouble();
        }
        public override void Write(Stream s)
        {
            s.WriteByte((byte)'[');
            for (int i = 0; i < m_Items.Count; i++) 
            {
                if (i > 0) 
                    s.WriteByte((byte)' ');
                PdfObject it = this.GetItem(i, false);
                if (it == null) 
                    continue;
                if (it.Deleted) 
                    continue;
                if (it.Id == 0) 
                    it.Write(s);
                else 
                {
                    byte[] buf = Encoding.ASCII.GetBytes(string.Format("{0} 0 R", it.WriteId));
                    s.Write(buf, 0, buf.Length);
                }
            }
            s.WriteByte((byte)']');
        }
        public override void GetAllReferences(List<PdfReference> refs, List<int> ids)
        {
            if (Id > 0) 
            {
                if (ids.Contains(Id)) 
                    return;
                ids.Add(Id);
            }
            for (int i = 0; i < ItemsCount; i++) 
            {
                PdfObject o = this.GetItem(i, true);
                if (o != null) 
                    o.GetAllReferences(refs, ids);
            }
            if (Id > 0) 
                ids.Remove(Id);
        }
        public override void GetAllWhoRefObject(int objId, PdfObject cur, List<PdfObject> res, List<int> ids)
        {
            if (Id > 0) 
            {
                if (ids.Contains(Id)) 
                    return;
                ids.Add(Id);
                cur = this;
            }
            for (int i = 0; i < ItemsCount; i++) 
            {
                PdfObject o = this.GetItem(i, true);
                if (o != null && o.Id == objId) 
                {
                    if (!res.Contains(cur)) 
                        res.Add(cur);
                }
                else if (o != null && o.Id == 0) 
                    o.GetAllWhoRefObject(objId, cur, res, ids);
            }
            if (Id > 0) 
                ids.Remove(Id);
        }
    }
}