using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;

namespace ZionGraphics.Handlers
{
    class PdfImporter : IDisposable
    {
        private PdfReader reader;

        private string path;

        private EncoderParameters encoderParameters;
        private EncoderParameters EncoderParameters
        {
            get
            {
                if (this.encoderParameters == null)
                {
                    encoderParameters = new System.Drawing.Imaging.EncoderParameters(2);
                }
                encoderParameters.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)System.Drawing.Imaging.EncoderValue.MultiFrame);
                encoderParameters.Param[1] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)System.Drawing.Imaging.EncoderValue.CompressionLZW);
                return encoderParameters;
            }
        }
        private EncoderParameters EncoderParametersMultipage
        {
            get
            {
                if (encoderParameters == null)
                    encoderParameters = new System.Drawing.Imaging.EncoderParameters(2);
                encoderParameters.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)System.Drawing.Imaging.EncoderValue.FrameDimensionPage);
                encoderParameters.Param[1] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)System.Drawing.Imaging.EncoderValue.CompressionLZW);
                return encoderParameters;
            }
        }

        public PdfImporter(string path)
        {
            this.path = path;
            this.reader = new PdfReader(this.path);
        }
        public void Dispose()
        {
            reader.Close();
            reader.Dispose();
            encoderParameters.Dispose();
        }

        internal MemoryStream ImageStream
        {
            get
            {
                System.IO.MemoryStream stream = new System.IO.MemoryStream();
                System.Drawing.Image data = null;


                for (int index = 1; index <= this.reader.NumberOfPages; index++)
                {
                    if (PdfTextExtractor.GetTextFromPage(reader, index).Trim() == string.Empty)
                    {
                        using (Bitmap bitmap = ExtractorBitmapFromPage(reader, index))
                        {
                            data = AddPage(stream, data, bitmap);
                        }
                    }
                }

                data.Save(@"D:\Teste.tiff", ImageFormat.Tiff);

                return stream;
            }
        }

        private System.Drawing.Image AddPage(MemoryStream stream, System.Drawing.Image data, Bitmap bitmap)
        {
            if (data == null)
            {
                data = (System.Drawing.Image)bitmap.Clone();
                data.Save(stream, CodecInfo.GetEncoder(System.Drawing.Imaging.ImageFormat.Tiff), EncoderParameters);
            }
            else
            {
                data.SaveAdd((System.Drawing.Image)bitmap, EncoderParametersMultipage);
            }
            return data;
        }

        private Bitmap ExtractorBitmapFromPage(PdfReader reader, int index)
        {
            Bitmap result = null;
            PdfReaderContentParser parser = new PdfReaderContentParser(reader);
            using (ImageRenderListener listener = new ImageRenderListener())
            {
                parser.ProcessContent(index, listener);

                if (listener.Images.Count == 0)
                    throw new NullReferenceException();

                result = (Bitmap)listener.Images.First().Value.Img;

            }
            parser = null;

            return result;
        }

    }
}
