#include "directorymapper.h"
#include "functions.h"
#include "fileIO.h"
#include <iostream>
#include <QtGUI>

using namespace std;

string APP_VERSION = "0.1.0";

DirectoryMapper::DirectoryMapper(QWidget *parent)
    : QMainWindow(parent)
{
	ui.setupUi(this);
	
	// Update User Interface
	string title = "Directory Mapper " + APP_VERSION;
	this->setWindowTitle(title.c_str());
	ui.authorLabel->setText("Created By Matthew B. Gately (mgately@code85.com)");
	
	// Connect Signals and Slots
	connect(ui.exitButton, SIGNAL(clicked()), this, SLOT(close()));
	connect(ui.mapButton, SIGNAL(clicked()), this, SLOT(mapDirectories()));
	//connect(ui.make1FileButton, SIGNAL(clicked()), this, SLOT(make1File()));
	
	// Menu Bar Signals and Slots
	connect(ui.actionExit, SIGNAL(triggered()), this, SLOT(close())); // File->Exit
	connect(ui.actionAbout, SIGNAL(triggered()), this, SLOT(aboutDialog())); // Help->About
	
	// Set Status Tips - These Show In The Status Bar When Mouse Is Hovered
	ui.actionExit->setStatusTip(tr("Exit This Application")); // File->Exit
	
	ui.directoryOnlyRadioButton->setChecked(true);
	
	// Update Application Progress Display
	ui.appProgress1->setEnabled(false);
	ui.appProgress2->setEnabled(false);
	ui.appProgress3->setEnabled(false);
	
	ui.appProgress1->setText("Calculating # Files and Folders");
	ui.appProgress2->setText("Mapping Files and Folders");
	ui.appProgress3->setText("Writing HTML File");
}

DirectoryMapper::~DirectoryMapper()
{

}

void DirectoryMapper::aboutDialog(void) {
	// Create The About Dialog Text
	string aboutDialogText = "<h1>Directory Mapper " + APP_VERSION + "</h1>";
	aboutDialogText += "<p>This program indexes the selected directory and creates";
	aboutDialogText += " an HTML file displaying all folders and files.</p>";
	aboutDialogText += "<p>Created By Matthew B. Gately (mgately@code85.com)</p>";
	
	// Display The About Dialog
	QMessageBox::about(this, tr("About Dialog"), aboutDialogText.c_str());
}

void DirectoryMapper::mapDirectories(void) {
	string htmlFile = "";
	string filePath = "";
	string outputFilePath = "";
	QString qFilePath = "";
	int mode, numFiles, numFolders;
	numFiles = 0;
	numFolders = 0;
	
	htmlFile = "<HTML>";
	
	// Reset Application Progress
	ui.appProgress1->setEnabled(false);
	ui.appProgress2->setEnabled(false);
	ui.appProgress3->setEnabled(false);
	
	ui.appProgress1->setText("Calculating # Files and Folders");
	ui.appProgress2->setText("Mapping Files and Folders");
	ui.appProgress3->setText("Writing HTML File");
	qApp->processEvents();
	
	ui.appProgress1->setEnabled(true);
	qApp->processEvents();
	
	// Get File Path
	qFilePath = ui.rootDirectoryLineEdit->text();
	QStringToString(qFilePath, filePath);
	
	// Get Output File Path
	qFilePath = ui.outputFileLineEdit->text();
	QStringToString(qFilePath, outputFilePath);
	
	// Verify That Path Exists
	// If Path Does Not Exist - Warn User Exit Function
	if (isDirectory(filePath.c_str()) == false) {
		// Message Box Warning
		QMessageBox::about(this, tr("File Does Not Exist"), "Warning - Root Directory Folder Does Not Exist");
		// Exit Function
		return;
	}
	if (outputFilePath == "") {
		QMessageBox::about(this, tr("Output File Not Specified"), "Warning - Output File Path Not Specified");
	}
	
	
	// Get The Number Of Files and Folders
	getNumFilesAndFolders(filePath.c_str(), numFiles, numFolders);
	ui.appProgress1->setText("<font color=green>Number Files and Folders Calculated</font>");
	qApp->processEvents();
	
	htmlFile += "<h1>Directory Information</h1>";
	htmlFile += "<b>Number of Folders: </b>" + integerToString(numFolders) + "<br>";
	htmlFile += "<b>Number of Files: </b>" + integerToString(numFiles) + "<br>";
	
	
	ui.appProgress2->setEnabled(true);
	qApp->processEvents();
	// Determine If You Should Map Just Folders or Files too
	if (ui.directoryOnlyRadioButton->isChecked() == true) {
		mode = 0;
	} else {
		mode = 1;
	}
	
	// Write Directory Map Header To HTML
	if (mode == 0) {
		htmlFile += "<h1>Directory Map</h1>";
	} else {
		htmlFile += "<h1>Directory and File Map</h1>";
	}
	
	mapFilesAndFolders(filePath.c_str(), mode, htmlFile);
	ui.appProgress2->setText("<font color=green>Directory Information Mapped</font>");
	qApp->processEvents();
	
	ui.appProgress3->setEnabled(true);
	qApp->processEvents();
	htmlFile += "</HTML>";
	writeBinaryFile(outputFilePath.c_str(), htmlFile);
	ui.appProgress3->setText("<font color=green>HTML Written</font>");
	qApp->processEvents();
	
	// If Requested Open Report After Generation
	if (ui.openCheckBox->isChecked() == true) {
		string openOutputFileCommand = "explorer " + outputFilePath;
		system(openOutputFileCommand.c_str());		
	}

}

