from enum import Enum, unique

from PyQt5.QtCore import QSize, Qt
from PyQt5.QtGui import QFont
from PyQt5.QtWidgets import QDialog, QMessageBox, QListWidgetItem, QLineEdit, QLabel, QSizePolicy

# 导入取车确认界面
import ui.pickupUi as pickupUi

# 取车密码
pickup_password = "123456"


# 窗口返回值
@unique
class ResultReturn(Enum):
    DEFAULT = 0  # 默认
    OK = 1  # 成功
    CANCEL = 2  # 取消
    FAIL = 3  # 失败
    ERROR = 4  # 出错


class PickupWindow(QDialog, pickupUi.Ui_Dialog):

    def __init__(self,tipstr):  # 默认使用空列表传值
        super(PickupWindow, self).__init__()
        self.setupUi(self)
        self.setWindowTitle("密码确认")
        if tipstr is not None:
            self.input_passwd_label.setText(tipstr)
        # 设置初始窗口大小
        # self.resize(QSize(500, 300))
        # 取消帮助按钮显示
        self.setWindowFlags(Qt.WindowCloseButtonHint)
        # 隐藏密码
        self.password_lineEdit.setEchoMode(QLineEdit.Password)
        # 设置属性为模态框
        self.setWindowModality(Qt.ApplicationModal)
        # self.verticalLayout_2.setContentsMargins(0, 0, 0, 0)

        # 居中打印车位信息
        # self.pickup_verticalLayout.setAlignment(Qt.AlignCenter)
        # 居中显示密码框
        self.horizontalLayout_2.setAlignment(Qt.AlignCenter)

        # 绑定界面按钮点击动作
        self.cancel_pushButton.clicked.connect(self.cancel_event)
        self.confirm_pushButton.clicked.connect(self.confirm_event)

        # 显示信息
        # self.info = info
        # 窗口返回值
        self.res = ResultReturn.DEFAULT

        # # 车位信息字典
        # self.space_info_dict = info

    # # 获取主窗体传来的车位信息
    # def get_data_dict(self, space_info_dict):
    #     self.space_info_dict = space_info_dict

    # 取消取车事件
    def cancel_event(self):
        self.hide()

    # 确认取车事件
    def confirm_event(self):
        # 重置返回值
        self.setResult(ResultReturn.DEFAULT)
        # 获取密码输入
        password = self.password_lineEdit.text()
        if password != pickup_password:
            self.password_lineEdit.clear()
            QMessageBox.warning(self, "提示", "密码错误,请重新输入", QMessageBox.Yes)
        else:
            self.setResult(ResultReturn.OK)
            self.hide()

    # 窗口显示处理
    def showEvent(self, event):
        # 重置返回值
        self.setResult(ResultReturn.DEFAULT)
        # 聚焦密码框
        self.password_lineEdit.setFocus()
        self.password_lineEdit.clear()
        # 自适应显示车位信息
        # self.draw_info()

    # 窗口隐藏处理
    def hideEvent(self, event):
        self.clearFocus()
        self.password_lineEdit.clear()

    # 窗口关闭处理
    def closeEvent(self, event):
        results = QMessageBox.question(self, '退出', '你确定要退出吗?', QMessageBox.Yes | QMessageBox.No,
                                       QMessageBox.No)  # "退出"代表的是弹出框的标题,"你确认退出.."表示弹出框的内容
        if results == QMessageBox.Yes:
            event.accept()  # 接受关闭事件
        else:
            event.ignore()  # 忽略关闭事件

    # 设置窗口返回结果
    def setResult(self, res):
        self.res = res

    # 获取窗口返回结果
    def getResult(self):
        return self.res

    # 设置显示信息
    def set_info(self, info):
        # 需显示信息
        self.info = info

    # 获取显示信息
    def get_info(self):
        return self.info

    # 获取中文释义
    def get_name(self, key):
        if key == 'id':
            return '下标'
        elif key == 'table_id':
            return '表编号'
        elif key == 'floor':
            return '楼层'
        elif key == 'height':
            return '限高'
        elif key == 'car_number':
            return '车牌号'
        elif key == 'statu':
            return '车位状态'
        elif key == 'plate_color':
            return '号牌颜色'
        elif key == 'plate_type':
            return '号牌类型'
        elif key == 'plate_confidence':
            return '号牌可信度'
        elif key == 'recognition_time':
            return '识别时间'
        elif key == 'primary_key':
            return '唯一码'
        else:
            return '未知'

    # 反向获取数据库字段名
    def get_tableName(self, key):
        if key == '下标':
            return 'id'
        elif key == '表编号':
            return 'table_id'
        elif key == '楼层':
            return 'floor'
        elif key == '限高':
            return 'height'
        elif key == '车牌号':
            return 'car_number'
        elif key == '车位状态':
            return 'statu'
        elif key == '号牌颜色':
            return 'plate_color'
        elif key == '号牌类型':
            return 'plate_type'
        elif key == '号牌可信度':
            return 'plate_confidence'
        elif key == '识别时间':
            return 'recognition_time'
        elif key == '唯一码':
            return 'primary_key'
        else:
            return '未知'

    # 动态绘制信息
    def draw_info(self):
        font = QFont("楷体")
        n_len_scale = int(80 / len(self.info))
        n_size = 20 if n_len_scale < 20 else n_len_scale
        font.setPointSize(n_size)
        for i in self.info:
            qLabel = QLabel()
            qLabel.setFont(font)
            qLabel.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
            # qLabel.setAlignment(Qt.AlignLeft)  # 左对齐
            qLabel.setAlignment(Qt.AlignCenter)  # 居中对齐
            qLabel.setText(i)
            self.pickup_verticalLayout.addWidget(qLabel)

        # self.pickup_listWidget.clear()
        # for i in self.info:
        #     qListWidgetItem = QListWidgetItem()
        #     qListWidgetItem.setSizeHint(QSize(50, 65))
        #     qListWidgetItem.setText(i)
        #     self.pickup_listWidget.addItem(qListWidgetItem)
