#!/usr/bin/env python
# -*- encoding: utf-8 -*-

from ui.own.frame_theme import MyFrame
from ui.main.top_page import TopPage
from ui.main.center_page import CenterPage
from ui.main.under_page import UnderPage
from utils.qt import QtWidgets, QtCore, Qt, QtGui


class Download(MyFrame):
    def setupUi(self, Download):
        self.reg = QtCore.QRegExp('^[a-zA-Z0-9_-]{16,16}$')
        self.validator = QtGui.QRegExpValidator(self)
        self.validator.setRegExp(self.reg)

        self.download = MyFrame(Download)
        self.download_layout = QtWidgets.QHBoxLayout(self.download)
        self.download_layout.setContentsMargins(0, 0, 0, 0)
        self.download_layout.setSpacing(0)


        self.message = QtWidgets.QWidget()
        self.message_layout = QtWidgets.QVBoxLayout(self.message)
        self.message_layout.setContentsMargins(0, 0, 0, 0)
        self.message_layout.setSpacing(0)

        self.message_widget = QtWidgets.QGroupBox("追溯信息")
        self.message_box_layout = QtWidgets.QVBoxLayout(self.message_widget)
        self.message_table = QtWidgets.QTableWidget(self.message_widget)
        self.message_table.setColumnCount(3)
        self.message_table.setShowGrid(True)
        self.message_table.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
        self.message_table.verticalHeader().setVisible(False)
        self.message_table.verticalHeader().setHighlightSections(True)
        self.message_table.verticalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Stretch)
        self.message_table.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Stretch)
        self.message_table.horizontalHeader().setSectionResizeMode(0, QtWidgets.QHeaderView.ResizeToContents)
        # self.message_table.horizontalHeader().setDefaultAlignment(Qt.AlignmentFlag.AlignCenter)
        self.message_table.setHorizontalHeaderLabels(["序号", "参数名称", "值"])
        self.message_table.setRowCount(6)

        item = QtWidgets.QTableWidgetItem("1")
        item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.message_table.setItem(0, 0, item)
        item = QtWidgets.QTableWidgetItem("Bootloader软件版本")
        item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.message_table.setItem(0, 1, item)
        item = QtWidgets.QTableWidgetItem("")
        item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.message_table.setItem(0, 2, item)

        item = QtWidgets.QTableWidgetItem("2")
        item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.message_table.setItem(1, 0, item)
        item = QtWidgets.QTableWidgetItem("应用程序软件版本号")
        item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.message_table.setItem(1, 1, item)
        item = QtWidgets.QTableWidgetItem("")
        item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.message_table.setItem(1, 2, item)

        item = QtWidgets.QTableWidgetItem("3")
        item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.message_table.setItem(2, 0, item)
        item = QtWidgets.QTableWidgetItem("算法软件版本号")
        item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.message_table.setItem(2, 1, item)
        item = QtWidgets.QTableWidgetItem("")
        item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.message_table.setItem(2, 2, item)

        item = QtWidgets.QTableWidgetItem("4")
        item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.message_table.setItem(3, 0, item)
        item = QtWidgets.QTableWidgetItem("硬件版本号")
        item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.message_table.setItem(3, 1, item)
        item = QtWidgets.QTableWidgetItem("")
        item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.message_table.setItem(3, 2, item)

        item = QtWidgets.QTableWidgetItem("5")
        item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.message_table.setItem(4, 0, item)
        item = QtWidgets.QTableWidgetItem("BMS序列号")
        item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.message_table.setItem(4, 1, item)
        item = QtWidgets.QTableWidgetItem("")
        item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.message_table.setItem(4, 2, item)

        item = QtWidgets.QTableWidgetItem("6")
        item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.message_table.setItem(5, 0, item)
        item = QtWidgets.QTableWidgetItem("电池编码")
        item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.message_table.setItem(5, 1, item)
        item = QtWidgets.QTableWidgetItem("")
        item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.message_table.setItem(5, 2, item)


        self.message_box_layout.addWidget(self.message_table)
        self.btn_read_version = QtWidgets.QPushButton("读取版本信息")
        self.message_box_layout.addWidget(self.btn_read_version)
        self.message_layout.addWidget(self.message_widget)

        # spacerItem3 = QtWidgets.QSpacerItem(40, 0, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
        # self.message_layout.addItem(spacerItem3)

        self.second_widget = MyFrame()
        self.second_widget_layout = QtWidgets.QGridLayout(self.second_widget)
        self.lb_sblx = QtWidgets.QLabel("设备类型")    
        self.second_widget_layout.addWidget(self.lb_sblx, 0, 0, 1, 1)
        self.cb_sblx = QtWidgets.QComboBox()
        self.cb_sblx.addItem("总控(BAU)")
        self.cb_sblx.addItem("主控(BCU)")
        self.cb_sblx.addItem("从控(CMU)")
        self.second_widget_layout.addWidget(self.cb_sblx, 0, 1, 1, 1)
        self.lb_bau = QtWidgets.QLabel("BAU")    
        self.second_widget_layout.addWidget(self.lb_bau, 1, 0, 1, 1)
        self.cb_bau = QtWidgets.QComboBox()
        self.second_widget_layout.addWidget(self.cb_bau, 1, 1, 1, 1)
        self.lb_bcu = QtWidgets.QLabel("BCU")    
        self.second_widget_layout.addWidget(self.lb_bcu, 2, 0, 1, 1)
        self.cb_bcu = QtWidgets.QComboBox()
        self.second_widget_layout.addWidget(self.cb_bcu, 2, 1, 1, 1)
        self.lb_cmu = QtWidgets.QLabel("CMU")    
        self.second_widget_layout.addWidget(self.lb_cmu, 3, 0, 1, 1)
        self.cb_cmu = QtWidgets.QComboBox()
        self.second_widget_layout.addWidget(self.cb_cmu, 3, 1, 1, 1)
        self.message_layout.addWidget(self.second_widget)

        # spacerItem2 = QtWidgets.QSpacerItem(40, 0, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
        # self.message_layout.addItem(spacerItem2)

        self.third_widget = MyFrame()
        self.third_widget_layout = QtWidgets.QGridLayout(self.third_widget)
        self.lb_cxwj = QtWidgets.QLabel("程序文件：")    
        self.third_widget_layout.addWidget(self.lb_cxwj, 0, 0, 1, 1)
        self.edt_filename = QtWidgets.QLineEdit()
        self.edt_filename.setReadOnly(True)
        self.third_widget_layout.addWidget(self.edt_filename, 0, 1, 1, 3)
        self.btn_chose = QtWidgets.QPushButton("选择...")    
        self.third_widget_layout.addWidget(self.btn_chose, 0, 4, 1, 1)
        self.btn_start_up = QtWidgets.QPushButton("开始升级")    
        self.third_widget_layout.addWidget(self.btn_start_up, 1, 0, 1, 1)
        self.btn_stop_up = QtWidgets.QPushButton("中止")    
        self.third_widget_layout.addWidget(self.btn_stop_up, 1, 2, 1, 1)
        self.btn_restart_up = QtWidgets.QPushButton("复位强制升级")    
        self.third_widget_layout.addWidget(self.btn_restart_up, 1, 4, 1, 1)
        self.message_layout.addWidget(self.third_widget)

        self.message_layout.addStretch() 

        self.download_layout.addWidget(self.message, 1)

        self.right_widget = QtWidgets.QWidget()
        self.right_widget_layout = QtWidgets.QVBoxLayout(self.right_widget)

        self.first_frame = MyFrame(self.right_widget)
        self.first_frame_layout = QtWidgets.QGridLayout(self.first_frame)
        self.lb_bms_sn = QtWidgets.QLabel("BMS序列号")
        self.first_frame_layout.addWidget(self.lb_bms_sn, 0, 0, 1, 1)
        self.btn_bms_sn = QtWidgets.QPushButton("录入")
        self.first_frame_layout.addWidget(self.btn_bms_sn, 1, 0, 1, 1)
        self.edt_bms_sn = QtWidgets.QLineEdit(self.first_frame)
        self.edt_bms_sn.setValidator(self.validator)
        self.first_frame_layout.addWidget(self.edt_bms_sn, 0, 1, 2, 4)       
        self.right_widget_layout.addWidget(self.first_frame)

        self.second_frame = MyFrame(self.right_widget)
        self.second_frame_layout = QtWidgets.QGridLayout(self.second_frame)
        self.lb_battery_sn = QtWidgets.QLabel("电池编码")
        self.second_frame_layout.addWidget(self.lb_battery_sn, 0, 0, 1, 1)
        self.btn_battery_sn = QtWidgets.QPushButton("录入")
        self.second_frame_layout.addWidget(self.btn_battery_sn, 1, 0, 1, 1)
        self.edt_battery_sn = QtWidgets.QLineEdit(self.first_frame)
        self.edt_battery_sn.setValidator(self.validator)
        self.second_frame_layout.addWidget(self.edt_battery_sn, 0, 1, 2, 4)  
        self.right_widget_layout.addWidget(self.second_frame)

        spacerItem1 = QtWidgets.QSpacerItem(40, 0, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
        self.right_widget_layout.addItem(spacerItem1)

        self.download_layout.addWidget(self.right_widget, 1)

        QtCore.QMetaObject.connectSlotsByName(Download)
