/*
 * 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 PdfPageOper
    {
        public List<string> Args = new List<string>();
        public string Name;
        public string Text;
        public int BeginPos;
        public int EndPos;
        public bool Deleted;
        public PdfPageOper ReplaceBy;
        public PdfPageOper InsertAfter;
        public double X1;
        public double X2;
        public double Y1;
        public double Y2;
        public PdfDictionary Dic;
        public bool IsImage;
        public bool IsForm;
        public object Tag;
        public override string ToString()
        {
            StringBuilder tmp = new StringBuilder();
            for (int i = 0; i < Args.Count; i++) 
            {
                tmp.Append(Args[i]);
                tmp.Append(' ');
            }
            tmp.Append(Name);
            return tmp.ToString();
        }
        public void OutHtml(StringBuilder res)
        {
            foreach (string a in Args) 
            {
                Pullenti.Util.MiscHelper.CorrectHtmlValue(res, a, false, false, false);
                res.Append(' ');
            }
            res.AppendFormat("<b>{0}</b>", Name);
            if (Text != null) 
            {
                res.Append(" text: <span style=\"color:green\">");
                Pullenti.Util.MiscHelper.CorrectHtmlValue(res, Text, false, false, false);
                res.Append("</span>");
            }
            if (IsImage && Dic != null) 
                res.AppendFormat(" <span style=\"color:green\">image: size={0}</span>", Dic.GetIntItem("Length"));
            if (IsForm && Dic != null) 
                res.AppendFormat(" <span style=\"color:green\">form (image): size={0}</span>", Dic.GetIntItem("Length"));
            if (X2 > 0 || Y2 > 0) 
                res.AppendFormat(" <span style=\"color:blue\">x=[{0} .. {1}] y=[{2} .. {3}]</span>", Math.Round(X1, 2), Math.Round(X2, 2), Math.Round(Y1, 2), Math.Round(Y2, 2));
        }
        public static List<PdfPageOper> Parse(List<PdfObject> li)
        {
            List<PdfPageOper> res = new List<PdfPageOper>();
            if (li == null) 
                return res;
            List<string> args = new List<string>();
            int pos = 0;
            for (int i = 0; i < li.Count; i++) 
            {
                PdfObject lex = li[i];
                string nam = CheckEndOfOper(li, i);
                if (nam == null) 
                {
                    if (args.Count == 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 + lex.ReadLen) - 1));
                if (args.Count > 0) 
                {
                    op.BeginPos = pos;
                    op.Args.AddRange(args);
                    args.Clear();
                }
                PdfRect r = null;
                if (lex.Tag is PdfText) 
                {
                    r = lex.Tag as PdfRect;
                    op.Text = (lex.Tag as PdfText).Text;
                }
                else if (lex.Tag is PdfRect) 
                {
                    r = lex.Tag as PdfRect;
                    if (lex.Tag is PdfImage) 
                        op.IsImage = true;
                    else if (lex.Tag is PdfPage) 
                        op.IsForm = true;
                    if (lex.Tag is IPdfDictionaryWrapper) 
                        op.Dic = (lex.Tag as IPdfDictionaryWrapper).Dic;
                }
                res.Add(op);
                if (res.Count == 2686) 
                {
                }
            }
            return res;
        }
        static string CheckEndOfOper(List<PdfObject> li, int i)
        {
            if (i >= li.Count) 
                return null;
            PdfName pname = li[i] as PdfName;
            if (pname == null || pname.Name.Length > 3 || ((((i + 1) < li.Count) && (li[i + 1] is PdfName) && (li[i + 1] as PdfName).Name == "Do"))) 
                return null;
            if (((i + 2) < li.Count) && (li[i + 2] is PdfName) && (li[i + 2] as PdfName).Name == "Tf") 
                return null;
            if (((i + 1) < li.Count) && (li[i + 1] is PdfName)) 
            {
                if (string.Compare((li[i + 1] as PdfName).Name, "SCN", true) == 0) 
                    return null;
            }
            return pname.Name;
        }
        public double GetArgDouble(int i)
        {
            if (i >= 0 && (i < Args.Count)) 
            {
                double d;
                if (Pullenti.Util.MiscHelper.TryParseDouble(Args[i], out d)) 
                    return d;
            }
            return 0;
        }
        public void GetBytesToStream(Stream s)
        {
            foreach (string a in Args) 
            {
                if (char.IsLetter(a[0])) 
                    s.WriteByte((byte)'/');
                foreach (char ch in a) 
                {
                    s.WriteByte((byte)((int)ch));
                }
                s.WriteByte((byte)0x20);
            }
            for (int i = 0; i < Name.Length; i++) 
            {
                s.WriteByte((byte)((int)Name[i]));
            }
            s.WriteByte((byte)0x20);
        }
        public byte[] GetBytes()
        {
            using (MemoryStream mem = new MemoryStream()) 
            {
                mem.WriteByte((byte)0x20);
                this.GetBytesToStream(mem);
                return mem.ToArray();
            }
        }
    }
}