import IOCard_ui
import sys
from PyQt5.QtWidgets import QApplication, QFileDialog, QLabel, QMainWindow, QWidget, QPushButton, QMessageBox
from PyQt5 import QtWidgets, uic, QtCore, QtGui
from PyQt5.QtGui import QImage, QPixmap, QPainter, QPen, QIcon
from PyQt5.QtCore import QRect, Qt, QCoreApplication, QObject
from IOcardQthread import IOcard


ui = IOCard_ui.Ui_MainWindow


class PyQtMain(QtWidgets.QMainWindow, ui):
    # region 初始化
    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)  # 創建主界面對象
        ui.__init__(self)  # 主界面對象初始化
        self.setupUi(self)  # 配置主界面對象
        # QtCore.QMetaObject.connectSlotsByName(self)  # 使用裝飾器取代信號槽
        # self.iniGuiEvent()

        self.IOCardThread = IOcard(SERVER_HOST= '192.168.200.101',SERVER_PORT = 502)


    @QtCore.pyqtSlot()
    def on_btn_IOCardConnect_clicked(self):
        self.IOCardThread.IOCard_connect()
        if self.IOCardThread.connect:
            self.debugBar('IOCard Connection!!!')
            self.IOCardThread.open()
            self.IOCardThread.start()
            self.IOCardThread.rawdataINPUT.connect(self.showInput)
        else:
            self.debugBar('IOCard Disconnection!!!')

    def showInput(self, input0,input1,input2,input3,input4,input5,input6,input7,input8,input9):
        self.state_input0.setVisible(input0)
        self.state_input1.setVisible(input1)
        self.state_input2.setVisible(input2)
        self.state_input3.setVisible(input3)
        self.state_input4.setVisible(input4)
        self.state_input5.setVisible(input5)
        self.state_input6.setVisible(input6)
        self.state_input7.setVisible(input7)
        self.state_input8.setVisible(input8)
        self.state_input9.setVisible(input9)
    @QtCore.pyqtSlot()
    def on_btn_motorMove_clicked(self):
        if self.IOCardThread.connect:
            self.debugBar('IOCard Connection!!!')
            self.IOCardThread.updataDisSpeed(-200,300)
        else:
            self.debugBar('IOCard Disconnection!!!')

    @QtCore.pyqtSlot()
    def on_btn_motorGoHome_clicked(self):
        if self.IOCardThread.connect:
            self.debugBar('IOCard Connection!!!')
            self.IOCardThread.goHome()
        else:
            self.debugBar('IOCard Disconnection!!!')

    @QtCore.pyqtSlot()
    def on_btn_openAll_clicked(self):
        if self.IOCardThread.connect:
            self.debugBar('IOCard Connection!!!')
            self.IOCardThread.openAll()
        else:
            self.debugBar('IOCard Disconnection!!!')

    @QtCore.pyqtSlot()
    def on_btn_closeAll_clicked(self):
        if self.IOCardThread.connect:
            self.debugBar('IOCard Connection!!!')
            self.IOCardThread.closeAll()
        else:
            self.debugBar('IOCard Disconnection!!!')





    # region 顯示MessageBox 並獲取選取按鍵狀態
    def showdialog(seelf):
        msg = QMessageBox()
        msg.setIcon(QMessageBox.Information)
        msg.setText("This is a message box")
        msg.setInformativeText("This is additional information")
        msg.setWindowTitle("MessageBox demo")
        msg.setWindowIcon(QIcon(r"yuntech.png"))
        msg.setDetailedText("The details are as follows:")
        msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
        msg.buttonClicked.connect(seelf.msgbtn)
        retval = msg.exec_()  # 執行

    def msgbtn(self, i):
        self.debugBar(f"MessageBox Button pressed is:{i.text()}")

    def debugBar(self, msg):
        self.statusBar.showMessage(str(msg), 5000)
    # endregion



if __name__ == '__main__':
    app = QApplication(sys.argv)
    MainWindow = PyQtMain()
    MainWindow.show()
    sys.exit(app.exec_())
