using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;
using System;
using System.Collections.Generic;
using System.Drawing;

namespace ZionGraphics.Handlers
{
    public class ImageData
    {
        public Rectangle Rect;

        public System.Drawing.Image Img;

        public ImageData(System.Drawing.Image img, Rectangle rect)
        {
            Img = img;

            Rect = rect;
        }
    }

    public class ImageRenderListener : IRenderListener, IDisposable
    {

        #region Fields
        Dictionary<int, ImageData> images = new Dictionary<int, ImageData>();
        #endregion Fields
        #region Properties
        public Dictionary<int, ImageData> Images
        {
            get { return images; }
        }
        #endregion Properties
        #region Methods
        #region Public Methods
        public void BeginTextBlock() { }

        public void Dispose()
        {
            foreach (var img in images)
            {
                img.Value.Img.Dispose();
            }
            images.Clear();
        }

        public void EndTextBlock() { }
        public void RenderImage(ImageRenderInfo renderInfo)
        {
            PdfImageObject image = renderInfo.GetImage();


            var matrix = renderInfo.GetImageCTM();

            float porportion = image.GetDrawingImage().Width / matrix[Matrix.I11];

            RectangleF rect = new RectangleF(
                matrix[Matrix.I31] * porportion,
                matrix[Matrix.I32] * porportion,
                matrix[Matrix.I11] * porportion,
                matrix[Matrix.I22] * porportion
                );

            try
            {
                PdfName filter = (PdfName)image.Get(PdfName.FILTER);

                if (filter != null)
                {
                    System.Drawing.Image drawingImage = image.GetDrawingImage();
                    string extension = ".";
                    if (filter == PdfName.DCTDECODE)
                    {
                        extension += PdfImageObject.ImageBytesType.JPG.FileExtension;
                    }
                    else if (filter == PdfName.JPXDECODE)
                    {
                        extension += PdfImageObject.ImageBytesType.JP2.FileExtension;
                    }
                    else if (filter == PdfName.FLATEDECODE)
                    {
                        extension += PdfImageObject.ImageBytesType.PNG.FileExtension;
                    }
                    else if (filter == PdfName.LZWDECODE)
                    {
                        extension += PdfImageObject.ImageBytesType.CCITT.FileExtension;
                    }

                    this.Images.Add(images.Count, new ImageData(drawingImage, new Rectangle((int)rect.X, (int)rect.Y, (int)rect.Width, (int)rect.Height)));
                }
            }
            catch
            {
                System.Drawing.Image drawingImage = image.GetDrawingImage();
                this.Images.Add(images.Count, new ImageData(drawingImage, new Rectangle((int)rect.X, (int)rect.Y, (int)rect.Width, (int)rect.Height)));
            }
        }
        public void RenderText(TextRenderInfo renderInfo) { }
        #endregion Public Methods
        #endregion Methods
    }
}
