from LowLevelWidgetsPackage.defaultConfigForWidgets import LOGGER
from PyQt6 import QtCore

class SignalClass(QtCore.QObject):
    property_changes = QtCore.pyqtSignal(str,str, str) #property_name, value, decvice_name

class DeviceStatusClass():
    def __init__(self, log:LOGGER = LOGGER("ALL"), *args, **kwargs):#args = multiple input variables, **kvargs multiple keyword variables (dictionaries, touples etc.)
        self.STATE = _HardWareStatesClass()
        self._Class_is_initialized = False
        self.log = log
        self._connection_status = self.STATE.UNKNOWN
        self.test_status = self.STATE.UNKNOWN
        self.last_fault_description = self.STATE.UNKNOWN
        self.last_fault_time = self.STATE.UNKNOWN
        self.device_name = self.STATE.UNKNOWN
        self.last_startUp_time = self.STATE.UNKNOWN
        self.last_self_check_time = self.STATE.UNKNOWN
        self.signals = SignalClass()

        self._Class_is_initialized = True
        

    def printStatus(self)-> str: 
        string_to_print = ""
        string_to_print += f"Device name: {self.device_name}\n"
        string_to_print += f"Connection status: {self.connection_status}\n"
        string_to_print += f"Test status: {self.test_status}\n"
        string_to_print += f"Last fault description: {self.last_fault_description}\n"
        string_to_print += f"Last fault time: {self.last_fault_time}\n"
        string_to_print += f"Last start up time: {self.last_startUp_time}\n"
        string_to_print += f"Last self check time: {self.last_self_check_time}\n"
        return string_to_print


    def funtionToBeExecutedWhenPropertyChanges(self,sender, status):
        if self._Class_is_initialized:
            if sender == self.test_status:
                property_name = "test_status"

            elif sender == self.last_fault_description:
                property_name = "last_fault_description"

            elif sender == self.last_fault_time:
                property_name = "last_fault_time"

            elif sender == self.device_name:
                property_name = "device_name"
            
            elif sender == self.last_startUp_time:
                property_name = "last_startUp_time"
            
            elif sender == self.last_self_check_time:
                property_name = "last_self_check_time"
            
            elif sender == self._connection_status:
                property_name = "_connection_status"
            
            else:
                property_name = "UNKNOWN"

            self.log.log(f"DEVICE STATUS CLASS, emiting signal: property_changes: poperty_name = {property_name}, toUpdate = {sender}", "SIGNAL")
            self.signals.property_changes.emit(property_name, str(status), self.device_name)

#FOLOWING LINES JUST ADDS GETTER AND SETTER FOR ALL PROPERTIES -> detect changes of these variables...
    @property
    def connection_status(self):
        return self._connection_status 

    @connection_status.setter
    def connection_status(self,status):
        self._connection_status = status
        self.funtionToBeExecutedWhenPropertyChanges(self._connection_status, status)

    @property
    def test_status(self):
        return self._test_status 

    @test_status.setter
    def test_status(self,status):
        self._test_status = status
        self.funtionToBeExecutedWhenPropertyChanges(self._test_status, status)
        

    @property
    def last_fault_description(self):
        return self._last_fault_description 

    @last_fault_description.setter
    def last_fault_description(self,status):
        self._last_fault_description = status
        self.funtionToBeExecutedWhenPropertyChanges(self._last_fault_description, status)

    @property
    def last_fault_time(self):
        return self._last_fault_time 

    @last_fault_time.setter
    def last_fault_time(self,status):
        self._last_fault_time = status
        self.funtionToBeExecutedWhenPropertyChanges(self._last_fault_time, status)

    @property
    def device_name(self):
        return self._device_name 

    @device_name.setter
    def device_name(self,status):
        self._device_name = status
        self.funtionToBeExecutedWhenPropertyChanges(self._device_name, status)

    @property
    def last_startUp_time(self):
        return self._last_startUp_time 

    @last_startUp_time.setter
    def last_startUp_time(self,status):
        self._last_startUp_time = status
        self.funtionToBeExecutedWhenPropertyChanges(self._last_startUp_time, status)

    @property
    def last_self_check_time(self):
        return self._last_self_check_time 

    @last_self_check_time.setter
    def last_self_check_time(self,status):
        self._last_self_check_time = status
        self.funtionToBeExecutedWhenPropertyChanges(self._last_self_check_time, status)
    


class _HardWareStatesClass():
    CONNECTED = "CONNECTED"
    DISCONNECTED = "DISCONNECTED"
    BUSY = "BUSY"
    IDLE = "IDLE"
    ERROR = "ERROR"
    UNKNOWN = "UNKNOWN"
    READY = "READY"

        