#include <QApplication>
#include <QMessageLogger>
#include <QtDebug>
#include <QtGlobal>

#include "log_to_file.hpp"
#include "log_tr.hpp"
#include "main_window.hpp"
#include "monitor_control.hpp"
#include "sensor_driver.hpp"

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

    QApplication application(argc, argv);

    qInfo(ltr("")); // New line for better visibility between runs
    qDebug(ltr("Starting application"));

    SensorDriver sensorDriver;

    auto sensors = sensorDriver.enumerateSensors();

    for(auto& sensor: sensors) {
        const auto sensorRange = sensor.readRange();
        const auto sensorValues = sensor.readValues();

        if(sensor) {
            qInfo(ltr("Sensor range: [%1,%2]").arg(sensorRange.first).arg(sensorRange.second));

            for(size_t i = 0; i < sensorValues.size(); ++i) {
                qInfo(ltr("Sensor value %1: %2").arg(i).arg(sensorValues[i]));
            }
        }
        else {
            qWarning(ltr("Sensor is invalid"));
        }
    }

    auto monitors = enumerateMonitors();

    for(auto& monitor: monitors) {
        const auto brightness = monitor.getBrightness();

        qInfo(ltr("Monitor brightness: %1").arg(brightness));

        if(!monitor.setBrightness(brightness)) {
            qWarning(ltr("Failed to set brightness"));
        }

        if(!monitor) {
            qWarning(ltr("Monitor invalid"));
        }
    }

    MainWindow mainWindow;
    mainWindow.show();
    return application.exec();
}
