using System;
using System.Collections.Generic;
using System.Text;

using ExpertPdf.MergePdf;

namespace EPMergePDFDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                InvalidArguments();
                return;
            }

            string outFile = "MergedDocument.pdf";

            PdfPageSize pageSize = PdfPageSize.A4;
            PDFCompressionLevel compression = PDFCompressionLevel.Normal;
            PDFPageOrientation orientation = PDFPageOrientation.Portrait;

            // search the PDF document options
            for (int argIdx = 0; argIdx < args.Length; argIdx++)
            {
                string argument = args[argIdx];

                if (argument.StartsWith("/pagesize:"))
                {
                    string pageSizeStr = argument.Substring("/pagesize:".Length, argument.Length - "/pagesize:".Length);

                    try
                    {
                        pageSize = (PdfPageSize)Enum.Parse(typeof(PdfPageSize), pageSizeStr, true);
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("Invalid page size.");
                        InvalidArguments();
                        return;
                    }
                }

                if (argument.StartsWith("/compression:"))
                {
                    string compressionStr = argument.Substring("/compression:".Length, argument.Length - "/compression:".Length);

                    try
                    {
                        compression = (PDFCompressionLevel)Enum.Parse(typeof(PDFCompressionLevel), compressionStr, true);
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("Invalid compression level.");
                        InvalidArguments();
                        return;
                    }
                }

                if (argument.StartsWith("/orientation:"))
                {
                    string orientationStr = argument.Substring("/orientation:".Length, argument.Length - "/orientation:".Length);

                    try
                    {
                        orientation = (PDFPageOrientation)Enum.Parse(typeof(PDFPageOrientation), orientationStr, true);
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("Invalid page orientation.");
                        InvalidArguments();
                        return;
                    }
                }
            }

            PdfDocumentOptions pdfDocumentOptions = new PdfDocumentOptions();

            pdfDocumentOptions.PdfCompressionLevel = compression;
            pdfDocumentOptions.PdfPageSize = pageSize;
            pdfDocumentOptions.PdfPageOrientation = orientation;

            PDFMerge pdfMerge = new PDFMerge(pdfDocumentOptions);

            for (int argIdx = 0; argIdx < args.Length; argIdx++)
            {
                string argument = args[argIdx];
                if (argument.StartsWith("/out:"))
                {
                    string outFilePath = argument.Substring("/out:".Length, argument.Length - "/out:".Length);
                    outFilePath = RemoveQuotes(outFilePath).Trim();
                    if (outFilePath != null && outFilePath.Length > 0)
                    {
                        outFile = outFilePath;
                    }
                }

                if (argument.StartsWith("/pdf:"))
                {
                    string pdfFilePath = argument.Substring("/pdf:".Length, argument.Length - "/pdf:".Length);
                    pdfFilePath = RemoveQuotes(pdfFilePath).Trim();
                    if (pdfFilePath != null && pdfFilePath.Length > 0)
                    {
                        pdfMerge.AppendPDFFile(pdfFilePath);
                    }
                    else
                    {
                        Console.WriteLine("Invalid pdf file name.");
                        InvalidArguments();
                        return;
                    }
                }

                if (argument == "/emptypage")
                {
                    pdfMerge.AppendEmptyPage();
                }

                if (argument.StartsWith("/image:"))
                {
                    string imageFilePath = argument.Substring("/image:".Length, argument.Length - "/image:".Length);
                    imageFilePath = RemoveQuotes(imageFilePath).Trim();
                    if (imageFilePath != null && imageFilePath.Length > 0)
                    {
                        pdfMerge.AppendImageFile(imageFilePath);
                    }
                    else
                    {
                        Console.WriteLine("Invalid image file name.");
                        InvalidArguments();
                        return;
                    }
                }

            }

            pdfMerge.SaveMergedPDFToFile(outFile);
        }

        public static string RemoveQuotes(string quotedString)
        {
            string res = quotedString;
            if (res.StartsWith("\""))
            {
                res = res.Substring(1, res.Length - 1);
            }
            if (res.EndsWith("\""))
            {
                res = res.Substring(0, res.Length - 1);
            }

            return res;
        }

        private static void InvalidArguments()
        {
            Console.WriteLine(@"Usage: mergepdf.exe {[/pdf:pdf_file] | [/emptypage] | [/image:img_file] }+ [/pagesize:(A4|A3|A2|A1|A0)] [/compression:(NoCompression|Normal|Best)] [/orientation:(Portrait|Landscape)] /out:out_file");
            Console.WriteLine(String.Format(@"You can specify pdf files and image files in any order."));
            Console.WriteLine(String.Format(@"Example: epmergepdf.exe /pdf:""C:\Report.pdf"" /emptypage /image:""C:\Image.Jpeg"" /out:MergedDocument.pdf"));
        }
    }
}
