/**
 * Copyright (c) 2001-2008 by PDFTron Systems Inc. All Rights Reserved.
 */
#include "pdf2svg.h"
#include <iostream>
#include <sstream>
#include <vector>

using namespace std;

/** 
 * A custom callback that can be used to report errors and other messages.
 */ 
char* PDF2SVG_API_CALL MyCallback(int mode, char* msg, void* user_data)
{
	if (mode == PDF2SVG_ERROR) {
		cout << "Error: " << msg << endl;
	}
	else if (mode == PDF2SVG_MSG) {
		cout << msg;
	}
	else if (mode == PDF2SVG_GETPASS) {
		static string gl_pass;
		cin >> gl_pass;
		return (char*)gl_pass.c_str();
	}
	else if (mode == PDF2SVG_OUT_FILENAME)
	{
		if(user_data != 0)
		{
			//stores the filename in the given vector 
			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";

/* the path to the resource file 
 * can be NULL in Windows 
 */
#ifdef _WIN32
char* resource_path =NULL;
#else
char* resource_path ="../../lib";
#endif

/**
 * A macro that can be used to switch between different code samples.
 */
#define EXAMPLE 1

/**
 * The simplest PDF2SVG program: Pass an explicit command-line string.
 *
 * This example converts 'tiger.pdf' to 'tiger.svg'
 * To convert all PDF documents in 'TestFiles' folder simply delete 'tiger.pdf'
 * from the command string.
 * 
 * For a detailed explanation of all commands, please refer to PDF2SVG SDK User 
 * Manual. Also, PDF2SVG Command-Line application is a great tool to experiment 
 * with the available options.
 */
#if EXAMPLE == 1
int main() 
{
	PDF2SVGInit(username, company, lic_key, resource_path);
	PDF2SVGRun("-o ../TestOutput/test1 ../../samples/TestFiles/tiger.pdf", MyCallback, 0);
	cout << "Done!" << endl;
	return 0;
}
#endif

/**
 * An example of how to use PDF2SVG SDK to implement a command-line application
 * for converting PDF documents to SVG.
 */
#if EXAMPLE == 2
void main(int argc, char** argv) 
{
	PDF2SVGInit(username, company, lic_key, resource_path);
	stringstream s;
	for (int i=1; i<argc; ++i) {
		s << argv[i] << ' ';
	}

	PDF2SVGRun((char*)s.str().c_str(), MyCallback, 0);
	cout << "Done!" << endl;
	return 0;
}
#endif

/** 
 * An example of how to dynamically build a command string based on 
 * user settings...
 *
 * Please note that this sample does not cover all possible options offered by 
 * PDF2SVG. For a detailed explanation of all commands, please refer to PDF2SVG 
 * SDK User Manual.
 */
#if EXAMPLE == 3
int main() 
{
	PDF2SVGInit(username, company, lic_key, resource_path);

	string output_folder = "../TestOutput/test2";
	string open_password = "secret";
	bool compress_using_svgz = true;
	bool embedimages = true;
	bool generate_xml_masterdoc = false;
	bool generate_thumbnails = false; 
	int thumbsize = 150;
	string filename_prefix = "";
	int filename_digits = 4;

	// Given the above settings build a command string.
	stringstream s;
	if (!output_folder.empty()) s << "-o " << output_folder << " ";
	if (!open_password.empty()) s << "-p " << open_password << " ";
	if (compress_using_svgz) s << "--svgz ";
	if (embedimages) s << "--embedimages ";

	if (generate_xml_masterdoc == false) s << "--noxmldoc ";
	if (generate_thumbnails) {
		s << "--thumbsize " << thumbsize << " ";
	}
	else {
		s << "--nothumbs ";
	}

	if (!filename_prefix.empty()) s << "--prefix " << filename_prefix << " ";
	if (filename_digits>0)  s << "--digits " << filename_digits << " ";

	// specify input PDF files and folders...
	s << "../../samples/TestFiles/tiger.pdf ";
	s << "../../samples/TestFiles/tai962.pdf "; 
	// s << "c:/my_pdf_folder "; etc ...


	// Execute the command string.
	vector<string> saved_files;
	PDF2SVGRun((char*)s.str().c_str(), MyCallback, &saved_files);

	//print the saved file(s)
	cout << "The Saved Files Are:" << endl;
	for(unsigned int i=0; i<saved_files.size(); i++)
	{
		cout << saved_files[i] << endl;
	}

	cout << "Done!" << endl;
	return 0;
}
#endif

