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


from ui.own.frame_theme import MyFrame
from utils.qt import QtWidgets, QMetaObject, QVBoxLayout, QGroupBox, Qt, QtCore
from utils.globalvar import SD
from ui.own.switch_button import SwitchButton

class Cell(MyFrame):
    def setupUi(self, Cell):
        self.cell = MyFrame(Cell)
        self.cell_layout = QVBoxLayout(self.cell)
        self.cell_layout.setContentsMargins(0, 0, 0, 0)
        self.cell_layout.setSpacing(0)

        self.group_cell_box = QGroupBox(self.cell)
        self.group_cell_box.setTitle("单体信息")
        self.group_cell_box_layout = QVBoxLayout(self.group_cell_box)

        self.btn_widget = QtWidgets.QWidget(self.group_cell_box)
        self.btn_widget_layout = QtWidgets.QHBoxLayout(self.btn_widget)

        self.btn_cell_vol = QtWidgets.QPushButton("单体电压")
        self.btn_widget_layout.addWidget(self.btn_cell_vol)
        self.btn_cell_temp = QtWidgets.QPushButton("单体温度")
        self.btn_widget_layout.addWidget(self.btn_cell_temp)

        self.btn_jz_temp = QtWidgets.QPushButton("极柱温度")
        self.btn_widget_layout.addWidget(self.btn_jz_temp)
        self.btn_bcu_temp = QtWidgets.QPushButton("极柱温度")
        self.btn_widget_layout.addWidget(self.btn_bcu_temp)

        self.btn_widget_layout.addStretch() 
        self.wdwdw = QtWidgets.QWidget(self.btn_widget)
        self.wdwdw_layout = QtWidgets.QVBoxLayout(self.wdwdw)
        self.plot_switch = SwitchButton(self.btn_widget)
        self.plot_switch.setMinimumSize(QtCore.QSize(70, 30))
        # size_policy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
        # size_policy.setHorizontalStretch(0)
        # size_policy.setVerticalStretch(0)
        # size_policy.setHeightForWidth(self.plot_switch.sizePolicy().hasHeightForWidth())
        self.wdwdw_layout.addWidget(self.plot_switch)
        self.btn_widget_layout.addWidget(self.wdwdw)
        self.lb_plot_switch = QtWidgets.QLabel("柱状图")
        self.btn_widget_layout.addWidget(self.lb_plot_switch)

        # self.btn_widget_layout.addStretch() 

        self.group_cell_box_layout.addWidget(self.btn_widget)

        self.under_right_widget = QtWidgets.QWidget(self.group_cell_box)
        self.under_right_layout = QVBoxLayout(self.under_right_widget)
        self.under_right_layout.setContentsMargins(0, 0, 0, 0)
        self.under_right_layout.setSpacing(0)

        self.cell_tab = QtWidgets.QStackedWidget(self.under_right_widget)
        self.cell_vol_table = QtWidgets.QTableWidget(self.cell_tab)
        self.cell_vol_table.setColumnCount(16)
        self.cell_vol_table.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
        # 设置只能选择行
        self.cell_vol_table.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
        self.cell_vol_table.setHorizontalHeaderLabels(["CMU ID", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15"])
        self.cell_vol_table.verticalHeader().setVisible(False)
        self.cell_vol_table.verticalHeader().setHighlightSections(True)
        self.cell_vol_table.verticalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Stretch)
        self.cell_vol_table.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Stretch)
        self.cell_vol_table.horizontalHeader().setSectionResizeMode(0, QtWidgets.QHeaderView.ResizeToContents)
        self.cell_vol_table.setRowCount(72)
        for i in range(72):
            item = QtWidgets.QTableWidgetItem("C" + str(i + 1))
            item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
            self.cell_vol_table.setItem(i, 0, item)
            for j in range(15):
                item = QtWidgets.QLineEdit("mV")
                item.setAlignment(Qt.AlignRight)
                item.setReadOnly(True)
                self.cell_vol_table.setCellWidget(i, j + 1, item)                
        self.cell_tab.addWidget(self.cell_vol_table)

        # self.cell_vol_table.cellWidget(0, 1).setText("333333mV")
        # self.cell_vol_table.cellWidget(1, 1).setText("333333mV")

        self.cell_temp_table = QtWidgets.QTableWidget(self.cell_tab)
        self.cell_temp_table.setColumnCount(16)
        self.cell_temp_table.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
        # 设置只能选择行
        self.cell_temp_table.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
        self.cell_temp_table.setHorizontalHeaderLabels(["CMU ID", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15"])
        self.cell_temp_table.verticalHeader().setVisible(False)
        self.cell_temp_table.verticalHeader().setHighlightSections(True)
        self.cell_temp_table.verticalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Stretch)
        self.cell_temp_table.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Stretch)
        self.cell_temp_table.horizontalHeader().setSectionResizeMode(0, QtWidgets.QHeaderView.ResizeToContents)
        self.cell_temp_table.setRowCount(72)
        for i in range(72):
            item = QtWidgets.QTableWidgetItem("T" + str(i + 1))
            item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
            self.cell_temp_table.setItem(i, 0, item)
            for j in range(15):
                item = QtWidgets.QLineEdit("℃")
                item.setAlignment(Qt.AlignRight)
                item.setReadOnly(True)
                self.cell_temp_table.setCellWidget(i, j + 1, item)                
        self.cell_tab.addWidget(self.cell_temp_table)

        self.under_right_layout.addWidget(self.cell_tab)
        self.group_cell_box_layout.addWidget(self.under_right_widget)
        self.cell_layout.addWidget(self.group_cell_box)
        
        #============================================================
        self.home_cell_table = QtWidgets.QTableWidget(self.group_cell_box)
        self.home_cell_table.setColumnCount(12)
        self.home_cell_table.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
        # 设置只能选择行
        self.home_cell_table.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
        self.home_cell_table.setHorizontalHeaderLabels(["编号", "电压", "温度", "编号", "电压", "温度", "编号", "电压", "温度", "编号", "电压", "温度"])
        self.home_cell_table.verticalHeader().setVisible(False)
        self.home_cell_table.verticalHeader().setHighlightSections(True)
        self.home_cell_table.verticalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Stretch)
        self.home_cell_table.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Stretch)
        # self.home_cell_table.horizontalHeader().setSectionResizeMode(0, QtWidgets.QHeaderView.ResizeToContents)
        self.home_cell_table.setRowCount(4)
        for i in range(4):
            for j in range(12):
                if j%3 == 0:
                    item = QtWidgets.QTableWidgetItem(str(int((i + 1) + 4 * (j/3))))
                    item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                    self.home_cell_table.setItem(i, j, item)
                if j%3 == 1:
                    item = QtWidgets.QLineEdit("mV")
                    item.setAlignment(Qt.AlignRight)
                    item.setReadOnly(True)
                    self.home_cell_table.setCellWidget(i, j, item)
                if j%3 == 2:
                    item = QtWidgets.QLineEdit("℃")
                    item.setAlignment(Qt.AlignRight)
                    item.setReadOnly(True)
                    self.home_cell_table.setCellWidget(i, j, item)
        self.group_cell_box_layout.addWidget(self.home_cell_table)            
        # self.cell_layout.addWidget(self.group_cell_box_layout)

        if SD.CCU_TYPE == 1:
            self.btn_widget.setHidden(False)
            self.home_cell_table.setHidden(True)
        else:
            self.btn_widget.setHidden(True)
            self.cell_tab.setHidden(True)
            self.home_cell_table.setHidden(False)
            self.group_cell_box_layout.addStretch() 

        self.group_set_box = QGroupBox(self.cell)
        self.group_set_box.setTitle("基本配置")
        self.group_set_box_layout = QtWidgets.QHBoxLayout(self.group_set_box)

        self.set_widget = QtWidgets.QWidget(self.group_set_box)
        self.set_widget_layout = QtWidgets.QGridLayout(self.set_widget)
        
        self.lb_cell_dccs = QtWidgets.QLabel("电池串数")
        self.set_widget_layout.addWidget(self.lb_cell_dccs, 0, 0)
        self.edt_cell_dccs = QtWidgets.QLineEdit("")
        self.set_widget_layout.addWidget(self.edt_cell_dccs, 0, 1)
        self.lb_cell_d = QtWidgets.QLabel("串")
        self.set_widget_layout.addWidget(self.lb_cell_d, 0, 2)

        self.lb_cell_temp = QtWidgets.QLabel("电池温度")
        self.set_widget_layout.addWidget(self.lb_cell_temp, 1, 0)
        self.edt_cell_temp = QtWidgets.QLineEdit("")
        self.set_widget_layout.addWidget(self.edt_cell_temp, 1, 1)
        self.lb_cell_dd = QtWidgets.QLabel("个")
        self.set_widget_layout.addWidget(self.lb_cell_dd, 1, 2)

        self.lb_mos_temp = QtWidgets.QLabel("MOS温度")
        self.set_widget_layout.addWidget(self.lb_mos_temp, 2, 0)
        self.edt_mos_temp = QtWidgets.QLineEdit("")
        self.set_widget_layout.addWidget(self.edt_mos_temp, 2, 1)
        self.lb_mos_dd = QtWidgets.QLabel("个")
        self.set_widget_layout.addWidget(self.lb_mos_dd, 2, 2)

        self.wgpe = QtWidgets.QLabel("温感配置")
        self.set_widget_layout.addWidget(self.wgpe, 0, 4, 1, 2)

        self.lb_t1 = QtWidgets.QLabel("T1")
        self.set_widget_layout.addWidget(self.lb_t1, 1, 4, 1, 1)
        self.cb_t1 = QtWidgets.QCheckBox(self.set_widget)
        self.set_widget_layout.addWidget(self.cb_t1, 1, 5, 1, 1)  
        self.lb_t2 = QtWidgets.QLabel("T2")
        self.set_widget_layout.addWidget(self.lb_t2, 2, 4, 1, 1)
        self.cb_t2 = QtWidgets.QCheckBox(self.set_widget)
        self.set_widget_layout.addWidget(self.cb_t2, 2, 5, 1, 1)  
        self.lb_t3 = QtWidgets.QLabel("T3")
        self.set_widget_layout.addWidget(self.lb_t3, 3, 4, 1, 1)
        self.cb_t3 = QtWidgets.QCheckBox(self.set_widget)
        self.set_widget_layout.addWidget(self.cb_t3, 3, 5, 1, 1)  
        self.lb_t4 = QtWidgets.QLabel("T4")
        self.set_widget_layout.addWidget(self.lb_t4, 4, 4, 1, 1)
        self.cb_t4 = QtWidgets.QCheckBox(self.set_widget)
        self.set_widget_layout.addWidget(self.cb_t4, 4, 5, 1, 1) 

        self.lb_t5 = QtWidgets.QLabel("T5")
        self.set_widget_layout.addWidget(self.lb_t5, 1, 7, 1, 1)
        self.cb_t5 = QtWidgets.QCheckBox(self.set_widget)
        self.set_widget_layout.addWidget(self.cb_t5, 1, 8, 1, 1)  
        self.lb_t6 = QtWidgets.QLabel("T6")
        self.set_widget_layout.addWidget(self.lb_t6, 2, 7, 1, 1)
        self.cb_t6 = QtWidgets.QCheckBox(self.set_widget)
        self.set_widget_layout.addWidget(self.cb_t6, 2, 8, 1, 1)  
        self.lb_t7 = QtWidgets.QLabel("T7")
        self.set_widget_layout.addWidget(self.lb_t7, 3, 7, 1, 1)
        self.cb_t7 = QtWidgets.QCheckBox(self.set_widget)
        self.set_widget_layout.addWidget(self.cb_t7, 3, 8, 1, 1)  
        self.lb_t8 = QtWidgets.QLabel("T8")
        self.set_widget_layout.addWidget(self.lb_t8, 4, 7, 1, 1)
        self.cb_t8 = QtWidgets.QCheckBox(self.set_widget)
        self.set_widget_layout.addWidget(self.cb_t8, 4, 8, 1, 1) 

        self.group_set_box_layout.addWidget(self.set_widget)

        self.btn_set_widget = QtWidgets.QWidget(self.group_set_box)
        self.btn_set_widget_layout = QtWidgets.QVBoxLayout(self.btn_set_widget)

        self.btn_read_set_param = QtWidgets.QPushButton("读取参数")
        self.btn_set_widget_layout.addWidget(self.btn_read_set_param)
        self.btn_update_set_param = QtWidgets.QPushButton("更新参数")
        self.btn_set_widget_layout.addWidget(self.btn_update_set_param)

        self.group_set_box_layout.addWidget(self.btn_set_widget)

        self.group_set_box_layout.addStretch() 
        self.cell_layout.addWidget(self.group_set_box)

        if SD.CCU_TYPE == 1:
            self.group_set_box.setHidden(True)

        QMetaObject.connectSlotsByName(Cell)
