#include <QApplication>
#include <QDir>
#include "mainwindow.h"
#include "logger.h"
#include "appcontext.h"
#include "version.h"
#include "utils.h"

#define SCREEN_WIDTH    1280
#define SCREEN_HEIGHT   720

int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
    QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
    QApplication app(argc, argv);

#ifdef NDEBUG
    Logger::setup(Logger::INFO, "storage/logs/log.txt");
#else
    Logger::setup(Logger::TRACE, "storage/logs/log.txt");
#endif

    CRITICAL_LOG(spdlog::level::info, "app start...");

    char *ip = NULL;
    char *mask = NULL;
    bool result = Utils::ipv4("eth0", &ip, &mask);

    LOG_INFO("PROJECT_NAME:         {}", PROJECT_NAME);
    LOG_INFO("PROJECT_VERSION:      {}", PROJECT_VERSION);
    LOG_INFO("PROJECT_GIT_HASH:     {}", PROJECT_GIT_HASH);
    LOG_INFO("PROJECT_GIT_BRANCH:   {}", PROJECT_GIT_BRANCH);
    LOG_INFO("PROJECT_BUILD_TIME:   {}", PROJECT_BUILD_TIME);
    if(result) {
        LOG_INFO("LOCAL IPv4:           {}/{}", ip, mask);
    } else {
        LOG_ERROR("get local ipv4 error");
    }

    QString broadcast = Utils::broadcast(ip, mask);
    LOG_INFO("BROADCAST:            {}", broadcast.toUtf8().data());
    free(ip);
    free(mask);

    if(argc != 2) {
        LOG_ERROR("miss arg: frequency");
        return -1;
    }

    int frequency = QString(argv[1]).toInt();

    AppContext &appCtx = AppContext::instance();

    MainWindow w(frequency);
    w.resize(SCREEN_WIDTH, SCREEN_HEIGHT);
    w.showFullScreen();

    int ret = app.exec();
    return ret;
}