#include "HeaderSort.h"
#include "AllHead.h"
#include "WBCLFZSystemModule.h"
#include <QFile>
#include <QTextStream>
#include <QtWidgets/QApplication>
#include <QCoreApplication>
#include <QDir>
#include <QDebug>
#include <QDateTime>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <QMessageBox>
#include "SqliteDBProcess/src/Application.h"
#include "SqliteDBProcess/src/sqlite.h"
 



void LoadProgramParams() {

    // 创建QSettings对象，指定配置文件路径
    QSettings settings("config.ini", QSettings::IniFormat);
 
    QString variable = settings.value("variable", "Default Value").toString();
    qDebug() << "Loaded variable:" << variable;

    // 修改程序变量
    variable = "New Value";
    settings.setValue("variable", variable);
    qDebug() << "Updated variable:" << variable;
}


void TestOCCT() {
    gp_Vec Z_vec(gp_Pnt(0, 0, 0), gp_Pnt(0, 0, 1));
    gp_Vec X_vec(gp_Pnt(0, 0, 0), gp_Pnt(1, 0, 0));
    gp_Vec Y_vec(gp_Pnt(0, 0, 0), gp_Pnt(0, 1, 0));
    Standard_Real rotationAngle = 90;
    rotationAngle = rotationAngle * M_PI / 180;

    gp_Trsf fly_Z_trsf;
    fly_Z_trsf.SetRotation(gp_Ax1(gp_Pnt(0, 0, 0), X_vec), rotationAngle);   //   Y --> Z   逆时针为 正 

    qDebug() << QString("Y_vec before:( %1 , %2 , %3 )").arg(Y_vec.X()).arg(Y_vec.Y()).arg(Y_vec.Z());
    Y_vec.Transform(fly_Z_trsf);
    qDebug() << QString("Y_vec after:( %1 , %2 , %3 )").arg(Y_vec.X()).arg(Y_vec.Y()).arg(Y_vec.Z());

    qDebug() << QString("X_vec before:( %1 , %2 , %3 )").arg(X_vec.X()).arg(X_vec.Y()).arg(X_vec.Z());
    X_vec.Transform(fly_Z_trsf);
    qDebug() << QString("Y_vec after:( %1 , %2 , %3 )").arg(X_vec.X()).arg(X_vec.Y()).arg(X_vec.Z());

    qDebug() << QString("Z_vec before:( %1 , %2 , %3 )").arg(Z_vec.X()).arg(Z_vec.Y()).arg(Z_vec.Z());
    Z_vec.Transform(fly_Z_trsf);
    qDebug() << QString("Z_vec after:( %1 , %2 , %3 )").arg(Z_vec.X()).arg(Z_vec.Y()).arg(Z_vec.Z());
}
 

