/**
 * Copyright (c) 2001-2008 by PDFTron Systems Inc. All Rights Reserved.
 */
#include "pagemaster.h"
#include <iostream>
#include <sstream>
#include <vector>

using namespace std;


/** 
 * A custom callback that can be used to report errors and other messages.
 */ 
char* PAGEMASTER_API_CALL MyCallback(int mode, char* msg, void* user_data)
{
	if (mode == PAGEMASTER_ERROR) {
		cout << "Error: " << msg << endl;
	}
	else if (mode == PAGEMASTER_MSG) {
		cout << msg;
	}
	else if (mode == PAGEMASTER_GETPASS) {
		static string gl_pass;
		cin >> gl_pass;
		return (char*)gl_pass.c_str();
	}
	else if (mode == PAGEMASTER_OUT_FILENAME)
	{
		if(user_data != 0)
		{
			vector<string>* saved_files=(vector<string>*)user_data;
			saved_files->push_back(string(msg));
		}
	}

	return 0;
}


/**
 * You can modify the following lines with your registration information.
 */
char* username = "John Doe";
char* company = "My Company";
char* lic_key = "my license key";

/**
 * A macro that can be used to switch between different code samples.
 */
#define EXAMPLE 1

/**
 * A simple PDF PageMaster program: Pass an explicit command-line string.
 *
 * This example will split the file bookmark.pdf and create
 * one new file for each page.
 *
 * The -s parameter specifies that split is the operation
 * The --digits parameter specifies that the numbers in resulting
 * file names should have at least 2 digits
 * The -o parameter specifies the output directory.  In this
 * case that files should be created in the TestOutput/test1 directory
 *
 */
#if EXAMPLE == 1
int main() 
{
	PageMasterInit(username, company, lic_key);
	PageMasterRun("-s --digits 2 -o ../TestOutput/test1/ ../TestFiles/bookmark.pdf", MyCallback, 0);
	cout << "Done!" << endl;
	return 0;
}
#endif

/**
 * An example of how to use PageMaster SDK to implement a command-line application
 * for the processing of PDF documents.
 */
#if EXAMPLE == 2
int main(int argc, char** argv) 
{
	PageMasterInit(username, company, lic_key);
	stringstream s;
	for (int i=1; i<argc; ++i) {
		s << argv[i] << ' ';
	}
	PageMasterRun((char*)s.str().c_str(), MyCallback, 0);
	cout << "Done!!" << endl;
	return 0;
}
#endif

/** 
 * A more complicated example that illustrates how to dynamically build a command 
 * string based on the users preferences...
 * Please note that this sample does not cover all possible options offered by 
 * Pagemaster. For a detailed explanation of all commands, please refer to Pagemaster 
 * User Manual.
 */
#if EXAMPLE == 3
int main() 
{
	PageMasterInit(username, company, lic_key);


	
	bool linearized_output = true;
	bool aes_encryption = false;
	string output_file = "../TestOutput/test3/test.pdf";
	string open_password = "mypass";
	string title = "My Title";
	string author = "Joe Doe";
	string subject = "My Subject";
	string keywords = "key1 key2 key2";
    string creator = "PDFTron PDFNet";
    string producer = "PageMaster";

	// Permissions
	bool printing = false;
	bool high_rez_printing = false;
	bool doc_modifications = true;  // Changing the document
	bool content_extraction = true;
	bool commenting = true;
	bool forms_editing = true;
	bool accessibility = true;
	bool document_assembly = true;

	// Given the above settings build a command string.
	stringstream s;
	//specify the merge option
	s << "-m ";


	if (!output_file.empty()) s << "-o " << output_file << " ";

	if (!open_password.empty()) s << "--userpass " << open_password << " ";

	// Set the permissions.
	s << "-e a -d \"";
	if (printing == false) s << "p";
	if (high_rez_printing == false) s << "h";
	if (doc_modifications == false) s << "m";
	if (content_extraction == false)  s << "c";
	if (commenting == false)  s << "o";
	if (forms_editing == false)  s << "f";
	if (accessibility == false)  s << "x";
	if (document_assembly == false)  s << "s";
	s << "\" ";

	if (linearized_output) s << "-l ";

	// Set document information.
	if (!title.empty())  s << "--settitle \"" << title << "\" ";
	if (!author.empty())  s << "--setauthor \"" << author << "\" ";
	if (!subject.empty())  s << "--setsubject \"" << subject << "\" ";
	if (!keywords.empty())  s << "--setkeywords \"" << keywords << "\" ";
	if (!creator.empty())  s << "--setcreator \"" << creator << "\" ";
	if (!producer.empty()) s << "--setproducer \"" << producer << "\" ";

	// specify input files and folders...
	s << "../../samples/TestFiles/bookmark.pdf,1-15 ";
	s << "../../samples/TestFiles/secured.pdf|test ";

	// Execute the command string.
	vector<string> saved_files;
	PageMasterRun((char*)s.str().c_str(), MyCallback, &saved_files);

	//print the saved file(s)
	cout << "The Saved File Is:" << endl;
	for(unsigned int i=0; i<saved_files.size(); i++)
	{
		cout << saved_files[i] << endl;
	}

	cout << "Done!!!" << endl;
	return 0;
}
#endif
