import os
import sys
from PyQt6 import QtWidgets
from PyQt6 import QtCore
from LowLevelWidgetsPackage import defaultConfigForWidgets 
from LowLevelWidgetsPackage.Relay16Package.Relay16GUI import Ui_ARRIGO_RELAY_16 as GUI_Form_RELAY_16


class signalClass(QtCore.QObject):
    checkbox_updated = QtCore.pyqtSignal(list,str)

class Relay16Class(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 RELAY16 CLASS","UNNECESSARY")
        self.ui = GUI_Form_RELAY_16() #UI form generated by UI designe
        self.ui.setupUi(self)
        self.signals = signalClass()
        self._name = "Relay16"
        self._list_of_checkboxes  = [ x for x in self.children() if isinstance(x, QtWidgets.QCheckBox)]
        self.list_of_ordered_checkboxes = self._getListOfOrderedCheckboxes()
        self.ui.pb_clear_all.clicked.connect(self.clearAllButtonCallback)
        for checkbox in self._list_of_checkboxes:
            checkbox.clicked.connect(self.checkboxCallback)


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

    def checkboxCallback(self):
        self.list_of_ordered_checkboxes[int(self.sender().text())-1] = self.sender().isChecked()
        self.signals.checkbox_updated.emit(self.list_of_ordered_checkboxes,self._name) #to distinguish between different widgets change self.name

    def _getListOfOrderedCheckboxes(self):
        list_of_ordered_checkboxes = [0]*len(self._list_of_checkboxes)
        for checkbox in self._list_of_checkboxes:
            if checkbox.isChecked(): 
                list_of_ordered_checkboxes[int(checkbox.objectName()) - 1] = 1
        return list_of_ordered_checkboxes

        

    def clearAllButtonCallback(self):
        for checkbox in self._list_of_checkboxes:
            checkbox.setChecked(False)
        self.list_of_ordered_checkboxes = [0]*16
        self.signals.checkbox_updated.emit(self.list_of_ordered_checkboxes,self._name) #to distinguish between different widgets change self.name


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