import os
import sys
from pathlib import Path

from PySide6.QtWidgets import QApplication
from PySide6.QtQml import QQmlApplicationEngine
from PySide6.QtGui import QIcon

from autogen.settings import url, import_paths
from Connection import ConnectionHandler
from Graph import GraphController
from SaveCSV import SaveCSV

if __name__ == '__main__':
    app = QApplication(sys.argv)
    
    # Set application icon
    app_dir = Path(__file__).parent.parent
    icon_path = os.fspath(app_dir / "IHM_PERContent" / "Ressources" / "logo.png")
    
    icon = QIcon(icon_path)
    app.setWindowIcon(icon)
    
    engine = QQmlApplicationEngine()
    
    # Create and expose connection handler to QML
    connection_handler = ConnectionHandler()
    engine.rootContext().setContextProperty("connectionHandler", connection_handler)
    
    # Create and expose graph controller to QML
    graph_controller = GraphController()
    engine.rootContext().setContextProperty("graphController", graph_controller)

    # Create and expose SaveCSV controller to QML
    savecsv_controller = SaveCSV()
    engine.rootContext().setContextProperty("SaveCSV", savecsv_controller)

    # Graph updates
    connection_handler.vitesseReelUpdated.connect(graph_controller.setVitesse)
    connection_handler.courantReelUpdated.connect(graph_controller.setCourant)
    connection_handler.WpReelUpdated.connect(graph_controller.setWp)

    #CSV updates
    connection_handler.vitesseReelUpdated.connect(savecsv_controller.setVitesse)
    connection_handler.courantReelUpdated.connect(savecsv_controller.setCourant)
    connection_handler.CpReelUpdated.connect(savecsv_controller.setCp)
    connection_handler.CmaxReelUpdated.connect(savecsv_controller.setCmax)
    connection_handler.XmaxReelUpdated.connect(savecsv_controller.setXmax)
    connection_handler.penteReelUpdated.connect(savecsv_controller.setp)
    connection_handler.WpReelUpdated.connect(savecsv_controller.setWp)
    connection_handler.T1ReelUpdated.connect(savecsv_controller.setT1)
    connection_handler.T2ReelUpdated.connect(savecsv_controller.setT2)
    
    
    
    engine.addImportPath(os.fspath(app_dir))
    for path in import_paths:
        engine.addImportPath(os.fspath(app_dir / path))
    
    engine.load(os.fspath(app_dir / url))
    if not engine.rootObjects():
        sys.exit(-1)
    
    sys.exit(app.exec())