#include "LAMPDataProcessEXE.h"
#include <QtWidgets/QApplication>
#include <QCoreApplication>
#include <QDebug>
#include <QFile>
#include <QTextStream>
#include <QDateTime>
#include "LAMPMainWidgetRunProgram.h"
#include "RasterWidgetMessageShow.h"


#pragma execution_character_set("utf-8")

// 自定义消息处理器函数
void customMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString& msg)
{
    QByteArray localMsg = msg.toLocal8Bit();
    const char* file = context.file ? context.file : "";
    const char* function = context.function ? context.function : "";
    QString dateTime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz");

    QFile outFile("application.log");
    outFile.open(QIODevice::WriteOnly | QIODevice::Append);
    QTextStream ts(&outFile);
	
	RasterMessageShow::RasterWidgetMessageShow* _showIntance = RasterMessageShow::RasterWidgetMessageShow::getInstance();

	switch (type) {
	case QtDebugMsg:
	{
		QString logMessage = QString("%1 Debug: %2 (%3:%4, %5)")
			.arg(dateTime) // Assuming dateTime is a QDateTime object and needs to be converted to string
			.arg(QString::fromLocal8Bit(localMsg.constData()))
			.arg(file)
			.arg(context.line)
			.arg(function);
		ts << logMessage << "\n";
		_showIntance->ShowMessageInfo(logMessage);
		break;
	}
	case QtInfoMsg:
	{
		QString logMessage = QString("%1 Info: %2 (%3:%4, %5)")
			.arg(dateTime)
			.arg(QString::fromLocal8Bit(localMsg.constData()))
			.arg(file)
			.arg(context.line)
			.arg(function);
		ts << logMessage << "\n";
		_showIntance->ShowMessageInfo(logMessage);
		break;
	}
	case QtWarningMsg:
	{
		QString logMessage = QString("%1 Warning: %2 (%3:%4, %5)")
			.arg(dateTime)
			.arg(QString::fromLocal8Bit(localMsg.constData()))
			.arg(file)
			.arg(context.line)
			.arg(function);
		ts << logMessage << "\n";
		break;
	}
	case QtCriticalMsg:
	{
		QString logMessage = QString("%1 Critical: %2 (%3:%4, %5)")
			.arg(dateTime)
			.arg(QString::fromLocal8Bit(localMsg.constData()))
			.arg(file)
			.arg(context.line)
			.arg(function);
		ts << logMessage << "\n";
		_showIntance->ShowMessageInfo(logMessage);
		break;
	}
	case QtFatalMsg:
	{
		QString logMessage = QString("%1 Fatal: %2 (%3:%4, %5)")
			.arg(dateTime)
			.arg(QString::fromLocal8Bit(localMsg.constData()))
			.arg(file)
			.arg(context.line)
			.arg(function);
		ts << logMessage<<"\n";
		_showIntance->ShowMessageInfo(logMessage);
		abort();
	}

	}

}

int main(int argc, char *argv[])
{
	qInstallMessageHandler(customMessageHandler);
    QApplication a(argc, argv);
    RasterMainWidgetRun();
    return a.exec();
}
