#include "mainwindow.h"

#include <QApplication>
#include <QMessageBox>

#include "charcode.h"
#include "utils.h"
#include "Application.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    a.setWindowIcon(QIcon(":/Resources/App.ico"));

    //设置中文字体
    a.setFont(QFont("Microsoft Yahei", 9));

    QString qss = utils::readFile(":/Resources/style.qss");
    a.setStyleSheet(qss);
    try {
        // 初始化服务层
        Application::instance()->init();
        if (!Application::instance()->getDatabaseConnector()->isOpen())
        {
            QMessageBox::critical(nullptr, "错误", "数据库不可访问，程序无法运行!");
            return 0;
        }
        else
        {
            MainWindow w;
            w.show();
            return a.exec();
        }
    }
    catch (MessageException &e)
    {
        QMessageBox::critical(0, "错误", e.getMessage());
    }
    catch (std::exception &e)
    {
        QString msg;
        msg.append("错误: ");
        msg.append(e.what());
        QMessageBox::critical(0, "Error", msg);
    }
}
