// host/src/main.cpp
#include <QApplication>
#include <QStyleFactory>
#include <QPermissions>
#include <QDebug>
#include "MainWindow.h"

// Helper to request permissions sequentially
void requestAndroidPermissions() {
#ifdef Q_OS_ANDROID
    // Request Location Permission
    // Required for hardware discovery (BLE/WiFi) and sometimes USB device enumeration on Android.
    // Note: Storage permissions are handled via the Manifest and Scoped Storage; 
    // explicit runtime requests for storage are often not needed for AppData folders.
    
    qApp->requestPermission(QLocationPermission{}, [](const QPermission &permission) {
        if (permission.status() == Qt::PermissionStatus::Granted) {
            qDebug() << "Location Permission Granted";
        } else {
            qWarning() << "Location Permission Denied";
        }
    });
#endif
}

int main(int argc, char *argv[]) {
    // High DPI Scaling
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);

    QApplication app(argc, argv);
    QApplication::setApplicationName("EIS Configurator");
    QApplication::setApplicationVersion("1.0");

    // --- Apply Dark Fusion Theme ---
    app.setStyle(QStyleFactory::create("Fusion"));

    QPalette p;
    p.setColor(QPalette::Window, QColor(53, 53, 53));
    p.setColor(QPalette::WindowText, Qt::white);
    p.setColor(QPalette::Base, QColor(25, 25, 25));
    p.setColor(QPalette::AlternateBase, QColor(53, 53, 53));
    p.setColor(QPalette::ToolTipBase, Qt::white);
    p.setColor(QPalette::ToolTipText, Qt::white);
    p.setColor(QPalette::Text, Qt::white);
    p.setColor(QPalette::Button, QColor(53, 53, 53));
    p.setColor(QPalette::ButtonText, Qt::white);
    p.setColor(QPalette::BrightText, Qt::red);
    p.setColor(QPalette::Link, QColor(42, 130, 218));
    p.setColor(QPalette::Highlight, QColor(42, 130, 218));
    p.setColor(QPalette::HighlightedText, Qt::black);
    app.setPalette(p);

    // --- Global Stylesheet for Modern Look ---
    app.setStyleSheet(
        "QToolTip { color: #ffffff; background-color: #2a82da; border: 1px solid white; }"
        "QGroupBox { border: 1px solid #555; border-radius: 5px; margin-top: 10px; font-weight: bold; }"
        "QGroupBox::title { subcontrol-origin: margin; left: 10px; padding: 0 3px; }"
        "QSpinBox, QDoubleSpinBox, QComboBox, QLineEdit { "
        "   background: #333; color: #FFF; border: 1px solid #555; padding: 4px; border-radius: 4px; "
        "   selection-background-color: #2A82DA; "
        "}"
        "QSpinBox::up-button, QDoubleSpinBox::up-button, QSpinBox::down-button, QDoubleSpinBox::down-button { "
        "   background: #444; width: 16px; border-radius: 2px;"
        "}"
        "QPushButton { "
        "   background-color: #444; border: 1px solid #555; border-radius: 4px; padding: 5px 15px; color: white; font-weight: bold;"
        "}"
        "QPushButton:hover { background-color: #555; }"
        "QPushButton:pressed { background-color: #2A82DA; }"
        "QPushButton:disabled { background-color: #333; color: #777; border: 1px solid #444; }"
        "QTabWidget::pane { border: 1px solid #444; }"
        "QTabBar::tab { background: #333; color: #AAA; padding: 8px 20px; border-top-left-radius: 4px; border-top-right-radius: 4px; }"
        "QTabBar::tab:selected { background: #535353; color: #FFF; border-bottom: 2px solid #2A82DA; }"
        "QScrollBar:vertical { background: #333; width: 12px; }"
        "QScrollBar::handle:vertical { background: #555; min-height: 20px; border-radius: 6px; }"
        "QLabel { color: #EEE; }"
    );

    // Set default font size
    QFont font = app.font();
    font.setPointSize(12);
    app.setFont(font);

    // Request Android Permissions at startup
    requestAndroidPermissions();

    MainWindow w;
    w.show();

    return app.exec();
}