#include "login.h"

#include <QApplication>
#include <QFileInfo>
#include <QSettings>
#include "logproc.h"
#include <windows.h>
#include <DbgHelp.h>
#include "CLogTrack.h"
#include <QProcess>
#include "client/windows/handler/exception_handler.h"



long  __stdcall CrashInfocallback(_EXCEPTION_POINTERS *pexcp)
{
    //创建 Dump 文件
    HANDLE hDumpFile = ::CreateFile(
                L"client.dmp",
                GENERIC_WRITE,
                0,
                NULL,
                CREATE_ALWAYS,
                FILE_ATTRIBUTE_NORMAL,
                NULL
                );
    if (hDumpFile != INVALID_HANDLE_VALUE)
    {
        //Dump信息
        MINIDUMP_EXCEPTION_INFORMATION dumpInfo;
        dumpInfo.ExceptionPointers = pexcp;
        dumpInfo.ThreadId = GetCurrentThreadId();
        dumpInfo.ClientPointers = TRUE;
        //写入Dump文件内容
        ::MiniDumpWriteDump(
                    GetCurrentProcess(),
                    GetCurrentProcessId(),
                    hDumpFile,
                    MiniDumpNormal,
                    &dumpInfo,
                    NULL,
                    NULL
                    );
    }

    EXCEPTION_RECORD* record = pexcp->ExceptionRecord;
    QString errCode(QString::number(record->ExceptionCode,16)),
            errAdr(QString::number((uint)record->ExceptionAddress,16));

    HWND task = nullptr;
    task=FindWindow(L"Shell_TrayWnd",nullptr);
    if(task)
    {
        ShowWindow(task,SW_SHOW);//隐藏任务栏
    }

    QMessageBox::critical(NULL,QString::fromLocal8Bit("程序崩溃"),
                          QString::fromLocal8Bit("<FONT size=4><div><b>对于发生的错误，表示诚挚的歉意</b><br/></div>")+
        QString::fromLocal8Bit("<div>错误代码：%1</div><div>错误地址：%2</div></FONT>").arg(errCode).arg(errAdr),
        QMessageBox::Ok);
    myServerLog()<<QString::fromLocal8Bit("程序崩溃,错误代码：%1,错误地址：%2").arg(errCode).arg(errAdr);

    return 0;
}

bool callback(const wchar_t *dump_path, const wchar_t *id,
	void *context, EXCEPTION_POINTERS *exinfo,
	MDRawAssertionInfo *assertion,
	bool succeeded)
{
	EXCEPTION_RECORD* record = exinfo->ExceptionRecord;
	QString errCode(QString::number(record->ExceptionCode, 16)),
		errAdr(QString::number((uint)record->ExceptionAddress, 16));

	HWND task = nullptr;
	task = FindWindow(L"Shell_TrayWnd", nullptr);
	if (task)
	{
		ShowWindow(task, SW_SHOW);//隐藏任务栏
	}

	QMessageBox::critical(NULL, QString::fromLocal8Bit("程序崩溃"),
		QString::fromLocal8Bit("<FONT size=4><div><b>对于发生的错误，表示诚挚的歉意</b><br/></div>") +
		QString::fromLocal8Bit("<div>错误代码：%1</div><div>错误地址：%2</div></FONT>").arg(errCode).arg(errAdr),
		QMessageBox::Ok);
	myDebug() << QString::fromLocal8Bit("程序崩溃,错误代码：%1,错误地址：%2").arg(errCode).arg(errAdr);

	return succeeded;
}

int main(int argc, char *argv[])
{    
	QApplication a(argc, argv);

	
    ::SetUnhandledExceptionFilter((LPTOP_LEVEL_EXCEPTION_FILTER)CrashInfocallback);

	g_appInfoPtr = std::make_shared<CAppInfo>();	

    QFileInfo file("coe.cfgi");
	QString sFilePath = file.absoluteFilePath();
	QSettings set(sFilePath, QSettings::IniFormat);

    g_appInfoPtr->m_bShowDebugInfo = set.value("config/debug", false).toBool();


	qInstallMessageHandler(CustomOutputMessage);

//	google_breakpad::ExceptionHandler eh(
//		L".", NULL, callback, NULL,
//		google_breakpad::ExceptionHandler::HANDLER_ALL);
		
	
    login w;
    w.show();

    QProcess process;
    process.start("taskkill /f /im launcher.exe");
    process.waitForFinished();


	if (argc > 1)
	{
		QString sParam = argv[1];
		if (sParam == "updateLauncher")
		{
            QString sSourceFile = g_appInfoPtr->m_sCacheFileDir + "uptemp/launcher.exe";
			QString sDestFileName = "launcher.exe";

			QFile file(sSourceFile);
			if (file.exists())
			{
				QFile::remove(sDestFileName);
				bool bcopy = file.copy(sDestFileName);
				myDebug() << bcopy;
			}
			return a.exec();
		}
		else if (sParam == "runClient")
		{
			return a.exec();
		}
		else
		{
			return  0;
		}
	}
	else
	{

#ifdef _DEBUG
        return a.exec();
#else
        return 0;
#endif
	}
}

