import os
import sys
from PyQt6 import QtWidgets
from PyQt6 import QtCore
from LowLevelWidgetsPackage import defaultConfigForWidgets 
from LowLevelWidgetsPackage.Ext import Ui_SET_VALUE_WIDGET


class signalClass(QtCore.QObject):
    value_changed_from_widget = QtCore.pyqtSignal(float,str) #value , name of widget
    value_changed_to_widget = QtCore.pyqtSignal(float,str) #value , name of widget

class SetValueClass(QtWidgets.QWidget):
    def __init__(self,log:defaultConfigForWidgets.LOGGER = defaultConfigForWidgets.LOGGER("ALL") ,*args, **kwargs):#args = multiple input variables, **kvargs multiple keyword variables (dictionaries, touples etc.)
    ################ INIT Function ##################
        super().__init__(*args, **kwargs)#super inherits all methods and properties from its parent  (QWIDGET classes)
        #Load UI forms creaated in QTdDesigner
        self.log = log
        self.log.log("STARTING setValue CLASS","UNNECESSARY")
        self.ui = Ui_SET_VALUE_WIDGET() #UI form generated by UI designe
        self.ui.setupUi(self)
        self.signals = signalClass()
        self._unit = ""
        self._name = "setValue"
        self.ui.pb_set.clicked.connect(self._setValueCallback)
        self.signals.value_changed_to_widget.connect(self._valueChangedSignalCallback)

    @QtCore.pyqtSlot(float,str)
    def _valueChangedSignalCallback(self,value:float,sender_name:str):
        self.log.log(f"SetValueClass: set Value: {value} from {sender_name}","SIGNAL")
        self.ui.la_current_value.setText(str(value)+self._unit)

    def _setValueCallback(self):
        self.signals.value_changed_from_widget.emit(self.ui.spin_value.value(),self._name)
        self.ui.la_current_value.setText(str(self.ui.spin_value.value())+self._unit)

    def setUnit(self,unit:str):
        self._unit = unit

    def setName(self,name:str):
        #to distinguish between different widgets change self.name -> this name will be emit in signals
        self._name = name
        

    
    
if __name__ == "__main__":
    app1 = QtWidgets.QApplication([])
    window1 = SetValueClass(QtWidgets.QWidget)
    window1.show()
    app1.exec()