// entry point
#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <process.h>
#include <string>
#include <cstdlib>
#include <commdlg.h>
#include "Form1.h"
#include <vector>
#include <fstream>
#include <sstream>
#include <cstring>

using namespace winForm1;
using namespace std;
using std::vector;
using namespace System;
#pragma comment(lib, "comdlg32.lib")

//function prototypes
void setTagName(string fileNameStr);
void setffmpeg(string fileNameStr);
void setmd5(string fileNameStr);
std::string setValues_fromFfmpeg();

// Open file dialog box
string openfilename(char *filter = "All Files (*.*)\0*.*\0", HWND owner = NULL) {
	OPENFILENAME ofn;
	char fileName[MAX_PATH] = "";
	ZeroMemory(&ofn, sizeof(ofn));
	ofn.lStructSize = sizeof(OPENFILENAME);
	ofn.hwndOwner = owner;
	ofn.lpstrFilter  = filter;
	ofn.lpstrFile = fileName;
	ofn.nMaxFile = MAX_PATH;
	ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
	ofn.lpstrDefExt = "";
	string fileNameStr;
	if ( GetOpenFileName(&ofn) )
		fileNameStr = fileName;
	return fileNameStr;
}

//string openfilename(char *filter = "All Files (*.*)\0*.*\0", HWND owner = NULL){

//char szFileName[100];
//OPENFILENAME openFileDialog;

//ZeroMemory(&openFileDialog, sizeof(openFileDialog));
//			openFileDialog.lStructSize= sizeof(openFileDialog);
//			openFileDialog.hwndOwner = owner;
//			openFileDialog.lpstrFilter = filter;
//			openFileDialog.lpstrFile = szFileName;
//			openFileDialog.nMaxFile = MAX_PATH;
//			openFileDialog.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
//			openFileDialog.lpstrDefExt = "";
//			string fileNameStr;
//			if(GetOpenFileName(&openFileDialog))
//				fileNameStr = szFileName;
//			return fileNameStr;
//}

// Our 'MAIN' which initialize openfilename and setName
[STAThreadAttribute]
int main(array<System::String^> ^args){
	setValues_fromFfmpeg();
	// Enabling Windows XP visual effects before any controls are created
	Application::EnableVisualStyles();
	Application::SetCompatibleTextRenderingDefault(false); 
	// Create the main window and run it
	Application::Run(gcnew Form1());
	//form1::button1_Click():
	return 0;
}

//function to dump media file idv3 tags using the DOS program tag
void setTagName(string fileNameStr){
	{
		const int MAXITEMS = 7;
		string myList[MAXITEMS] = {"TITLE", "ARTIST", "ALBUM", "YEAR", "TRACK", "GENRE", "COMMENT"};
		//loop to inject myList array data one at a time because output in file delivers only first field
		for (int i = 0; i < MAXITEMS; i++){
			//each time we have to cycle through for each tag
			string command = "tag --nocheck --tostdoutn \""+ myList[i]+"\" \"";
			command+= fileNameStr; command += "\" >> \"output.txt\"";
			system(command.c_str());
			//here we open file to add a new line
			ofstream myfile;
			myfile.open ("output.txt",ios_base::app);
			myfile << ":" + myList[i] + "\n";
			myfile.close();
		}
	}
				
}
//function to dump media file idv3 tags and file info using the DOS program ffmpeg
void setffmpeg(string fileNameStr){
		string command = "ffmpeg -i ";
		command+= fileNameStr; command += "> ffmpeg.txt 2>>&1";
		system(command.c_str());
}
//function to dump md5 hash using md5deep
void setmd5(string fileNameStr){
		string command = "md5deep ";
		command+= fileNameStr; command += "> md5.txt 2>>&1";
		system(command.c_str());
};

string setValues_fromFfmpeg(){
		//data collection process
		string fileNameStr;
		fileNameStr = openfilename();
		cout << fileNameStr.c_str();
		cin.ignore();
		setTagName(fileNameStr.c_str());
		setffmpeg(fileNameStr.c_str());
		setmd5(fileNameStr.c_str());
		//setValues_fromFfmpeg();
		//set array variable to be passed to display windows
		std::string filename("ffmpeg.txt");
		std::ifstream ifs(filename.c_str());
		string myList2[16] = {"title", "comment", "rating", "disc", "album_artist", "date", "track", "media_type", "genre", "bitrate", "Duration", "season_number", "composer", "episode_sort", "major_brand", "minor_version"};
		std::stringstream oss;
		oss << ifs.rdbuf();
		if(!ifs && !ifs.eof())
		std::cerr << "Error reading file '" << filename << "'\n";
		std::string contents(oss.str());
		std::ofstream outFile;
		outFile.open("ffmpeg2.txt");
		//begin loop
		for (int i = 0; i < 15; i++){
			size_t position = contents.find(myList2[i]);
			switch (i){
				case 0:
				if(position != std::string::npos){
					laVector.at(0)=contents.substr(position + 17, 31);
				}
				break;
				case 1:
				if(position != std::string::npos){
					laVector.at(1)=contents.substr(position + 17, 31);
				}
				break;
				case 2:
				if(position != std::string::npos){
					laVector.at(2)=contents.substr(position + 17, 31);
				}
				break;
				case 3:
				if(position != std::string::npos){
					laVector.at(3)=contents.substr(position + 17, 31);
				}
				break;
				case 4:
				if(position != std::string::npos){
					laVector.at(4)=contents.substr(position + 17, 31);
				}
				break;
				case 5:
				if(position != std::string::npos){
					laVector.at(5)=contents.substr(position + 17, 31);
				}
				break;
				case 6:
				if(position != std::string::npos){
					laVector.at(6)=contents.substr(position + 17, 31);
				}
				break;
				case 7:
				if(position != std::string::npos){
					laVector.at(7)=contents.substr(position + 17, 31);
				}
				break;
				case 8:
				if(position != std::string::npos){
					laVector.at(8)=contents.substr(position + 17, 31);
				}
				break;
				case 9:
				if(position != std::string::npos){
					laVector.at(9)=contents.substr(position + 9, 31);
				}
				break;
				case 10:
				if(position != std::string::npos){
					laVector.at(10)=contents.substr(position + 6, 9);
				}
				break;
				case 11:
				if(position != std::string::npos){
					laVector.at(11)=contents.substr(position + 17, 31);
				}
				break;
				case 12:
				if(position != std::string::npos){
					laVector.at(12)=contents.substr(position + 17, 31);
				}
				break;
				case 13:
				if(position != std::string::npos){
					laVector.at(13)=contents.substr(position + 17, 31);
				}
				break;
				case 14:
				if(position != std::string::npos){
					laVector.at(14)=contents.substr(position + 17, 31);
				}
				break;
				case 15:
				if(position != std::string::npos){
					laVector.at(15)=contents.substr(position + 17, 31);
				}
				break;
				default:
				if(position != std::string::npos){
				}

			}
		}
		return "";
}