import sys

from Map import Map
from CarController import CarController

from PySide6.QtWidgets import QFileDialog, QToolBox
from PySide6.QtCore import QElapsedTimer, QTimer, QElapsedTimer, QObject, Signal, Slot, QThread, QMutex

class mainLoop(QObject):
    updateFPS = Signal(int)
    mutex = QMutex()
    
    def __init__(self, parent):
        super().__init__()
        self.parent = parent
        self.painter = parent.ui.mainSurf

        self.map = Map()

        self.controller = CarController(self.map)
        self.controller.addParent(parent)

        self.painter.addMap(self.map)
        self.painter.addCarController(self.controller)
        
        self.timer = None

        self.fpsTimer = QElapsedTimer()
        self.updateFPS.connect(self.parent.updatePhysicsFps)

        self.fpsAverage = 0
    
    def threadSafe(func):
        def inner(*args, **kwargs):
            args[0].mutex.lock()
            func(*args, **kwargs)
            args[0].mutex.unlock()
        return inner

    def start(self):
        self.fpsTimer.start()
        
        self.timer = QTimer()
        self.timer.timeout.connect(self.updateFps)
        self.timer.timeout.connect(self.update)
        self.timer.setInterval(1000/60)
        #self.stopSignal.connect(self.timer.stop)

    def startTimer(self):
        self.timer.start()

    def stopTimer(self):
        self.timer.stop()

    def setTimerInterval(self,t):
        self.timer.setInterval(t)

    def quit(self):
        super().quit()
        self.timer.stop()

    @threadSafe
    def update(self):
        try:
            self.controller.update()
            if self.controller.t > 3600:
                self.stopTimer()
        except:
            (type, value, traceback) = sys.exc_info()
            print(type, value, traceback.print_tb())
    
    @threadSafe
    def openNetwork(self, filename):
        if filename == '':
            return
        self.map.fromPath(filename)
        self.painter.generateTransform()

        self.controller.prepareRoute()

    @threadSafe
    def openVehicles(self, filename):
        if filename == '':
            return
        self.controller.fromPath(filename)
        if self.map.isLoaded():
            self.controller.prepareRoute()
    
    @threadSafe
    def quickLoad(self):
        self.map.fromPath("comp_inter.net.xml")
        self.painter.generateTransform()
        self.controller.fromPath("comp_inter.rou.xml")
        self.controller.prepareRoute()

    def updateFps(self):
        self.fpsAverage += 1
        if(self.fpsAverage == 32):
            newFps = self.fpsTimer.restart()
            self.updateFPS.emit(newFps/32)
            self.fpsAverage = 0

