// entry point
#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <process.h>
#include <string>
#include <cstdlib>
#include <commdlg.h>
#include <fstream>
#include <sstream>
#include <cstring>
//#include <msclr\marshal_cppstd.h>
#include "Form1.h"
using namespace winForm1;
using namespace std;
using namespace System;
//using namespace msclr::interop;
#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;
}

// Our 'MAIN' which initialize openfilename and setName
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
//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 laArray[9];
//get ffmpeg output file
std::string filename("ffmpeg.txt");
std::ifstream ifs(filename.c_str());
const int MAXITEMS2 = 11;
string myList2[11] = {"title", "comment", "rating", "artist", "album", "date", "track", "genre", "bitrate", "Duration"};
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 < MAXITEMS2; i++)
{
size_t position = contents.find(myList2[i]);
string myString;
switch (i)
{
case 0:
if(position != std::string::npos)
{
myString=contents.substr(position + 17, 31);
laArray[0]=myString;
outFile << myString << endl;
}
break;
case 1:
if(position != std::string::npos)
{
myString=contents.substr(position + 0, 21);
laArray[1]=myString;
outFile << myString << endl;
}
break;
case 2:
if(position != std::string::npos)
{
myString=contents.substr(position + 0, 21);
laArray[2]=myString;
outFile << myString << endl;
}
break;
case 3:
if(position != std::string::npos)
{
myString=contents.substr(position + 0, 21);
laArray[3]=myString;
outFile << myString << endl;
}
break;
case 4:
if(position != std::string::npos)
{
myString=contents.substr(position + 0, 21);
laArray[4]=myString;
outFile << myString << endl;
}
break;
case 5:
if(position != std::string::npos)
{
myString=contents.substr(position + 0, 21);
laArray[5]=myString;
outFile << myString << endl;
}
break;
case 6:
if(position != std::string::npos)
{
myString=contents.substr(position + 0, 21);
laArray[6]=myString;
outFile << myString << endl;
}
break;
case 7:
if(position != std::string::npos)
{
myString=contents.substr(position + 0, 21);
laArray[7]=myString;
outFile << myString << endl;
}
break;
case 8:
if(position != std::string::npos)
{
myString=contents.substr(position + 0, 21);
laArray[8]=myString;
outFile << myString << endl;
}
break;
default:
if(position != std::string::npos)
{
myString=contents.substr(position + 0, 21);
outFile << myString << endl;
}

}
}
return 0;


// 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(laArray, 11));
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());
};