/*
 * 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
 */

package com.pullenti.unitext.internal.pdf;

public class PdfArray extends PdfObject {

    private java.util.ArrayList<PdfObject> m_Items = new java.util.ArrayList<PdfObject>();

    public int getItemsCount() {
        return m_Items.size();
    }


    public PdfObject getItem(int ind, boolean keepRef) throws java.io.IOException {
        if ((ind < 0) || ind >= m_Items.size()) 
            return null;
        if ((m_Items.get(ind) instanceof PdfReference) && !keepRef) {
            PdfObject obj = sourceFile.getObject(m_Items.get(ind).id);
            if (obj == null) 
                return null;
            com.pullenti.unisharp.Utils.putArrayValue(m_Items, ind, obj);
        }
        return m_Items.get(ind);
    }

    public void removeItem(int ind) {
        if (ind >= 0 && (ind < m_Items.size())) 
            m_Items.remove(ind);
    }

    @Override
    public boolean isSimple(int lev) throws java.io.IOException {
        if (m_Items.size() > 10 || lev > 5) 
            return false;
        for (int i = 0; i < m_Items.size(); 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();
    }

    @Override
    public String toStringEx(int lev) throws java.io.IOException {
        if (lev > 10) 
            return ("[..." + this.getItemsCount() + "]");
        StringBuilder res = new StringBuilder();
        res.append("[");
        int i;
        for (i = 0; i < m_Items.size(); 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.size()) 
            res.append("... ").append(m_Items.size());
        res.append("]");
        return res.toString();
    }

    @Override
    public void outHtml(StringBuilder res) {
        try {
            res.append("<b>[</b> ");
            for (int i = 0; i < getItemsCount(); i++) {
                PdfObject o = this.getItem(i, false);
                if (o == null) {
                    res.append("null");
                    continue;
                }
                if (o.id > 0) 
                    res.append("<a href=\"obj").append(o.id).append("\">");
                com.pullenti.util.MiscHelper.correctHtmlValue(res, o.toString(), false, false, false);
                if (o.id > 0) 
                    res.append("</a>");
                if ((i + 1) < getItemsCount()) {
                    res.append(", ");
                    if ((o instanceof PdfDictionary) || (o instanceof PdfArray)) 
                        res.append("<br/>");
                }
            }
            res.append("<b> ]</b>");
            this._outHtmlRefs(res);
        } catch (Exception ex123) {
        }
    }

    public void postParse(PdfStream stream) throws java.io.IOException {
        m_Items.clear();
        int p0 = stream.getPosition();
        byte ch = (byte)stream.readByte();
        if (((char)ch) != '[') 
            return;
        while (true) {
            int i = stream.peekSolidByte();
            if (i < 0) 
                break;
            ch = (byte)i;
            if (((char)ch) == ']') {
                stream.setPosition(stream.getPosition() + 1);
                return;
            }
            PdfObject obj = stream.parseObject(sourceFile, false);
            if (obj == null) 
                break;
            m_Items.add(obj);
        }
    }

    @Override
    public double getDouble() throws java.io.IOException {
        if (m_Items.size() < 1) 
            return 0.0;
        PdfObject it = this.getItem(0, false);
        if (it == null) 
            return 0.0;
        return it.getDouble();
    }

    @Override
    public void write(com.pullenti.unisharp.Stream s) throws java.io.IOException {
        s.write((byte)'[');
        for (int i = 0; i < m_Items.size(); i++) {
            if (i > 0) 
                s.write((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 = com.pullenti.unisharp.Utils.encodeCharset(java.nio.charset.Charset.forName("ASCII"), (((Integer)it.getWriteId()).toString() + " 0 R"));
                s.write(buf, 0, buf.length);
            }
        }
        s.write((byte)']');
    }

    @Override
    public void getAllReferences(java.util.ArrayList<PdfReference> refs, java.util.ArrayList<Integer> ids) throws java.io.IOException {
        if (id > 0) {
            if (ids.contains(id)) 
                return;
            ids.add(id);
        }
        for (int i = 0; i < getItemsCount(); i++) {
            PdfObject o = this.getItem(i, true);
            if (o != null) 
                o.getAllReferences(refs, ids);
        }
        if (id > 0) 
            ids.remove(id);
    }

    @Override
    public void getAllWhoRefObject(int objId, PdfObject cur, java.util.ArrayList<PdfObject> res, java.util.ArrayList<Integer> ids) throws java.io.IOException {
        if (id > 0) {
            if (ids.contains(id)) 
                return;
            ids.add(id);
            cur = this;
        }
        for (int i = 0; i < getItemsCount(); 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);
    }
    public PdfArray() {
        super();
    }
}
