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

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

class Balance(MyFrame):
    def setupUi(self, Balance):
        self.balance = MyFrame(Balance)
        self.balance_layout = QtWidgets.QGridLayout(self.balance)
        self.balance_layout.setContentsMargins(5, 5, 5, 5)
        self.balance_layout.setSpacing(0)

        self.line_groupbox = QtWidgets.QGroupBox(self.balance)
        self.line_groupbox.setTitle("采样线故障（红色：故障）")
        self.line_groupbox_layout = QtWidgets.QHBoxLayout(self.line_groupbox)
        self.line_qscrollarea = QtWidgets.QScrollArea(self.line_groupbox)
        self.line_qscrollarea.setWidgetResizable(True)
        self.line_qscrollarea.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
        self.line_groupbox_layout.addWidget(self.line_qscrollarea)
        self.line_qwidget = QtWidgets.QWidget()
        self.line_qscrollarea.setWidget(self.line_qwidget)
        self.line_qscrollarea_layout = QtWidgets.QGridLayout(self.line_qwidget)
        self.line_qscrollarea_layout.setContentsMargins(3, 3, 3, 3)
        self.line_qscrollarea_layout.setSpacing(0)
        for i in range(15):
            bmu_label = QtWidgets.QLabel("CMU" + str(i + 1))
            bmu_label.setAlignment(Qt.AlignCenter)
            self.line_qscrollarea_layout.addWidget(bmu_label, i, 0, 1, 1)
        for i in range(15):
            for j in range(72):
                qline_lebal = QtWidgets.QLineEdit("C" + str(j + 1))
                qline_lebal.setAlignment(Qt.AlignCenter)
                qline_lebal.setFixedWidth(50)
                qline_lebal.setReadOnly(True)
                self.line_qscrollarea_layout.addWidget(qline_lebal, i, j + 1, 1, 1)

        # self.line_groupbox = QtWidgets.QGroupBox(self.balance)
        # self.line_groupbox.setTitle("采样线故障（红：故障）")
        # self.line_groupbox_layout = QtWidgets.QVBoxLayout(self.line_groupbox)
        # self.line_qscrollarea = QtWidgets.QScrollArea(self.line_groupbox)
        # self.line_qscrollarea.setWidgetResizable(True)
        # # self.line_qscrollarea.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
        # self.line_groupbox_layout.addWidget(self.line_qscrollarea)
        # self.line_qwidget = QtWidgets.QWidget()
        # self.line_qscrollarea.setWidget(self.line_qwidget)
        # self.line_qscrollarea_layout = QtWidgets.QGridLayout(self.line_qwidget)
        # self.line_qscrollarea_layout.setContentsMargins(3, 3, 3, 3)
        # self.line_qscrollarea_layout.setSpacing(0)
        # for i in range(15):
        #     bmu_label = QtWidgets.QLabel("CMU" + str(i + 1))
        #     bmu_label.setAlignment(Qt.AlignCenter)
        #     self.line_qscrollarea_layout.addWidget(bmu_label, 0, i, 1, 1)
        # for i in range(15):
        #     for j in range(72):
        #         qline_lebal = QtWidgets.QLineEdit("C" + str(j + 1))
        #         qline_lebal.setAlignment(Qt.AlignCenter)
        #         qline_lebal.setReadOnly(True)
        #         qline_lebal.setFixedWidth(50)
        #         self.line_qscrollarea_layout.addWidget(qline_lebal, j + 1, i, 1, 1)
        # to do
        # set_edt_bg(self.line_qscrollarea_layout.itemAtPosition(2, 0).widget(), QtGui.QColor(Qt.red))
        # set_edt_bg(self.line_qscrollarea_layout.itemAtPosition(3, 1).widget(), QtGui.QColor(Qt.red))

        # self.line_status_widget = QtWidgets.QWidget()
        # self.line_status_widget_layout = QtWidgets.QHBoxLayout(self.line_status_widget)
        # self.line_status_widget_layout.setContentsMargins(0, 0, 0, 0)
        # self.line_status_widget_layout.setSpacing(0)
        # self.line_status_widget_layout.addStretch()
        # self.qline_lebal = QtWidgets.QLineEdit("")
        # self.qline_lebal.setAlignment(Qt.AlignCenter)
        # self.qline_lebal.setReadOnly(True)
        # set_edt_bg(self.qline_lebal, QtGui.QColor(Qt.white))
        # self.line_status_widget_layout.addWidget(self.qline_lebal)
        # self.lb_lebal = QtWidgets.QLabel("   无故障   ")
        # self.line_status_widget_layout.addWidget(self.lb_lebal)
        # self.line_status_widget_layout.addStretch()
        # self.line_groupbox_layout.addWidget(self.line_status_widget)

        # self.line_status_widget_2 = QtWidgets.QWidget()
        # self.line_status_widget_2_layout = QtWidgets.QHBoxLayout(self.line_status_widget_2)
        # self.line_status_widget_2_layout.setContentsMargins(0, 0, 0, 0)
        # self.line_status_widget_2_layout.setSpacing(0)
        # self.line_status_widget_2_layout.addStretch()
        # self.qline_bal = QtWidgets.QLineEdit("")
        # self.qline_bal.setAlignment(Qt.AlignCenter)
        # self.qline_bal.setReadOnly(True)
        # set_edt_bg(self.qline_bal, QtGui.QColor(Qt.red))
        # self.line_status_widget_2_layout.addWidget(self.qline_bal)
        # self.lb_bal = QtWidgets.QLabel("采样线故障")
        # self.line_status_widget_2_layout.addWidget(self.lb_bal)
        # self.line_status_widget_2_layout.addStretch()

        # self.line_groupbox_layout.addWidget(self.line_status_widget_2)


        self.balance_layout.addWidget(self.line_groupbox, 0, 0, 1, 1)

        self.on_off_groupbox = QtWidgets.QGroupBox(self.balance)
        self.on_off_groupbox.setTitle("均衡开关（绿色：开启）")
        self.on_off_groupbox_layout = QtWidgets.QHBoxLayout(self.on_off_groupbox)
        self.on_off_qscrollarea = QtWidgets.QScrollArea(self.on_off_groupbox)
        self.on_off_qscrollarea.setWidgetResizable(True)
        self.on_off_qscrollarea.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
        self.on_off_groupbox_layout.addWidget(self.on_off_qscrollarea)
        self.on_off_qwidget = QtWidgets.QWidget()
        self.on_off_qscrollarea.setWidget(self.on_off_qwidget)
        self.on_off_qscrollarea_layout = QtWidgets.QGridLayout(self.on_off_qwidget)
        self.on_off_qscrollarea_layout.setContentsMargins(3, 3, 3, 3)
        self.on_off_qscrollarea_layout.setSpacing(0)
        for i in range(15):
            bmu_label = QtWidgets.QLabel("CMU" + str(i + 1))
            bmu_label.setAlignment(Qt.AlignCenter)
            self.on_off_qscrollarea_layout.addWidget(bmu_label, i, 0, 1, 1)
        for i in range(15):
            for j in range(72):
                qon_off_lebal = QtWidgets.QLineEdit("C" + str(j + 1))
                qon_off_lebal.setAlignment(Qt.AlignCenter)
                qon_off_lebal.setFixedWidth(50)
                qon_off_lebal.setReadOnly(True)
                self.on_off_qscrollarea_layout.addWidget(qon_off_lebal, i, j + 1, 1, 1)

        # set_edt_bg(self.on_off_qscrollarea_layout.itemAtPosition( 0 + 0,1).widget(), QtGui.QColor(Qt.red))
        # set_edt_bg(self.on_off_qscrollarea_layout.itemAtPosition( 0 + 2, 6).widget(), QtGui.QColor(Qt.red))
        # set_edt_bg(self.on_off_qscrollarea_layout.itemAtPosition(3, 1).widget(), QtGui.QColor(Qt.red))

        # self.on_off_widget = QtWidgets.QWidget()
        # self.on_off_widget_layout = QtWidgets.QVBoxLayout(self.on_off_widget)
        # self.on_off_widget_layout.setContentsMargins(0, 0, 0, 0)
        # self.on_off_widget_layout.setSpacing(0)
        # self.on_off_widget_layout.addStretch()
        # self.qline_l = QtWidgets.QLineEdit("")
        # self.qline_l.setAlignment(Qt.AlignCenter)
        # self.qline_l.setReadOnly(True)
        # set_edt_bg(self.qline_l, QtGui.QColor(Qt.white))
        # self.on_off_widget_layout.addWidget(self.qline_l)
        # self.lb_l = QtWidgets.QLabel("关闭均衡")
        # self.lb_l.setAlignment(Qt.AlignCenter)
        # self.on_off_widget_layout.addWidget(self.lb_l)
        # self.qline_ll = QtWidgets.QLineEdit("")
        # self.qline_ll.setAlignment(Qt.AlignCenter)
        # self.qline_ll.setReadOnly(True)
        # set_edt_bg(self.qline_ll, QtGui.QColor(Qt.green))
        # self.on_off_widget_layout.addWidget(self.qline_ll)
        # self.lb_ll = QtWidgets.QLabel("打开均衡")
        # self.lb_ll.setAlignment(Qt.AlignCenter)
        # self.on_off_widget_layout.addWidget(self.lb_ll)
        # self.on_off_widget_layout.addStretch()

        # self.on_off_groupbox_layout.addWidget(self.on_off_widget)


        self.balance_layout.addWidget(self.on_off_groupbox, 0, 1, 1, 1)

        # self.balance_control_groupbox = QtWidgets.QGroupBox(self.balance)
        # self.balance_control_groupbox.setTitle("均衡强制控制（选中：控制）")
        # self.balance_control_groupbox_layout = QtWidgets.QVBoxLayout(self.balance_control_groupbox)
        # self.balance_control_qscrollarea = QtWidgets.QScrollArea(self.balance_control_groupbox)
        # self.balance_control_qscrollarea.setWidgetResizable(True)
        # # self.balance_control_qscrollarea.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
        # self.balance_control_groupbox_layout.addWidget(self.balance_control_qscrollarea)
        # self.balance_control_qwidget = QtWidgets.QWidget()
        # self.balance_control_qscrollarea.setWidget(self.balance_control_qwidget)
        # self.balance_control_qscrollarea_layout = QtWidgets.QGridLayout(self.balance_control_qwidget)
        # self.balance_control_qscrollarea_layout.setContentsMargins(3, 3, 3, 3)
        # self.balance_control_qscrollarea_layout.setSpacing(0)
        # for i in range(15):
        #     bmu_label = QtWidgets.QLabel("CMU" + str(i + 1))
        #     bmu_label.setAlignment(Qt.AlignCenter)
        #     self.balance_control_qscrollarea_layout.addWidget(bmu_label, 0, i, 1, 1)
        # for i in range(15):
        #     for j in range(72):
        #         qbalance_control_widget = QtWidgets.QWidget()
        #         qbalance_control_widget_layout = QtWidgets.QHBoxLayout(qbalance_control_widget)
        #         qbalance_control_widget_layout.setContentsMargins(5, 5, 5, 5)
        #         qbalance_control_widget_layout.setSpacing(0)
        #         qbalance_control_lebal = QtWidgets.QLineEdit("C" + str(j + 1))
        #         qbalance_control_lebal.setAlignment(Qt.AlignCenter)
        #         qbalance_control_lebal.setReadOnly(True)
        #         qbalance_control_lebal.setFixedWidth(50)
        #         qbalance_control_widget_layout.addWidget(qbalance_control_lebal)
        #         qbalance_control_checkbox = QtWidgets.QCheckBox()
        #         qbalance_control_widget_layout.addWidget(qbalance_control_checkbox)
        #         self.balance_control_qscrollarea_layout.addWidget(qbalance_control_widget, j + 1, i, 1, 1)
        
        self.balance_control_groupbox = QtWidgets.QGroupBox(self.balance)
        self.balance_control_groupbox.setTitle("均衡强制控制（选中：控制）")
        self.balance_control_groupbox_layout = QtWidgets.QHBoxLayout(self.balance_control_groupbox)
        self.balance_control_qscrollarea = QtWidgets.QScrollArea(self.balance_control_groupbox)
        self.balance_control_qscrollarea.setWidgetResizable(True)
        self.balance_control_qscrollarea.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
        self.balance_control_groupbox_layout.addWidget(self.balance_control_qscrollarea)
        self.balance_control_qwidget = QtWidgets.QWidget()
        self.balance_control_qscrollarea.setWidget(self.balance_control_qwidget)
        self.balance_control_qscrollarea_layout = QtWidgets.QGridLayout(self.balance_control_qwidget)
        self.balance_control_qscrollarea_layout.setContentsMargins(3, 3, 3, 3)
        self.balance_control_qscrollarea_layout.setSpacing(0)
        for i in range(15):
            bmu_label = QtWidgets.QLabel("CMU" + str(i + 1))
            bmu_label.setAlignment(Qt.AlignCenter)
            self.balance_control_qscrollarea_layout.addWidget(bmu_label, i, 0, 1, 1)
        for i in range(15):
            for j in range(72):
                qbalance_control_widget = QtWidgets.QWidget()
                qbalance_control_widget_layout = QtWidgets.QHBoxLayout(qbalance_control_widget)
                qbalance_control_widget_layout.setContentsMargins(5, 5, 5, 5)
                qbalance_control_widget_layout.setSpacing(0)
                qbalance_control_lebal = QtWidgets.QLineEdit("C" + str(j + 1))
                qbalance_control_lebal.setAlignment(Qt.AlignCenter)
                qbalance_control_lebal.setReadOnly(True)
                qbalance_control_lebal.setFixedWidth(50)
                qbalance_control_widget_layout.addWidget(qbalance_control_lebal)
                qbalance_control_checkbox = QtWidgets.QCheckBox()
                qbalance_control_widget_layout.addWidget(qbalance_control_checkbox)
                self.balance_control_qscrollarea_layout.addWidget(qbalance_control_widget, i, j + 1, 1, 1)
        
        self.balance_layout.addWidget(self.balance_control_groupbox, 1, 0, 1, 1)
        # self.balance_control_qscrollarea_layout.itemAtPosition(0 + 0, 1).widget().layout().itemAt(1).widget().stateChanged.connect(lambda state = self.balance_control_qscrollarea_layout.itemAtPosition(0 + 0, 1).widget().layout().itemAt(1).widget(), cmu_id = i + 1, cell_id = j + 1: self._balance_ctl(state, cmu_id, cell_id))    
        # set_edt_bg(self.on_off_qscrollarea_layout.itemAtPosition( 0 + 0,1).widget(), QtGui.QColor(Qt.red))
        # self.balance_control_qscrollarea_layout.itemAtPosition(2, 3).widget().layout().itemAt(1).widget().setCheckState(Qt.Checked)
        # self.balance_control_qscrollarea_layout.itemAtPosition(0, 1).widget().layout().itemAt(1).widget().setCheckState(Qt.Checked)

        self.balance_fault_groupbox = QtWidgets.QGroupBox(self.balance)
        self.balance_fault_groupbox.setTitle("均衡故障（红色：故障）")
        self.balance_fault_groupbox_layout = QtWidgets.QHBoxLayout(self.balance_fault_groupbox)
        self.balance_fault_qscrollarea = QtWidgets.QScrollArea(self.balance_fault_groupbox)
        self.balance_fault_qscrollarea.setWidgetResizable(True)
        self.balance_fault_qscrollarea.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
        self.balance_fault_groupbox_layout.addWidget(self.balance_fault_qscrollarea)
        self.balance_fault_qwidget = QtWidgets.QWidget()
        self.balance_fault_qscrollarea.setWidget(self.balance_fault_qwidget)
        self.balance_fault_qscrollarea_layout = QtWidgets.QGridLayout(self.balance_fault_qwidget)
        self.balance_fault_qscrollarea_layout.setContentsMargins(3, 3, 3, 3)
        self.balance_fault_qscrollarea_layout.setSpacing(0)
        for i in range(15):
            bmu_label = QtWidgets.QLabel("CMU" + str(i + 1))
            bmu_label.setAlignment(Qt.AlignCenter)
            self.balance_fault_qscrollarea_layout.addWidget(bmu_label, i, 0, 1, 1)
        for i in range(15):
            for j in range(72):
                qbalance_fault_lebal = QtWidgets.QLineEdit("C" + str(j + 1))
                qbalance_fault_lebal.setAlignment(Qt.AlignCenter)
                qbalance_fault_lebal.setReadOnly(True)
                qbalance_fault_lebal.setFixedWidth(50)
                self.balance_fault_qscrollarea_layout.addWidget(qbalance_fault_lebal, i, j + 1, 1, 1)

        # set_edt_bg(self.balance_fault_qscrollarea_layout.itemAtPosition( 0 + 0,1).widget(), QtGui.QColor(Qt.red))
        # set_edt_bg(self.balance_fault_qscrollarea_layout.itemAtPosition( 0 + 2, 6).widget(), QtGui.QColor(Qt.red))

        # self.balance_fault_widget = QtWidgets.QWidget()
        # self.balance_fault_widget_layout = QtWidgets.QVBoxLayout(self.balance_fault_widget)
        # self.balance_fault_widget_layout.setContentsMargins(0, 0, 0, 0)
        # self.balance_fault_widget_layout.setSpacing(0)
        # self.balance_fault_widget_layout.addStretch()
        # self.edt_fault = QtWidgets.QLineEdit("")
        # self.edt_fault.setAlignment(Qt.AlignCenter)
        # self.edt_fault.setReadOnly(True)
        # set_edt_bg(self.edt_fault, QtGui.QColor(Qt.white))
        # self.balance_fault_widget_layout.addWidget(self.edt_fault)
        # self.lb_fault = QtWidgets.QLabel("无故障")
        # self.lb_fault.setAlignment(Qt.AlignCenter)
        # self.balance_fault_widget_layout.addWidget(self.lb_fault)
        # self.edt_faultl = QtWidgets.QLineEdit("")
        # self.edt_faultl.setAlignment(Qt.AlignCenter)
        # self.edt_faultl.setReadOnly(True)
        # set_edt_bg(self.edt_faultl, QtGui.QColor(Qt.red))
        # self.balance_fault_widget_layout.addWidget(self.edt_faultl)
        # self.lb_faultl = QtWidgets.QLabel("均衡故障")
        # self.lb_faultl.setAlignment(Qt.AlignCenter)
        # self.balance_fault_widget_layout.addWidget(self.lb_faultl)
        # self.balance_fault_widget_layout.addStretch()

        # self.balance_fault_groupbox_layout.addWidget(self.balance_fault_widget)        
        
        self.balance_layout.addWidget(self.balance_fault_groupbox, 1, 1, 1, 1)

        QtCore.QMetaObject.connectSlotsByName(Balance)
