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

from ui.own.frame_theme import MyFrame
from ui.own.palette_theme import set_edt_bg
from utils.qt import QtWidgets, QtCore, Qt, QColor


class Hard(MyFrame):
    def setupUi(self, Hard):

        self.hard = MyFrame(Hard)
        self.hard_layout = QtWidgets.QHBoxLayout(self.hard)
        # self.hard_layout.setContentsMargins(5, 5, 5, 5)
        self.hard_layout.setSpacing(0)

        self.hard_left_groupbox = QtWidgets.QGroupBox(self.hard)
        self.hard_left_groupbox.setTitle("单体电压校准")
        self.hard_left_groupbox_layout = QtWidgets.QVBoxLayout(self.hard_left_groupbox)

        self.hard_left_top_widget = QtWidgets.QWidget()
        self.hard_left_top_layout = QtWidgets.QHBoxLayout(self.hard_left_top_widget)

        self.lb_cj = QtWidgets.QLabel("从机编号选择:")
        self.hard_left_top_layout.addWidget(self.lb_cj)
        self.cb_bmu_index = QtWidgets.QComboBox()
        for i in range(15):
            self.cb_bmu_index.addItem(str(i + 1))
        self.hard_left_top_layout.addWidget(self.cb_bmu_index)


        self.hard_left_top_layout.addStretch() 

        self.hard_left_groupbox_layout.addWidget(self.hard_left_top_widget)

        self.hard_table = QtWidgets.QTableWidget(self.hard_left_groupbox)
        self.hard_table.setColumnCount(6)
        self.hard_table.verticalHeader().setVisible(False)
        self.hard_table.verticalHeader().setHighlightSections(True)
        self.hard_table.verticalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Stretch)
        self.hard_table.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Stretch)
        self.hard_table.horizontalHeader().setSectionResizeMode(0, QtWidgets.QHeaderView.ResizeToContents)
        self.hard_table.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
        # 设置只能选择行
        self.hard_table.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
        self.hard_table.setHorizontalHeaderLabels(["序号", "参数名称", "电池真实值", "BMS上传值", "误差", "单位"])
        self.hard_table.setRowCount(72)

        for i in range(72):
            item = QtWidgets.QTableWidgetItem(str(i + 1))
            item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
            self.hard_table.setItem(i, 0, item)
            item = QtWidgets.QTableWidgetItem("mV")
            item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
            self.hard_table.setItem(i, 5, item)
            # if i == 0:
            #     item = QtWidgets.QTableWidgetItem("所有单体电压")
            #     item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
            #     self.hard_table.setItem(0, 1, item)               
            # else:
            item = QtWidgets.QTableWidgetItem("C" + str(i + 1) + "单体电压")
            item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
            self.hard_table.setItem(i, 1, item)
            item = QtWidgets.QLineEdit()
            item.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
            self.hard_table.setCellWidget(i, 2, item)  
            item = QtWidgets.QTableWidgetItem("")
            item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
            self.hard_table.setItem(i, 3, item) 
            item = QtWidgets.QTableWidgetItem("")
            item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
            self.hard_table.setItem(i, 4, item)            
        
        # self.hard_table.item(0, 3).setText("2200")
        # self.hard_table.item(4, 3).setText("2200")
        # self.hard_table.item(8, 3).setText("2200")

        self.hard_left_groupbox_layout.addWidget(self.hard_table)

        self.set_widget = QtWidgets.QWidget()
        self.set_widget_layout = QtWidgets.QGridLayout(self.set_widget)
        self.lb_wctj = QtWidgets.QLabel("误差统计：最大值")
        self.set_widget_layout.addWidget(self.lb_wctj, 0, 0, 1, 2)

        self.edt_max = QtWidgets.QLineEdit(self.set_widget)
        self.edt_max.setText("0")
        self.edt_max.setAlignment(Qt.AlignCenter)
        self.edt_max.setReadOnly(True)
        set_edt_bg(self.edt_max, QColor(Qt.gray))
        self.set_widget_layout.addWidget(self.edt_max, 0, 2, 1, 1)

        self.lb_zxz = QtWidgets.QLabel("mV   最小值")
        self.set_widget_layout.addWidget(self.lb_zxz, 0, 3, 1, 1)

        self.edt_min = QtWidgets.QLineEdit(self.set_widget)
        self.edt_min.setText("0")
        self.edt_min.setAlignment(Qt.AlignCenter)
        self.edt_min.setReadOnly(True)
        set_edt_bg(self.edt_min, QColor(Qt.gray))
        self.set_widget_layout.addWidget(self.edt_min, 0, 4, 1, 1)

        self.lb_jz = QtWidgets.QLabel("mV   均值")
        self.set_widget_layout.addWidget(self.lb_jz, 0, 5, 1, 1)      

        self.edt_avg = QtWidgets.QLineEdit(self.set_widget)
        self.edt_avg.setText("0")
        self.edt_avg.setAlignment(Qt.AlignCenter)
        self.edt_avg.setReadOnly(True)
        set_edt_bg(self.edt_avg, QColor(Qt.gray))
        self.set_widget_layout.addWidget(self.edt_avg, 0, 6, 1, 1)

        self.lb_bzz = QtWidgets.QLabel("mV   标准差")
        self.set_widget_layout.addWidget(self.lb_bzz, 0, 7, 1, 1)      

        self.edt_bzc = QtWidgets.QLineEdit(self.set_widget)
        self.edt_bzc.setText("0")
        self.edt_bzc.setAlignment(Qt.AlignCenter)
        self.edt_bzc.setReadOnly(True)
        set_edt_bg(self.edt_bzc, QColor(Qt.gray))
        self.set_widget_layout.addWidget(self.edt_bzc, 0, 8, 1, 1)

        self.lb_dw = QtWidgets.QLabel("mV")
        self.set_widget_layout.addWidget(self.lb_dw, 0, 9, 1, 1)  

        self.lb_dtjzz = QtWidgets.QLabel("单体校准值")
        self.set_widget_layout.addWidget(self.lb_dtjzz, 1, 0, 1, 1)  

        self.edt_cell = QtWidgets.QLineEdit(self.set_widget)
        self.edt_cell.setText("0")
        self.edt_cell.setAlignment(Qt.AlignCenter)
        self.edt_cell.setReadOnly(True)
        set_edt_bg(self.edt_cell, QColor(Qt.gray))
        self.set_widget_layout.addWidget(self.edt_cell, 1, 1, 1, 1)

        self.lb_dsw = QtWidgets.QLabel("mV")
        self.set_widget_layout.addWidget(self.lb_dsw, 1, 2, 1, 1)  

        self.btn_hand = QtWidgets.QPushButton("手动校准")
        self.set_widget_layout.addWidget(self.btn_hand, 1, 4, 1, 2)  

        self.btn_auto = QtWidgets.QPushButton("自动校准")
        self.set_widget_layout.addWidget(self.btn_auto, 1, 7, 1, 2)  

        self.hard_left_groupbox_layout.addWidget(self.set_widget)

        self.hard_layout.addWidget(self.hard_left_groupbox, 1)

        self.hard_right_frame = MyFrame(self.hard)
        self.hard_right_frame_layout = QtWidgets.QVBoxLayout(self.hard_right_frame)

        self.rtc_groupbox = QtWidgets.QGroupBox("RTC时钟校准")
        self.rtc_groupbox_layout = QtWidgets.QHBoxLayout(self.rtc_groupbox)
        self.lb_rtc = QtWidgets.QLabel("RTC时钟")
        self.rtc_groupbox_layout.addWidget(self.lb_rtc)
        self.qline_rtc = QtWidgets.QLineEdit(self.rtc_groupbox)
        self.qline_rtc.setReadOnly(True)
        self.rtc_groupbox_layout.addWidget(self.qline_rtc)
        self.qcheckbox_rtc = QtWidgets.QCheckBox("实时更新")
        self.rtc_groupbox_layout.addWidget(self.qcheckbox_rtc)
        self.btn_rtc = QtWidgets.QPushButton("时钟校准")
        self.rtc_groupbox_layout.addWidget(self.btn_rtc)
        self.hard_right_frame_layout.addWidget(self.rtc_groupbox, 1)


        self.vol_groupbox = QtWidgets.QGroupBox("总电压校准")
        self.vol_groupbox_layout = QtWidgets.QVBoxLayout(self.vol_groupbox)
        self.vol_table = QtWidgets.QTableWidget(self.vol_groupbox)
        self.vol_table.setColumnCount(7)
        self.vol_table.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
        self.vol_table.verticalHeader().setVisible(False)
        self.vol_table.verticalHeader().setHighlightSections(True)
        self.vol_table.verticalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Stretch)
        self.vol_table.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Stretch)
        self.vol_table.horizontalHeader().setSectionResizeMode(0, QtWidgets.QHeaderView.ResizeToContents)
        self.vol_table.setHorizontalHeaderLabels(["序号", "参数名称", "模拟总压值", "BMS上传值", "校准值", "误差", "单位"])
        self.vol_table.setRowCount(3)

        item = QtWidgets.QTableWidgetItem("1")
        item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.vol_table.setItem(0, 0, item)
        item = QtWidgets.QTableWidgetItem("总压")
        item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.vol_table.setItem(0, 1, item)
        item = QtWidgets.QLineEdit()
        item.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.vol_table.setCellWidget(0, 2, item)
        item = QtWidgets.QTableWidgetItem("")
        item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.vol_table.setItem(0, 3, item)
        item = QtWidgets.QLineEdit()
        item.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.vol_table.setCellWidget(0, 4, item)
        item = QtWidgets.QTableWidgetItem("")
        item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.vol_table.setItem(0, 5, item)
        item = QtWidgets.QTableWidgetItem("V")
        item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.vol_table.setItem(0, 6, item)

        item = QtWidgets.QTableWidgetItem("2")
        item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.vol_table.setItem(1, 0, item)
        item = QtWidgets.QTableWidgetItem("总压2")
        item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.vol_table.setItem(1, 1, item)
        item = QtWidgets.QLineEdit()
        item.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.vol_table.setCellWidget(1, 2, item)
        item = QtWidgets.QTableWidgetItem("")
        item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.vol_table.setItem(1, 3, item)
        item = QtWidgets.QLineEdit()
        item.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.vol_table.setCellWidget(1, 4, item)
        item = QtWidgets.QTableWidgetItem("")
        item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.vol_table.setItem(1, 5, item)
        item = QtWidgets.QTableWidgetItem("V")
        item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.vol_table.setItem(1, 6, item)

        item = QtWidgets.QTableWidgetItem("3")
        item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.vol_table.setItem(2, 0, item)
        item = QtWidgets.QTableWidgetItem("总压3")
        item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.vol_table.setItem(2, 1, item)
        item = QtWidgets.QLineEdit()
        item.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.vol_table.setCellWidget(2, 2, item)
        item = QtWidgets.QTableWidgetItem("")
        item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.vol_table.setItem(2, 3, item)
        item = QtWidgets.QLineEdit()
        item.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.vol_table.setCellWidget(2, 4, item)
        item = QtWidgets.QTableWidgetItem("")
        item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.vol_table.setItem(2, 5, item)
        item = QtWidgets.QTableWidgetItem("V")
        item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.vol_table.setItem(2, 6, item)

        self.vol_groupbox_layout.addWidget(self.vol_table)
        self.vol_widget = QtWidgets.QWidget(self.vol_groupbox)
        self.vol_widget_layout = QtWidgets.QHBoxLayout(self.vol_widget)
        self.btn_hand_cali = QtWidgets.QPushButton("手动校准")
        self.vol_widget_layout.addWidget(self.btn_hand_cali)
        self.btn_auto_cali = QtWidgets.QPushButton("自动校准")
        self.vol_widget_layout.addWidget(self.btn_auto_cali)
        self.vol_groupbox_layout.addWidget(self.vol_widget)
        self.hard_right_frame_layout.addWidget(self.vol_groupbox, 3)


        self.cur_groupbox = QtWidgets.QGroupBox("总电流校准")
        self.cur_groupbox_layout = QtWidgets.QVBoxLayout(self.cur_groupbox)
        self.cur_table = QtWidgets.QTableWidget(self.cur_groupbox)
        self.cur_table.setColumnCount(5)
        self.cur_table.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
        self.cur_table.verticalHeader().setVisible(False)
        self.cur_table.verticalHeader().setHighlightSections(True)
        self.cur_table.verticalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Stretch)
        self.cur_table.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Stretch)
        self.cur_table.horizontalHeader().setSectionResizeMode(0, QtWidgets.QHeaderView.ResizeToContents)
        self.cur_table.setHorizontalHeaderLabels(["测试工步", "STEP1", "STEP2", "STEP3", "单位"])
        self.cur_table.setRowCount(3)

        item = QtWidgets.QTableWidgetItem("模拟电流值")
        item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.cur_table.setItem(0, 0, item)
        item = QtWidgets.QLineEdit()
        item.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.cur_table.setCellWidget(0, 1, item)
        item = QtWidgets.QLineEdit()
        item.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.cur_table.setCellWidget(0, 2, item)
        item = QtWidgets.QLineEdit()
        item.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.cur_table.setCellWidget(0, 3, item)
        item = QtWidgets.QTableWidgetItem("A")
        item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.cur_table.setItem(0, 4, item)

        item = QtWidgets.QTableWidgetItem("BMS上传值")
        item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.cur_table.setItem(1, 0, item)
        item = QtWidgets.QTableWidgetItem("A")
        item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.cur_table.setItem(1, 4, item)

        item = QtWidgets.QTableWidgetItem("误差")
        item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.cur_table.setItem(2, 0, item)
        item = QtWidgets.QTableWidgetItem("A")
        item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.cur_table.setItem(2, 4, item)

        self.cur_groupbox_layout.addWidget(self.cur_table)
        self.cur_widget = QtWidgets.QWidget(self.cur_groupbox)
        self.cur_widget_layout = QtWidgets.QHBoxLayout(self.cur_widget)
        self.lb_cali = QtWidgets.QLabel("增益校准值")
        self.cur_widget_layout.addWidget(self.lb_cali)
        self.qline_cali = QtWidgets.QLabel(self.cur_widget)
        self.cur_widget_layout.addWidget(self.qline_cali)
        self.lb_zero = QtWidgets.QLabel("零漂校准值")
        self.cur_widget_layout.addWidget(self.lb_zero)
        self.qline_zero = QtWidgets.QLabel(self.cur_widget)
        self.cur_widget_layout.addWidget(self.qline_zero)
        self.btn_cur_hand_cali = QtWidgets.QPushButton("手动校准")
        self.cur_widget_layout.addWidget(self.btn_cur_hand_cali)
        self.btn_hand_auto_cali = QtWidgets.QPushButton("自动校准")
        self.cur_widget_layout.addWidget(self.btn_hand_auto_cali)
        self.cur_groupbox_layout.addWidget(self.cur_widget)
        self.hard_right_frame_layout.addWidget(self.cur_groupbox, 3)

        self.temp_groupbox = QtWidgets.QGroupBox("从控温度配置")
        self.temp_groupbox_layout = QtWidgets.QHBoxLayout(self.temp_groupbox)
        self.temp_qscrollarea = QtWidgets.QScrollArea(self.temp_groupbox)
        self.temp_qscrollarea.setWidgetResizable(True)
        self.temp_qscrollarea.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
        self.temp_groupbox_layout.addWidget(self.temp_qscrollarea)
        self.temp_qwidget = QtWidgets.QWidget()
        self.temp_qscrollarea.setWidget(self.temp_qwidget)
        self.temp_qscrollarea_layout = QtWidgets.QGridLayout(self.temp_qwidget)
        # self.temp_qscrollarea_layout.setContentsMargins(3, 3, 3, 3)
        self.temp_qscrollarea_layout.setSpacing(0)
        for i in range(16):
            if i == 0:
                bmu_label = QtWidgets.QLabel("CMU ID")
                # bmu_label.setAlignment(Qt.AlignCenter)
                self.temp_qscrollarea_layout.addWidget(bmu_label, i, 0, 1, 1)  
            else:              
                bmu_label = QtWidgets.QLabel("CMU" + str(i))
                # bmu_label.setAlignment(Qt.AlignCenter)
                self.temp_qscrollarea_layout.addWidget(bmu_label, i, 0, 1, 1)
        for i in range(16):
            for j in range(72):
                if i == 0:
                    bmu_label = QtWidgets.QLabel("T" + str(j + 1))
                    # bmu_label.setAlignment(Qt.AlignCenter)
                    self.temp_qscrollarea_layout.addWidget(bmu_label, i , j + 1, 1, 1)               
                else:
                    qtemp_checkbox = QtWidgets.QCheckBox()
                    # qtemp_lebal.setAlignment(Qt.AlignCenter)
                    # qtemp_checkbox
                    qtemp_checkbox.setFixedWidth(40)
                    # qtemp_lebal.setReadOnly(True)
                    self.temp_qscrollarea_layout.addWidget(qtemp_checkbox, i, j + 1, 1, 1)

        self.hard_right_frame_layout.addWidget(self.temp_groupbox, 2)
        
        self.btn_widget = QtWidgets.QWidget()
        self.btn_widget_layout = QtWidgets.QHBoxLayout(self.btn_widget)

        self.btn_read_hard_param = QtWidgets.QPushButton("读取硬件参数")
        self.btn_widget_layout.addWidget(self.btn_read_hard_param)

        self.btn_exit_hard_param = QtWidgets.QPushButton("退出参数标定")
        self.btn_widget_layout.addWidget(self.btn_exit_hard_param)

        self.hard_right_frame_layout.addWidget(self.btn_widget)
        self.hard_right_frame_layout.addStretch()

        self.hard_layout.addWidget(self.hard_right_frame, 1)

        QtCore.QMetaObject.connectSlotsByName(Hard)
