/** @id $Id$

   This file has been added:
     - by bootstrap.sh
     - on Tue, 10 November 2015 20:45:44 +0100

*/
//       1         2         3         4         5         6         7         8
// 45678901234567890123456789012345678901234567890123456789012345678901234567890

#ifndef PDFSIGN_HXX
#define PDFSIGN_HXX

#include <QMainWindow>
#include <ui_pdfsign.hxx>
#include <poppler/qt5/poppler-qt5.h>
//#include <podofo/doc/PdfDocument.h>
#include <QFileDialog>
#include <memory>

/// Main Window
/** Main window for pdfsign */
class PdfSign: public QMainWindow, protected Ui::Pdfsign {
    Q_OBJECT;
  public:
    explicit PdfSign(QWidget *parent = 0): QMainWindow(parent) {
      setupUi(this);
    }
    virtual ~PdfSign() {}
  public slots:
    void on_actionOpen_triggered() {
      QString fileName(QFileDialog::getOpenFileName(this, tr("Open PDF File"), QString(),
                                                    tr("PDF-Document (*.pdf)")));
      if (fileName.isEmpty()) return;
      _pdf = std::shared_ptr<Poppler::Document>(Poppler::Document::load(fileName));
      showPage();
      
    }
  public:
    void showPage(int pageNumber=0) {
      if (!_pdf) return;
      std::shared_ptr<Poppler::Page> pdfPage(_pdf->page(pageNumber));
      if (!pdfPage) return;
      _pdfDisplay->setPixmap(QPixmap::fromImage(pdfPage->renderToImage()));
      return;
    }
    void sign(QString filename) {
      PdfSigIncMemDocument doc(filename.c_str());
      char                    *buffer = NULL;
      buffer = (char *)malloc(809600);
      PdfOutputDevice         output(buffer, 809600);
      PdfSignOutputDevice     signOutput(&output);
      signOutput.SetSignatureSize(2048);
      printf( "Page count:%d\n", doc.GetPageCount());
      
      PdfSigIncSignatureField *sign = doc.GetSignatureField();
      sign->SetSignatureDate(PdfDate());
      sign->SetSignatureReason(L"I dont agree");
      //sign->SetSignatureImage("e:\\sign.jpg", 0, 100, 200, 100, 100);
      sign->SetSignatureText(L"Hello World", 0, 100, 200, 100, 100, 12);
      doc.Initialize();
      doc.CreateVisualSign();
      doc.Write(&signOutput);
      printf( "Has Signature:%d\n", signOutput.HasSignaturePosition());
      /*
      char buff[809600] = {0};
      size_t len;
      BYTE *Signature = NULL;
      DWORD cbSignature = 0;
      
      signOutput.AdjustByteRange();
      signOutput.Seek(0);
      len = signOutput.ReadForSignature(buff, 809600);
      printf("total tbs size: %d\n", len);
      SignMessage( (BYTE *)buff, len, &Signature, &cbSignature);//Digital signing using MSCAPI
      printf("signature size: %d\n", cbSignature);
      const PoDoFo::PdfData *sig = new PoDoFo::PdfData((char *)Signature, cbSignature);
      signOutput.SetSignature(*sig);
          */
      signOutput.Flush();
      
      FILE *fp = NULL;
      fp = fopen("/tmp/signed.pdf", "wb" );
      fwrite( buffer, signOutput.GetLength(), 1, fp );
      fclose(fp);
      free(buffer);
    }
  private:
    std::shared_ptr<Poppler::Document> _pdf;
};

#endif
