#include "mainForm.h"
#include <QApplication>
#include <QDir>
#include <QResource>
#include <QThread>
#include "QsLog.h"
#include "preDefine.h"
#include "Control/flowControl.h"
#include "floatForm.h"
#include "DTools/configManger.h"
#include <windows.h>
#include <QProcess>
#include <QSqlDatabase>
#include "DTools/dump.h"

using namespace QsLogging;

QString g_appDir;

QThread tcpThread;
QThread  workThread;

/*
void InitLogger()
{
    QString logDir = QString("%1/log").arg(g_appDir);
    Logger& logger = Logger::instance();
    logger.setLoggingLevel(TraceLevel);
    QDir().mkdir(logDir);
    QString logPath = QString("%1/%2").arg(logDir).arg("log");
    DestinationPtr fileDst(DestinationFactory::MakeFileDestination(
    logPath, EnableLogRotation, MaxSizeBytes(10*1024*1024), MaxOldLogCount(40)));
    logger.addDestination(fileDst);
    DestinationPtr consoleDst(DestinationFactory::MakeDebugOutputDestination());
    logger.addDestination(consoleDst);
}
*/

void InitLogger(){
    QString logDir = QString("%1/log").arg(g_appDir);
    QDir().mkdir(logDir);
    QsLogging::Logger& logger = QsLogging::Logger::instance();
    logger.setLoggingLevel(QsLogging::TraceLevel);
    //日志按天输出
    QsLogging::DestinationPtr fileDst(QsLogging::DestinationFactory::MakeFileDestination(logDir,"FmPlugin", 30,QsLogging::EnableLogRotation,
    QsLogging::MaxSizeBytes(10*1024*1024), QsLogging::MaxOldLogCount(50)));
    logger.addDestination(fileDst);
    QsLogging::DestinationPtr consoleDst(QsLogging::DestinationFactory::MakeDebugOutputDestination());
    logger.addDestination(consoleDst);
}

void LoadTheme(const QString& theme)
{
    QString rccPath = QString("%1/skin/%2.rcc").arg(g_appDir).arg(theme);
    if(!QFile(rccPath).exists())
    {
        return;
    }
    QResource::registerResource(rccPath);

    QFile qssFile(QString(":/%1.qss").arg(theme));
    if(!qssFile.open(QFile::ReadOnly))
    {
        return;
    }
    qApp->setStyleSheet(qssFile.readAll());
    qssFile.close();
}

int main(int argc, char *argv[])
{
#ifdef Q_OS_WIN
    SetUnhandledExceptionFilter(FMExcpHandler);
#endif
    HANDLE m_hMutex  =  CreateMutex(NULL, FALSE,  L"Global\\fmTakeaway" );
    if(NULL!=m_hMutex){
        if  (GetLastError()  ==  ERROR_ALREADY_EXISTS||GetLastError()  ==  ERROR_ACCESS_DENIED)  {
            CloseHandle(m_hMutex);
            m_hMutex  =  NULL;
            return  -1;
        }
    }
    else{
        return -1;
    }

    QApplication a(argc, argv);
    g_appDir = a.applicationDirPath();

    // 初始化日志
    InitLogger();

    // 加载主题
    LoadTheme(APP_THEME);

    QLOG_INFO() << QString("[<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<---->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>]");
    QLOG_INFO() << QString("[<<<<<<<<<<<<<<<<<<<<<<<-- fmTakeaway Start -->>>>>>>>>>>>>>>>>>>>>>>>>>>]");
    QLOG_INFO() << QString("[<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<---->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>]");

    // 将控制器移到工作线程
    FlowControl::GetInstance().moveToThread(&workThread);
    workThread.start();

    FloatForm f;
    MainForm w;
    QObject::connect(&w, &MainForm::showFloatForm, &f, &FloatForm::onShow);

    QObject::connect(&FlowControl::GetInstance(), &FlowControl::startRemind, &f, &FloatForm::onStartRemind);
    QObject::connect(&FlowControl::GetInstance(), &FlowControl::doHideMainShowFloatFrom, &w, &MainForm::on_mainBtnHide_clicked);

    QObject::connect(&FlowControl::GetInstance(), &FlowControl::doLockFloatForm, &f, &FloatForm::onLockFloatForm);
    QObject::connect(&FlowControl::GetInstance(), &FlowControl::doUnLockFloatFrom, &f, &FloatForm::onUnLockFloatForm);
    QObject::connect(&FlowControl::GetInstance(), &FlowControl::doUpdateStoreStatus, &f, &FloatForm::onUpdateStoreStatus);

    QObject::connect(&w, &MainForm::startRemind, &f, &FloatForm::onStartRemind);
    QObject::connect(&w, &MainForm::stopRemind, &f, &FloatForm::onStopRemind);
    QObject::connect(&f, &FloatForm::showMainForm, &w, &MainForm::show);
    w.MyShow();

    return a.exec();
}
