#include "connexion.h"
#include "copyMyChoices.h"

#include <QApplication>
#include <QDebug>
#include <QFile>
#include <QTextStream>
#include <QString>
#include <QMessageBox>
#include <iostream> 

void myMessageOutput(QtMsgType type, const char *msg)
 {
     QFile file(qApp->applicationDirPath ()+"/errorLog.txt");
     file.open(QIODevice::ReadWrite|QIODevice::Append);
     QTextStream s(&file);
     switch (type) {
     case QtDebugMsg:
         s << "Debug: " << QString(msg) << "\n";
         break;
     case QtWarningMsg:
         s << "Warning: " << QString(msg) << "\n";
         break;
     case QtCriticalMsg:
         s << "Critical: " << QString(msg) << "\n";
         break;
     case QtFatalMsg:
         s << "Fatal: " << QString(msg) << "\n";
         abort();
     }

     file.close();
 }

int main (int argc, char * argv[])
{
        qInstallMsgHandler(myMessageOutput);
	QApplication a(argc,argv);
        QFile f(a.applicationDirPath ()+"/errorLog.txt");
        f.remove();
	QString arg;
	QString file;
	QStringList args;
	args = a.arguments();
	if (args.size()>1)
	{
		  file = a.arguments()[1];
	    }
        QFile returnFile(a.applicationDirPath()+"/../../../../tmp/returnFile.txt");
	if (!returnFile.open(QIODevice::ReadWrite|QIODevice::Text))
	{
		QMessageBox::warning(0,"Warning","Exchange file cannot open",QMessageBox::Ok);
	    }
	QTextStream stream(&returnFile);
	Common::Connexion * c = new Common::Connexion;
	if (!c->connect())
	{
		  qWarning() << __FILE__ << QString::number(__LINE__) << "cannot connect to database" ;
	    }
	CopyMyChoices * copy = new CopyMyChoices(0);
	copy->run(arg);
	stream << copy->returnString();
	int r = a.exec();
	//QMessageBox::information(0,"Warning",copy->returnString(),QMessageBox::Ok);
	

	return r;
}
