#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""

@author: Marcel Weschke
@email: marcel.weschke@directbox.de
"""
# %% Load libraries
import os
import sys
import threading
import time
from PyQt6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget, QSplashScreen, QLabel
from PyQt6.QtWebEngineWidgets import QWebEngineView
from PyQt6.QtCore import QUrl, QTimer, Qt
from PyQt6.QtGui import QPixmap

# Performance-Optimierungen für Qt WebEngine
os.environ.update({
    # Hardware-Beschleunigung forcieren
    "QTWEBENGINE_CHROMIUM_FLAGS": (
        "--ignore-gpu-blocklist "
        "--enable-gpu-rasterization "
        "--enable-zero-copy "
        "--disable-logging "
        "--no-sandbox "
        "--disable-dev-shm-usage "
        "--disable-extensions "
        "--disable-plugins "
        "--disable-background-timer-throttling "
        "--disable-backgrounding-occluded-windows "
        "--disable-renderer-backgrounding "
        "--disable-features=TranslateUI "
        "--aggressive-cache-discard "
        "--memory-pressure-off"
    ),
    # Logging reduzieren
    "QT_LOGGING_RULES": "qt.webenginecontext.debug=false",
    "QTWEBENGINE_DISABLE_SANDBOX": "1",
    # Cache-Optimierungen
    "QTWEBENGINE_DISABLE_GPU_THREAD": "0"
})

# Importiere deine Dash-App
from jogging_dashboard_browser_app import app

class DashThread(threading.Thread):
    """Optimierter Dash-Thread mit besserer Kontrolle"""
    def __init__(self):
        super().__init__(daemon=True)
        self.dash_ready = False

    def run(self):
        try:
            # Dash mit Performance-Optimierungen starten
            app.run(
                debug=False,
                port=8051,
                use_reloader=False,
                host='127.0.0.1',
                threaded=True,  # Threading für bessere Performance
                processes=1     # Single process für Desktop
            )
        except Exception as e:
            print(f"Dash-Server Fehler: {e}")

    def wait_for_dash(self, timeout=10):
        """Warte bis Dash-Server bereit ist"""
        import requests
        start_time = time.time()

        while time.time() - start_time < timeout:
            try:
                response = requests.get('http://127.0.0.1:8051/', timeout=2)
                if response.status_code == 200:
                    self.dash_ready = True
                    return True
            except:
                pass
            time.sleep(0.5)

        return False

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Jogging Dashboard - Desktop")
        self.setGeometry(100, 100, 1400, 900)  # Größere Standardgröße

        # Performance: Lade Seite erst wenn Dash bereit ist
        self.browser = None
        self.setup_ui()

    def setup_ui(self):
        """UI-Setup ohne sofortiges Laden der Seite"""
        central_widget = QWidget()
        layout = QVBoxLayout()

        # Loading-Label während Dash startet
        self.loading_label = QLabel("🚀 Dashboard wird geladen...")
        self.loading_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.loading_label.setStyleSheet("""
            QLabel {
                font-size: 18px;
                color: #333;
                background: #f0f0f0;
                padding: 20px;
                border-radius: 10px;
            }
        """)

        layout.addWidget(self.loading_label)
        central_widget.setLayout(layout)
        self.setCentralWidget(central_widget)

    def load_dashboard(self):
        """Lade Dashboard nachdem Dash bereit ist"""
        # Browser-Widget erstellen
        self.browser = QWebEngineView()

        # Performance-Einstellungen für WebEngineView
        settings = self.browser.settings()
        settings.setAttribute(settings.WebAttribute.PluginsEnabled, False)
        settings.setAttribute(settings.WebAttribute.JavascriptEnabled, True)
        settings.setAttribute(settings.WebAttribute.LocalStorageEnabled, True)

        # Seite laden
        self.browser.setUrl(QUrl("http://127.0.0.1:8051"))

        # Layout aktualisieren
        central_widget = QWidget()
        layout = QVBoxLayout()
        layout.setContentsMargins(0, 0, 0, 0)  # Kein Rand für maximale Größe
        layout.addWidget(self.browser)
        central_widget.setLayout(layout)
        self.setCentralWidget(central_widget)

        print("✅ Dashboard geladen!")

class SplashScreen(QSplashScreen):
    """Splash Screen für bessere UX während des Startens"""
    def __init__(self):
        # Einfacher Text-Splash (du kannst ein Logo hinzufügen)
        pixmap = QPixmap(400, 200)
        pixmap.fill(Qt.GlobalColor.white)
        super().__init__(pixmap)

        # Text hinzufügen
        self.setStyleSheet("""
            QSplashScreen {
                background-color: #2c3e50;
                color: white;
                font-size: 16px;
            }
        """)

    def showMessage(self, message):
        super().showMessage(message, Qt.AlignmentFlag.AlignCenter, Qt.GlobalColor.white)

def main():
    print("🚀 Starte Jogging Dashboard...")

    # Qt Application mit Performance-Flags
    app_qt = QApplication(sys.argv)
    #app_qt.setAttribute(Qt.ApplicationAttribute.AA_EnableHighDpiScaling, True)  # Not working yet
    #app_qt.setAttribute(Qt.ApplicationAttribute.AA_UseHighDpiPixmaps, True)     # Not working yet

    # Splash Screen anzeigen
    splash = SplashScreen()
    splash.show()
    splash.showMessage("Initialisiere Dashboard...")
    app_qt.processEvents()

    # Dash-Server starten
    dash_thread = DashThread()
    dash_thread.start()

    splash.showMessage("Starte Web-Server...")
    app_qt.processEvents()

    # Auf Dash warten
    if dash_thread.wait_for_dash(timeout=15):
        splash.showMessage("Dashboard bereit!")
        app_qt.processEvents()

        # Hauptfenster erstellen und laden
        window = MainWindow()

        # Kurz warten für bessere UX
        time.sleep(0.5)

        # Dashboard laden
        window.load_dashboard()

        # Splash schließen und Hauptfenster anzeigen
        splash.close()
        window.show()

        print("✅ Dashboard erfolgreich gestartet!")

    else:
        splash.showMessage("❌ Fehler beim Starten!")
        app_qt.processEvents()
        time.sleep(2)
        splash.close()
        print("❌ Dashboard konnte nicht gestartet werden!")
        sys.exit(1)

    # Event-Loop starten
    sys.exit(app_qt.exec())

if __name__ == "__main__":
    main()
