from PyQt5.QtWidgets import *
from window import *
from threadMcu import *
import os

class Ui_CusMainWindow(Ui_mainWindow):
    def __init__(self, MainWindow):
        self.setupUi(MainWindow)
        self.displayInit()
        #Add a thread in Window and link the signals to the window display
        self.thread_McuComToolHandle = Thread_McuComToolHandle()
        self.thread_McuComToolHandle.signalComData.connect(self.mcu_DataDisplay)  #Link Data to display
        self.thread_McuComToolHandle.signalComPort.connect(self.mcu_PortDispaly)
        self.thread_McuComToolHandle.signalComLog.connect(self.mcu_LogDispaly)
        self.thread_McuComToolHandle.start()


        self.portConnection = "UnSuccess"

    def displayInit(self):
        #update MCU COM Tool display
        self.comboBox_McuPortSelection.clear()
        #connect PortOpen button to port open function
        self.pushButton_McuUartOpen.clicked.connect(self.mcu_ComPortOpen) #Connect openPort function to connect button
        self.pushButton_McuUartClose.clicked.connect(self.mcu_ComPortClose)  # Connect openPort function to connect button
        self.checkBox_DataSave.toggled.connect(self.mcu_ExcelSave)

    ############################################### Singals #########################################################
    def closeEvent(self):
        self.thread_McuComToolHandle.gui_CoseCom()

    def mcu_LogDispaly(self,str):
        self.lineEdit_McuStatus.setText(str)

    def mcu_ComPortOpen(self):
        item = self.comboBox_McuPortSelection.currentText()        # find current selected COM
        if(item != ""):
            self.thread_McuComToolHandle.gui_OpenCom(item)
        else:
            self.lineEdit_McuStatus.setText("Port open failed!")

    def mcu_ComPortClose(self):
        # find current selected COM
        item = self.comboBox_McuPortSelection.currentText()
        if(item != ""):
            self.thread_McuComToolHandle.gui_CoseCom()
            if self.checkBox_DataSave.isChecked():
                self.checkBox_DataSave.click()
        else:
            self.lineEdit_McuStatus.setText("No port is available!")

    def mcu_PortDispaly(self, str):
        if str != "None":
            self.comboBox_McuPortSelection.addItem(str)
        else:
            self.comboBox_McuPortSelection.clear()

    def mcu_DataDisplay(self,dict):
        if len(dict) != 0:
            self.lineEdit_Counter.setText(str(dict["Counter"]))
            self.lineEdit_Vrms.setText(str(dict["Vrms"]))
            self.lineEdit_Irms.setText(str(dict["Irms"]))
            self.lineEdit_Freq.setText(str(dict["Freq"]))
            self.lineEdit_Pactive.setText(str(dict["Pactive"]))
            self.lineEdit_Preactive.setText(str(dict["Preactive"]))
            self.lineEdit_Papparent.setText(str(dict["Papparent"]))
            self.lineEdit_Pfactor.setText(str(dict["Pfactor"]))

            self.portConnection = "Success"
        else:
            self.lineEdit_Counter.clear()
            self.lineEdit_Vrms.clear()
            self.lineEdit_Irms.clear()
            self.lineEdit_Freq.clear()
            self.lineEdit_Pactive.clear()
            self.lineEdit_Preactive.clear()
            self.lineEdit_Papparent.clear()
            self.lineEdit_Pfactor.clear()

    ############################################### Human Action #######################################################
    def mcu_ExcelSave(self):
        if self.checkBox_DataSave.isChecked():
            self.thread_McuComToolHandle.excelSaveFlg = 1
        else:
            self.thread_McuComToolHandle.excelSaveFlg = 0

class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.function = None
    def closeEvent(self, event):
        print("Close event")
        self.function() #Save the excel
        event.accept()