#include "mainwindow.h"
#include <QApplication>
#include <QFile>
#include <QDateTime>
#include <QDir>
#include <QDebug>
#include <QStandardPaths>
#include <QScreen>
QString logFileFullName;

//回调函数实现debug信息到文件
void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
    QByteArray localMsg = msg.toLocal8Bit();
    QString logText = "";
    switch (type)
    {
        case QtDebugMsg:
            //logText = QString("\tDebug\t %1 (%2:%3, %4)").arg(localMsg.constData()).arg(context.file).arg(context.line).arg(context.function);
            logText = QString("\tDebug\t %1").arg(localMsg.constData());
            break;
        case QtInfoMsg:
            logText = QString("\tInfo\t %1").arg(localMsg.constData());
            break;
        case QtWarningMsg:
        {
#ifdef QT_NO_WARNING_OUTPUT
#else
            logText = QString("\tWarning\t %1").arg(localMsg.constData());
#endif
        }
        break;
        case QtCriticalMsg:
            logText = QString("\tCritical\t %1").arg(localMsg.constData());
            break;
        case QtFatalMsg:
            logText = QString("\tFatal\t %1").arg(localMsg.constData());
            abort();
    }

    QDateTime dt = QDateTime::currentDateTime();//获取系统现在的时间
    QString current_date = dt.toString("yyyy/MM/dd hh:mm:ss");

    QFile outFile(logFileFullName);
    if(outFile.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text))
    {
        if(logText.compare("") != 0)
        {
            QTextStream logTextStream(&outFile);
            logTextStream << current_date << logText << Qt::endl;
        }
        outFile.flush();
        outFile.close();
    }
    else
    {
        qDebug() << "Failed to open log file.";
    }
}

static void installMsgHandler()
{
    //create a logfilename with current datatime
    QDateTime dt = QDateTime::currentDateTime();//get the current system datatime
    QString current_date = dt.toString("yyyy-MM-dd.hh-mm-ss");

    //qDebug() << QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
    QString folderPath = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation) + "/PoleMaster";
    folderPath = QDir::toNativeSeparators(folderPath);
    QString logPath = folderPath + "/Log";
    logPath = QDir::toNativeSeparators(logPath);

    QDir logdir;
    if(!logdir.exists(folderPath))
        logdir.mkdir(folderPath);   //create polemaster folder in Windows-"C:/Users/<USER>/Documents"   OS X-"~/Documents"
    if(!logdir.exists(logPath))
        logdir.mkdir(logPath);    //create sub folder log in /users/.../documetn/polemaster/

    QString logfileName = current_date + ".log";
    logFileFullName = logPath + "/" + logfileName;
    logFileFullName = QDir::toNativeSeparators(logFileFullName);

#ifdef QT_NO_DEBUG_OUTPUT
    qInstallMessageHandler(myMessageOutput);
#else
    qInstallMessageHandler(0);  //To restore the message handler, will output to console
#endif

}


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

#if(QT_VERSION >= QT_VERSION_CHECK(4,0,0))
    QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif

    QApplication a(argc, argv);

    const float DEFAULT_DPI = 96.0;
    QList<QScreen*> screens = QApplication::screens();

    if (screens.size() > 0)
    {
        QScreen* screen = screens[0];
        double dpiX = screen->logicalDotsPerInch();
        //rate = dpiX / 96.0;
        float fontSize = dpiX / DEFAULT_DPI;
        if (fontSize < 1.1)
        {
            fontSize = 1.0;
        }
        else if (fontSize < 1.4)
        {
            fontSize = 1.25;
        }
        else if (fontSize < 1.6)
        {
            fontSize = 1.5;
        }
        else if (fontSize < 1.8)
        {
            fontSize = 1.75;
        }
        else
        {
            fontSize = 2.0;
        }
        QFont font = a.font();
        font.setPointSize(font.pointSize()*fontSize);
        a.setFont(font);
    }
#else
    QApplication a(argc, argv);
#endif

    installMsgHandler();

    // Load an application style
    QFile styleFile( ":/qss/default.qss" );
    styleFile.open( QFile::ReadOnly );
    if(styleFile.isOpen())
    {
        // Apply the loaded stylesheet
        QString style( styleFile.readAll() );
        a.setStyleSheet( style );
        styleFile.close();
    }

    MainWindow w;
    w.show();

    return a.exec();
}