// 自定义消息处理程序
void LogTextcustomMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString& msg)
{

    // -------- 日志输出   ----------------------------------------------------------------------------------

    // 获取应用程序执行文件的路径
    QString exePath = QCoreApplication::applicationFilePath();

    // 使用QFileInfo获取父目录
    QFileInfo fileInfo(exePath);
    QString parentDir = fileInfo.dir().path();

    // 获取当前日期和时间
    QDateTime currentDateTime = QDateTime::currentDateTime();
    // 格式化日期和时间为字符串，精确到秒
    QString dateTimeString = currentDateTime.toString("yyyyMMdd_hh");
    // 创建文件名
    QString fileName = parentDir + "/logs/" + "log_" + dateTimeString + ".log";
    // 创建一个日志文件
    QFile logFile(fileName);
    logFile.open(QIODevice::WriteOnly | QIODevice::Append);
    QTextStream textStream(&logFile);
    // 输出到控制台
    QTextStream consoleStream(stdout);

    // 写入日志信息
    switch (type) {
    case QtDebugMsg:
        textStream << "[Debug] ["+ currentDateTime.toString("yyyy-MM-dd hh:mm:ss")+"] ";
        consoleStream << "[Debug] [" + currentDateTime.toString("yyyy-MM-dd hh:mm:ss") + "] ";
        break;
    case QtWarningMsg:
        textStream << "[Warning] [" + currentDateTime.toString("yyyy-MM-dd hh:mm:ss") + "] ";
        consoleStream << "[Warning] [" + currentDateTime.toString("yyyy-MM-dd hh:mm:ss") + "] ";
        break;
    case QtCriticalMsg:
        textStream << "[Critical] [" + currentDateTime.toString("yyyy-MM-dd hh:mm:sss") + "] ";
        consoleStream << "[Critical] [" + currentDateTime.toString("yyyy-MM-dd hh:mm:sss") + "] ";
        break;
    case QtFatalMsg:
        textStream << "[Fatal] [" + currentDateTime.toString("yyyy-MM-dd hh:mm:ss") + "] ";
        consoleStream << "[Fatal] [" + currentDateTime.toString("yyyy-MM-dd hh:mm:ss") + "] ";
        break;
    }
    //QByteArray localMsg = msg.toLocal8Bit();
    //const char* file = context.file ? context.file : "";
    //const char* function = context.function ? context.function : "";
    //localMsg = QString("%1 (%2, %3:%4)\n").arg(msg).arg(function).arg(file).arg(context.line).toLocal8Bit();
    consoleStream << msg << Qt::endl;  // 输出到控制台
    textStream << msg << Qt::endl;// 设置文本流
 
}



static QString message = QString();

void db4sMessageOutput(QtMsgType type, const QMessageLogContext& context, const QString& msg)
{
    QByteArray localMsg = msg.toLocal8Bit();

    // Emulate the default Qt treatment. This might not work in some OS, like Windows.
    fprintf(stderr, "%s\n", localMsg.constData());

    const char* file = context.file ? context.file : "";
    const char* function = context.function ? context.function : "";
    localMsg = QString("%1 (%2, %3:%4)\n").arg(msg).arg(function).arg(file).arg(context.line).toLocal8Bit();

    // Log using the SQLite log mechanism, so it gets the same treatment than messages
    // reported by SQLite itself. This will allow these messages to be seen in our Log window.
    // Error code will be the type
    sqlite3_log(static_cast<int>(type), localMsg.constData());
}

void boxMessageOutput(QtMsgType, const QMessageLogContext&, const QString& msg)
{
    message += msg + "\n";
}



int ProcessdBTestmain(int argc, char** argv)
{

#ifdef Q_OS_WIN
    // In Windows, there is no output to terminal for a graphical application, so we install
    // the output to message box, until the main window is shown or the application exits.
    qInstallMessageHandler(LogTextcustomMessageHandler);
#endif

    // Create application object. All the initialisation stuff happens in there
    Application a(argc, argv);

    // If there has been invocations to the message handler, show it to user in a message box.
    if (!message.isEmpty()) {
        QMessageBox messageBox;
        messageBox.setTextFormat(Qt::RichText);
        messageBox.setText("<pre>" + message.toHtmlEscaped() + "</pre>");
        messageBox.exec();
    }

    // Quit application now if user doesn't want to see the UI
    if (a.dontShowMainWindow())
        return 0;

    qInstallMessageHandler(LogTextcustomMessageHandler);

    // Run application
    return a.exec();
}


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

   // ProcessdBTestmain(argc, argv);

    QApplication a(argc, argv);
    qInstallMessageHandler(LogTextcustomMessageHandler);
    TestOCCT();
    // 示例输出
    qDebug() << "Program is init debug log out ...";
    qWarning() << "Program is init waring log out ...";
    qCritical() << "Program is init Critical log out ...";

    // -------- 测试程序正式执行 ----------------------------------------------------------------------------------
    WBCLFZSystemModule* w=new WBCLFZSystemModule();  // 
    w->show();
    return a.exec();

}








