from PyQt6 import QtWidgets,QtCore
from LowLevelWidgetsPackage.defaultConfigForWidgets import LOGGER 
from TestsRelatedPackage.TestResultWidgetsPackage.DefaultTestWidgetPromptPackage.DefaultTestWidgetPromptGUI import Ui_Form


class DefaultTestWidgetPromptClass(QtWidgets.QWidget):
    signal_change_test_result = QtCore.pyqtSignal(object,str,str)
    def __init__(self, test, log:LOGGER = 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.test = test
        self.log.log("STARTING TestTemplate CLASS","UNNECESSARY")
        self.ui = Ui_Form() #UI form generated by UI designe
        self.ui.setupUi(self)
        self.setEnabled(False)
        self.loadDefaults()
        self.ui.pb_yes.clicked.connect(self._yesButtonCallback)
        self.ui.pb_no.clicked.connect(self._noButtonCallback)

        if self.test.result == "PASS":
            self.ui.la_promt_label.setText("Test passed")
            self.ui.pb_no.setEnabled(False)
            self.ui.pb_yes.setEnabled(False)
        
        elif self.test.result == "FAIL":
            self.ui.la_promt_label.setText("Test failed")
            self.ui.pb_no.setEnabled(False)
            self.ui.pb_yes.setEnabled(False)

    def loadDefaults(self):
        self.ui.la_test_name.setText(self.test.name)
        self.ui.la_test_result.setText(self.test.result)
        test_time = float(str(self.test.test_time).replace("s",""))
        self.ui.la_test_time.setText(f"{test_time:.3f}s")
        self.ui.la_promt_label.setText("Did test run successfully?")
        self.ui.pb_no.setEnabled(True)
        self.ui.pb_yes.setEnabled(True)

    def setTestResult(self, result):
        result = str(result).replace("True","PASS").replace("False","FAIL")
        self.ui.la_test_result.setText(result)

    def _setTestResultWithSignalEmit(self, result):
        result = str(result).replace("True","PASS").replace("False","FAIL")
        self.ui.la_test_result.setText(result)
        self.signal_change_test_result.emit(self.test,result,"DefaultTestWidgetPromptClass")

    def setTestTime(self, test_time):
        time =  float(str(test_time).replace("s",""))
        self.ui.la_test_time.setText(f"{time:.3f}s")

    def _yesButtonCallback(self):
        self._setTestResultWithSignalEmit("PASS")
        self.ui.la_promt_label.setText("Test passed")
        self.ui.pb_no.setEnabled(False)
        self.ui.pb_yes.setEnabled(False)

    def _noButtonCallback(self):
        self._setTestResultWithSignalEmit("FAIL")
        self.ui.la_promt_label.setText("Test failed")
        self.ui.pb_no.setEnabled(False)
        self.ui.pb_yes.setEnabled(False)