from PyQt6 import QtWidgets
from PyQt6 import QtCore
from LowLevelWidgetsPackage import defaultConfigForWidgets 
from LowLevelWidgetsPackage.MultipleIOsPackage.MultipleIOsGUI import Ui_Form
from LowLevelWidgetsPackage.SingleIOPackage.SingleIOModule import SingleIOClass
from LowLevelWidgetsPackage.setValuePackage.setValueModule import SetValueClass


class signalClass(QtCore.QObject):
    pin_changed_from_widget = QtCore.pyqtSignal(int,int,bool,bool,str) #cardNumber,PinNumber,output,impendance,sender_name
    pin_changed_to_widget = QtCore.pyqtSignal(int,int,float,bool,bool,str) #cardNumber,PinNumber,voltage,output,impendance,sender_name

class MultipleIOClass(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 SingleIO CLASS","UNNECESSARY")
        self.ui = Ui_Form() #UI form generated by UI designe
        self.ui.setupUi(self)
        self.signals = signalClass()
        self._name = "MultipleIOs"
        self._number_of_custom_columns = 4
        self.ui.pb_make_changes.clicked.connect(self._makeChanges)
        self.ui.pb_update_treshold.clicked.connect(self._updateTreshold)

    def _updateTreshold(self):
        for row in range(self.ui.table.rowCount()):
            for column in range(self.ui.table.columnCount()):
                if column > self._number_of_custom_columns-1:
                    cellWidget:SingleIOClass
                    cellWidget = self.ui.table.cellWidget(row,column)
                    cellWidget.setVoltageTreshold(min=self.ui.spin_voltage_from.value(),max=self.ui.spin_voltage_to.value())

    def _makeChanges(self):
        self.ui.table.clear()
        self.createTable()

    def createTable(self):
        number_of_columns = self.ui.spin_n_of_pins.value()
        number_of_rows = self.ui.spin_n_of_cards.value()
        self.ui.table.setColumnCount(number_of_columns+self._number_of_custom_columns)
        self.ui.table.setRowCount(number_of_rows) 

        header = [f"PIN {column}" for column in range(number_of_columns)]
        header.insert(0,"VISASTRING")
        header.insert(1,"STATUS")
        header.insert(2,"DAC")
        header.insert(3,"ADC")
        self.ui.table.setHorizontalHeaderLabels(header)
        self._fillTable()

    def _fillTable(self):
        for row in range(self.ui.table.rowCount()):
            for column in range(self.ui.table.columnCount()):
                if column == 0:
                    self.ui.table.setItem(row,column,QtWidgets.QTableWidgetItem("visaString"))
                elif column == 1:
                    self.ui.table.setItem(row,column,QtWidgets.QTableWidgetItem("OK"))
                elif column == 2:
                    cellWidget = SetValueClass(log=self.log)
                    cellWidget.setName(f"CARD:{row}, DAC")
                    cellWidget.setUnit("V")
                    cellWidget.signals.value_changed_to_widget.emit(12,"MultipleIOsClass")
                    self.ui.table.setCellWidget(row,column,cellWidget)
                elif column == 3:
                    self.ui.table.setItem(row,column,QtWidgets.QTableWidgetItem("ADC"))
                else:
                    cellWidget = SingleIOClass(log=self.log)
                    cellWidget.setName(f"CARD:{row}, Pin{column-2}")
                    cellWidget.setHideLabels(False)
                    cellWidget.setVoltageTreshold(min=self.ui.spin_voltage_from.value(),max=self.ui.spin_voltage_to.value())
                    cellWidget.signals.voltage_to_widget.emit(13.50056,"MultipleIOsClass")
                    self.ui.table.setCellWidget(row,column,cellWidget)
            self.ui.table.resizeRowToContents(row)
        self.ui.table.resizeColumnsToContents()            


    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 = MultipleIOClass(QtWidgets.QWidget)
    window1.show()
    app1.exec()