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

    public java.util.ArrayList<String> args = new java.util.ArrayList<String>();

    public String name;

    public String text;

    public int beginPos;

    public int endPos;

    public boolean deleted;

    public PdfPageOper replaceBy;

    public PdfPageOper insertAfter;

    public double x1;

    public double x2;

    public double y1;

    public double y2;

    public PdfDictionary dic;

    public boolean isImage;

    public boolean isForm;

    public Object tag;

    @Override
    public String toString() {
        StringBuilder tmp = new StringBuilder();
        for (int i = 0; i < args.size(); i++) {
            tmp.append(args.get(i));
            tmp.append(' ');
        }
        tmp.append(name);
        return tmp.toString();
    }

    public void outHtml(StringBuilder res) throws java.io.IOException {
        for (String a : args) {
            com.pullenti.util.MiscHelper.correctHtmlValue(res, a, false, false, false);
            res.append(' ');
        }
        res.append("<b>").append(name).append("</b>");
        if (text != null) {
            res.append(" text: <span style=\"color:green\">");
            com.pullenti.util.MiscHelper.correctHtmlValue(res, text, false, false, false);
            res.append("</span>");
        }
        if (isImage && dic != null) 
            res.append(" <span style=\"color:green\">image: size=").append(dic.getIntItem("Length")).append("</span>");
        if (isForm && dic != null) 
            res.append(" <span style=\"color:green\">form (image): size=").append(dic.getIntItem("Length")).append("</span>");
        if (x2 > 0 || y2 > 0) 
            res.append(" <span style=\"color:blue\">x=[").append(com.pullenti.unisharp.Utils.mathRound(x1, 2)).append(" .. ").append(com.pullenti.unisharp.Utils.mathRound(x2, 2)).append("] y=[").append(com.pullenti.unisharp.Utils.mathRound(y1, 2)).append(" .. ").append(com.pullenti.unisharp.Utils.mathRound(y2, 2)).append("]</span>");
    }

    public static java.util.ArrayList<PdfPageOper> parse(java.util.ArrayList<PdfObject> li) {
        java.util.ArrayList<PdfPageOper> res = new java.util.ArrayList<PdfPageOper>();
        if (li == null) 
            return res;
        java.util.ArrayList<String> _args = new java.util.ArrayList<String>();
        int pos = 0;
        for (int i = 0; i < li.size(); i++) {
            PdfObject lex = li.get(i);
            String nam = checkEndOfOper(li, i);
            if (nam == null) {
                if (_args.size() == 0) 
                    pos = (int)lex.readPos;
                _args.add(lex.toString());
                continue;
            }
            PdfPageOper op = new PdfPageOper();
            op.name = nam;
            op.beginPos = (int)lex.readPos;
            op.endPos = (int)(((lex.readPos + ((long)lex.readLen)) - (1L)));
            if (_args.size() > 0) {
                op.beginPos = pos;
                com.pullenti.unisharp.Utils.addToArrayList(op.args, _args);
                _args.clear();
            }
            PdfRect r = null;
            if (lex.tag instanceof PdfText) {
                r = (PdfRect)com.pullenti.unisharp.Utils.cast(lex.tag, PdfRect.class);
                op.text = ((PdfText)com.pullenti.unisharp.Utils.cast(lex.tag, PdfText.class)).text;
            }
            else if (lex.tag instanceof PdfRect) {
                r = (PdfRect)com.pullenti.unisharp.Utils.cast(lex.tag, PdfRect.class);
                if (lex.tag instanceof PdfImage) 
                    op.isImage = true;
                else if (lex.tag instanceof PdfPage) 
                    op.isForm = true;
                if (lex.tag instanceof IPdfDictionaryWrapper) 
                    op.dic = ((IPdfDictionaryWrapper)com.pullenti.unisharp.Utils.cast(lex.tag, IPdfDictionaryWrapper.class)).getDic();
            }
            res.add(op);
            if (res.size() == 2686) {
            }
        }
        return res;
    }

    private static String checkEndOfOper(java.util.ArrayList<PdfObject> li, int i) {
        if (i >= li.size()) 
            return null;
        PdfName pname = (PdfName)com.pullenti.unisharp.Utils.cast(li.get(i), PdfName.class);
        if (pname == null || pname.name.length() > 3 || ((((i + 1) < li.size()) && (li.get(i + 1) instanceof PdfName) && com.pullenti.unisharp.Utils.stringsEq(((PdfName)com.pullenti.unisharp.Utils.cast(li.get(i + 1), PdfName.class)).name, "Do")))) 
            return null;
        if (((i + 2) < li.size()) && (li.get(i + 2) instanceof PdfName) && com.pullenti.unisharp.Utils.stringsEq(((PdfName)com.pullenti.unisharp.Utils.cast(li.get(i + 2), PdfName.class)).name, "Tf")) 
            return null;
        if (((i + 1) < li.size()) && (li.get(i + 1) instanceof PdfName)) {
            if (com.pullenti.unisharp.Utils.stringsCompare(((PdfName)com.pullenti.unisharp.Utils.cast(li.get(i + 1), PdfName.class)).name, "SCN", true) == 0) 
                return null;
        }
        return pname.name;
    }

    public double getArgDouble(int i) {
        if (i >= 0 && (i < args.size())) {
            double d;
            com.pullenti.unisharp.Outargwrapper<Double> wrapd234 = new com.pullenti.unisharp.Outargwrapper<Double>();
            boolean inoutres235 = com.pullenti.util.MiscHelper.tryParseDouble(args.get(i), wrapd234);
            d = (wrapd234.value != null ? wrapd234.value : 0);
            if (inoutres235) 
                return d;
        }
        return 0.0;
    }

    public void getBytesToStream(com.pullenti.unisharp.Stream s) throws java.io.IOException {
        for (String a : args) {
            if (Character.isLetter(a.charAt(0))) 
                s.write((byte)'/');
            for (char ch : a.toCharArray()) {
                s.write((byte)((int)ch));
            }
            s.write((byte)0x20);
        }
        for (int i = 0; i < name.length(); i++) {
            s.write((byte)((int)name.charAt(i)));
        }
        s.write((byte)0x20);
    }

    public byte[] getBytes() throws Exception, java.io.IOException {
        try (com.pullenti.unisharp.MemoryStream mem = new com.pullenti.unisharp.MemoryStream()) {
            mem.write((byte)0x20);
            this.getBytesToStream(mem);
            return mem.toByteArray();
        }
    }
    public PdfPageOper() {
    }
}
