# 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 io
import typing
from pullenti.unisharp.Utils import Utils
from pullenti.unisharp.Streams import Stream

from pullenti.util.MiscHelper import MiscHelper
from pullenti.unitext.internal.pdf.PdfObject import PdfObject
from pullenti.unitext.internal.pdf.PdfReference import PdfReference

class PdfArray(PdfObject):
    
    def __init__(self) -> None:
        super().__init__()
        self.__m_items = list()
    
    @property
    def items_count(self) -> int:
        return len(self.__m_items)
    
    def get_item(self, ind : int, keep_ref : bool=False) -> 'PdfObject':
        if ((ind < 0) or ind >= len(self.__m_items)): 
            return None
        if ((isinstance(self.__m_items[ind], PdfReference)) and not keep_ref): 
            obj = self.source_file.get_object(self.__m_items[ind].id0_)
            if (obj is None): 
                return None
            self.__m_items[ind] = obj
        return self.__m_items[ind]
    
    def remove_item(self, ind : int) -> None:
        if (ind >= 0 and (ind < len(self.__m_items))): 
            del self.__m_items[ind]
    
    def is_simple(self, lev : int) -> bool:
        if (len(self.__m_items) > 10 or lev > 5): 
            return False
        i = 0
        while i < len(self.__m_items): 
            it = self.get_item(i, False)
            if (it is not None and not it.is_simple(lev + 1)): 
                return False
            i += 1
        return True
    
    def add(self, obj : 'PdfObject') -> None:
        self.__m_items.append(obj)
    
    def clear(self) -> None:
        self.__m_items.clear()
    
    def to_string_ex(self, lev : int) -> str:
        if (lev > 10): 
            return "[...{0}]".format(self.items_count)
        res = io.StringIO()
        print("[", end="", file=res)
        i = 0
        i = 0
        first_pass699 = True
        while True:
            if first_pass699: first_pass699 = False
            else: i += 1
            if (not (i < len(self.__m_items))): break
            if (i > 0): 
                print(", ", end="", file=res)
            if (res.tell() > 100): 
                break
            it = self.get_item(i, False)
            if (it is None): 
                print("NULL", end="", file=res)
            elif (not it.is_simple(lev + 1)): 
                break
            else: 
                str0_ = it.to_string_ex(lev + 1)
                if (len(str0_) < 20): 
                    print(str0_, end="", file=res)
                    continue
                else: 
                    break
        if (i < len(self.__m_items)): 
            print("... {0}".format(len(self.__m_items)), end="", file=res, flush=True)
        print("]", end="", file=res)
        return Utils.toStringStringIO(res)
    
    def out_html(self, res : io.StringIO) -> None:
        from pullenti.unitext.internal.pdf.PdfDictionary import PdfDictionary
        print("<b>[</b> ", end="", file=res)
        i = 0
        first_pass700 = True
        while True:
            if first_pass700: first_pass700 = False
            else: i += 1
            if (not (i < self.items_count)): break
            o = self.get_item(i, False)
            if (o is None): 
                print("null", end="", file=res)
                continue
            if (o.id0_ > 0): 
                print("<a href=\"obj{0}\">".format(o.id0_), end="", file=res, flush=True)
            MiscHelper.correct_html_value(res, str(o), False, False, False)
            if (o.id0_ > 0): 
                print("</a>", end="", file=res)
            if ((i + 1) < self.items_count): 
                print(", ", end="", file=res)
                if ((isinstance(o, PdfDictionary)) or (isinstance(o, PdfArray))): 
                    print("<br/>", end="", file=res)
        print("<b> ]</b>", end="", file=res)
        self._out_html_refs(res)
    
    def _post_parse(self, stream : 'PdfStream') -> None:
        self.__m_items.clear()
        p0 = stream.position
        ch = stream.read_byte()
        if ((chr(ch)) != '['): 
            return
        while True:
            i = stream.peek_solid_byte()
            if (i < 0): 
                break
            ch = (i)
            if ((chr(ch)) == ']'): 
                stream.position = stream.position + 1
                return
            obj = stream.parse_object(self.source_file, False)
            if (obj is None): 
                break
            self.__m_items.append(obj)
    
    def get_double(self) -> float:
        if (len(self.__m_items) < 1): 
            return 0
        it = self.get_item(0, False)
        if (it is None): 
            return 0
        return it.get_double()
    
    def write0_(self, s : Stream) -> None:
        s.writebyte(ord('['))
        i = 0
        first_pass701 = True
        while True:
            if first_pass701: first_pass701 = False
            else: i += 1
            if (not (i < len(self.__m_items))): break
            if (i > 0): 
                s.writebyte(ord(' '))
            it = self.get_item(i, False)
            if (it is None): 
                continue
            if (it.deleted): 
                continue
            if (it.id0_ == 0): 
                it.write0_(s)
            else: 
                buf = "{0} 0 R".format(it.write_id).encode("ASCII", 'ignore')
                s.write(buf, 0, len(buf))
        s.writebyte(ord(']'))
    
    def get_all_references(self, refs : typing.List['PdfReference'], ids : typing.List[int]) -> None:
        if (self.id0_ > 0): 
            if (self.id0_ in ids): 
                return
            ids.append(self.id0_)
        i = 0
        while i < self.items_count: 
            o = self.get_item(i, True)
            if (o is not None): 
                o.get_all_references(refs, ids)
            i += 1
        if (self.id0_ > 0): 
            ids.remove(self.id0_)
    
    def get_all_who_ref_object(self, obj_id : int, cur : 'PdfObject', res : typing.List['PdfObject'], ids : typing.List[int]) -> None:
        if (self.id0_ > 0): 
            if (self.id0_ in ids): 
                return
            ids.append(self.id0_)
            cur = (self)
        i = 0
        while i < self.items_count: 
            o = self.get_item(i, True)
            if (o is not None and o.id0_ == obj_id): 
                if (not cur in res): 
                    res.append(cur)
            elif (o is not None and o.id0_ == 0): 
                o.get_all_who_ref_object(obj_id, cur, res, ids)
            i += 1
        if (self.id0_ > 0): 
            ids.remove(self.id0_)