#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@文件    :emv_home.py
@时间    :2022/11/17 13:33:56
@作者    :None
@版本    :1.0
@说明    :主页面逻辑
'''


from ui.home import UiEmvHomePage
from utils.qt import QMainWindow, Qt, QStyle, QMessageBox, QGuiApplication, Signal


class Win_Emv_Home(UiEmvHomePage, QMainWindow):

    interface_signal = Signal(int)
    connect_signal = Signal()
    disconnect_signal = Signal()
    emv_on_signal = Signal()
    emv_off_signal = Signal()

    def __init__(self, parent=None):
        super(Win_Emv_Home, self).__init__(parent)
        self.setupUi(self)

        self.cb_interface.currentIndexChanged.connect(self._interface_chose)
        self.emv_switch.checkedChanged.connect(self._get_state)
        self.btn_emv_on.clicked.connect(self._emv_on)
        self.btn_emv_off.clicked.connect(self._emv_off)

    # 居中显示
    def center(self):
        # 新方法
        self.setGeometry(
            QStyle.alignedRect(
                Qt.LeftToRight,
                Qt.AlignCenter,
                self.size(),
                QGuiApplication.primaryScreen().availableGeometry(),
            ),
        )

    def _interface_chose(self):
        self.interface_signal.emit(self.cb_interface.currentIndex())

    # EMV 连接
    def _get_state(self, checked):
        if checked:
            self.connect_signal.emit()
        else:
            self.disconnect_signal.emit()

    def _emv_on(self):
        self.emv_on_signal.emit()

    def _emv_off(self):
        self.emv_off_signal.emit()

    def can_connect_error(self):
        QMessageBox.critical(self, "失败！", "CAN 连接失败！")
        self.emv_switch.state = False
        return

    def no_can_device(self):
        QMessageBox.critical(self, "失败！", "无CAN 设备！")
        self.emv_switch.state = False
        return

    def no_com_device(self):
        QMessageBox.critical(self, "失败！", "无法连接此串口设备！")
        self.emv_switch.state = False
        return
