"""
main.py — Linedance afspiller.

Start:
    python main.py
"""

import sys
import os

if getattr(sys, 'frozen', False):
    # Kørende som PyInstaller .exe — sæt VLC sti til app-mappen
    _app_dir = os.path.dirname(sys.executable)
    os.environ.setdefault("VLC_PLUGIN_PATH", os.path.join(_app_dir, "plugins"))

APP_VERSION = "0.9.5"

sys.path.insert(0, os.path.dirname(__file__))

from app_logger import setup_logging
setup_logging()

from PyQt6.QtWidgets import QApplication
from ui.main_window import MainWindow
from ui.themes import apply_theme


def main():
    app = QApplication(sys.argv)
    app.setApplicationName("LineDance Player")
    app.setOrganizationName("LineDance")

    # Undertrryk ufarlige Qt Windows-advarsler i konsollen
    import sys as _sys
    from PyQt6.QtCore import qInstallMessageHandler, QtMsgType
    def _qt_msg_handler(msg_type, context, message):
        # Undertrykk kendte harmløse advarsler
        if any(x in message for x in [
            "registerTimer", "Unhandled scheme",
            "Point size <= 0", "setPointSize",
            "QFont::", "Timers cannot be stopped",
            "Timers cannot be started",
            "Cannot create children for a parent",
            "event dispatcher has already been destroyed",
        ]):
            return
        if _sys.platform == "win32":
            print(message)
    qInstallMessageHandler(_qt_msg_handler)

    # Indlæs sprog fra indstillinger
    from ui.settings_dialog import load_settings
    from translations import load_language
    settings = load_settings()
    load_language(settings.get("language", "da"))

    apply_theme(app, dark=settings.get("dark_theme", True))

    window = MainWindow()
    window.show()

    sys.exit(app.exec())


if __name__ == "__main__":
    main()
