"""QTFM Simple Gradiometer.

First milestone: open two QTFM sensors over independent serial ports.
Data parsing and plotting will be added in later milestones.
"""

from __future__ import annotations

import sys

from PyQt5.QtCore import QTimer, Qt
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication

from main_window import MainWindow
from theme import APP_ICON_PATH, APP_STYLESHEET
from version import APP_VERSION


def create_app() -> QApplication:
    """Create and configure the Qt application."""
    QApplication.setAttribute(Qt.AA_EnableHighDpiScaling, True)
    QApplication.setAttribute(Qt.AA_UseHighDpiPixmaps, True)

    app = QApplication.instance() or QApplication(sys.argv)
    app.setApplicationName("QTFM Simple Gradiometer")
    app.setApplicationVersion(APP_VERSION)
    app.setOrganizationName("QuSpin")
    app.setStyle("Fusion")
    app.setStyleSheet(APP_STYLESHEET)
    # Every window title bar and the taskbar share the QTFM icon; without
    # this only the exe file itself carries it and windows show a default.
    if APP_ICON_PATH.exists():
        app.setWindowIcon(QIcon(str(APP_ICON_PATH)))
    return app


def main() -> int:
    """Launch the desktop application."""
    app = create_app()
    window = MainWindow()
    window.show()
    if "--smoke-test" in sys.argv:
        QTimer.singleShot(750, app.quit)
    return app.exec_()


if __name__ == "__main__":
    raise SystemExit(main())
