/*  Found that previous library was complied with the MSVC compiler and not compatible with GNU toolchain. Tried to use Msys with PDFHummus Cmake build tools and it was hard-coded
 *  to MSVC. So building for GNU compiler will take some modification of Cmake.
 *  4.16.20 - after getting an image onto the pdf, we realized there was an error that was not getting logged/presented to us.
 *  we came upon this error, by trying to run the pdfHummusTest1.exe and the pdf file was not being created (because it was still
 *  open -> Windows locking files regardless of where they are open). This is the reason why we wanted to figure out if PDFHummus
 *  had an error logging capability and then we had to try and figure out how to use it... to be continued for next time.
 * 19 April 2020
 * Gal's documentation for using the logging capabilitites was old, the LogConfiguration function takes 3 parameters.
 * Gal does not actually handle or log errors.
 */

#include <stdlib.h>
#include <iostream>
#include "PDFWriter.h"
#include "PDFPage.h"
#include "PageContentContext.h"
#include "PDFFormXObject.h"
#include "Trace.h"
#include <windows.h>
#include <winnt.h>
#include "PDFModifiedPage.h"
#include "AbstractContentContext.h"
int main(){
    //  
    //  Go to: "%LOCALAPPDATA%\VirtualStore"  - because Windows does not write to the C:\ drive  
    printf("if running on Windows check here for file(s): %%LOCALAPPDATA%%\\VirtualStore\n");
    PDFWriter pdfWriter;
    //  PREPARE PDF DOCUMENT
    //  ********************
    //  get a PDFWriter object to interact with. set the PDF version, set log file
    EStatusCode e = pdfWriter.StartPDF("c:\\myFile.pdf",ePDFVersion13,LogConfiguration(true,true,".\\logMe"));
    if (e){ 
        /*  25APR20 - look through codebase and found these three reasons for nonrecoverable failure:
        *  - user requested encryption, document doesn't support it
        *  - it is unlikely, but for some reason GK wanted to call OutputFile::CloseFile(), it failed 
        *  - operating system was unsuccessful at giving a lock for the filename *
        * 
        *  * Windows 'gives' a lock, but doesn't protect others from writing to this filename
        */
        printf("Terminating (c:\\myFile.pdf): unsuccessful lock or couldn't close existing or requested encryption but not supported\n");
        TRACE_LOG("Terminating (c:\\myFile.pdf): unsuccessful lock or couldn't close existing or requested encryption but not supported");
        exit(1);
    }
    TRACE_LOG("Notified OS we might use c:\\myFile.pdf");
    PDFPage* pdfPage = new PDFPage();
    //  set dimensions for an A4 sized page
    pdfPage->SetMediaBox(PDFRectangle(0,0,595,842));
    PageContentContext* pageContentContext = pdfWriter.StartPageContentContext(pdfPage);
    //  include font file
    PDFUsedFont* arialTTF = pdfWriter.GetFontForFile("C:\\Windows\\Fonts\\arial.ttf");
    //  include image file
    
    //  ADD CONTENT 
    //  ********************
    
    //  set font color to black
    pageContentContext->k(0,0,0,1);
    //  set text object
    pageContentContext->BT();
    //  set font 
    pageContentContext->Tf(arialTTF,1);
    //  set position of text
    pageContentContext->Tm(20,0,0,20,40,822);
    //  insert text into PDF
    pageContentContext->Tj("Text placed and scaled with Tm");
    //  end of text
    pageContentContext->ET();
    pdfWriter.EndPageContentContext(pageContentContext);
    pdfWriter.WritePageAndRelease(pdfPage);
    pdfWriter.EndPDF();
    // ****************Shutdown Usage************
    // ******************************************
    // ******************************************
    // pdfWriter.Shutdown("c:\\myFile.pdf.shutdown");
    /*PDFWriter pdfWriterB;
    pdfWriterB.ContinuePDF("c:\\myFile.pdf","myFile.pdf.shutdown","",LogConfiguration(true,true,".\\logMe"));
    PDFPage* pdfPageB = new PDFPage();
    //  set dimensions for an A4 sized page
    pdfPageB->SetMediaBox(PDFRectangle(0,0,595,842));
    PageContentContext* pageContentContextB = pdfWriterB.StartPageContentContext(pdfPageB);
    PDFFormXObject* image = pdfWriterB.CreateFormXObjectFromJPGFile("C:\\Users\\bad-p\\Desktop\\Work Folder\\VS CODE\\SanAntonioPass.jpg");
    if (!image){
        TRACE_LOG("Image file not found, creating PDF without image.");
        printf("The image \"C:\\Users\\bad-p\\Desktop\\Work Folder\\VS CODE\\SanAntonioPass.jpg\" was not found.");
    }
    pageContentContextB->q();
    pageContentContextB->cm(0.4,0,0,0.4,57.5,241);
    pageContentContextB->Do(pdfPageB->GetResourcesDictionary().AddFormXObjectMapping(image->GetObjectID()));
    pageContentContextB->Q();
    delete image;
    //  END PAGE, WRITE PDF.
    //  ******************** 
    pdfWriterB.EndPageContentContext(pageContentContextB);
    EStatusCode f = pdfWriterB.WritePageAndRelease(pdfPageB);
    if (f){
        printf ("something interesting");
    }
    pdfWriterB.EndPDF();*/
    
   // PDFWriter pdfWriterB;
    //pdfWriterB.StartPDF("c:\\myFile.pdf",ePDFVersion13,LogConfiguration(true,true,".\\logMe"));
    //pdfWriterB.ContinuePDF("C:\\Users\\bad-p\\AppData\\Local\\VirtualStore\\myFile.pdf","C:\\Users\\bad-p\\AppData\\Local\\VirtualStore\\myFile.pdf.shutdown");
    // Create a new page
    pdfWriter.ModifyPDF("c:\\myFile.pdf",ePDFVersion13,"", LogConfiguration(true,true,".\\logMe"));
    PDFModifiedPage pdfPage2(&pdfWriter,0);
    AbstractContentContext* contentContext = pdfPage2.StartContentContext();
    PDFFormXObject* image = pdfWriter.CreateFormXObjectFromJPGFile("C:\\Users\\bad-p\\Desktop\\Work Folder\\VS CODE\\SanAntonioPass.jpg");
    if (!image){
        TRACE_LOG("Image file not found, creating PDF without image.");
        printf("The image \"C:\\Users\\bad-p\\Desktop\\Work Folder\\VS CODE\\SanAntonioPass.jpg\" was not found.");
    }
    contentContext->q();
    contentContext->cm(0.4,0,0,0.4,57.5,241);
    contentContext->Do("oooogAAABooogggAAABoogggaaa");
    //contentContext->Do(pdfPage2->GetResourcesDictionary().AddFormXObjectMapping(image->GetObjectID()));
    contentContext->Q();
    delete image;
    //AbstractContentContext::TextOptions opt(pdfWriter.GetFontForFile("C:\\Windows\\Fonts\\arial.ttf"),14,AbstractContentContext::eGray,0);
    //contentContext->WriteText(75,805,"Test Text",opt);
    pdfPage2.EndContentContext();
    pdfPage2.WritePage();
    /*pdfPage2->SetMediaBox(PDFRectangle(0,0,595,842));
    // Create a content context for the page
    PageContentContext* pageContentContext2 = pdfWriter.StartPageContentContext(pdfPage2);
    				
    // End content context, and write the page
    pdfWriter.EndPageContentContext(pageContentContext2);
    pdfWriter.WritePageAndRelease(pdfPage2);*/
    pdfWriter.EndPDF();
}
