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


from ui.balance import Balance
from ui.own.frame_theme import MyFrame
from ui.own.my_chart_view import MyChartView
from utils.globalvar import SD
from utils.qt import QMessageBox, QChart, QValueAxis, QHBoxLayout, QPainter, Qt, Signal, Slot, QLegendMarker


class Win_Balance(Balance, MyFrame):

    balance_control_signal = Signal(int, int, int)

    def __init__(self, parent=None):
        super(Win_Balance, self).__init__(parent)
        self.setupUi(self)


        for i in range(15):
            for j in range(72):
                # self.balance_control_qscrollarea_layout.itemAtPosition(j + 1, i).widget().layout().itemAt(1).widget().stateChanged.connect(lambda state = self.balance_control_qscrollarea_layout.itemAtPosition(j + 1, i).widget().layout().itemAt(1).widget(), cmu_id = i + 1, cell_id = j + 1: self._balance_ctl(state, cmu_id, cell_id))
                self.balance_control_qscrollarea_layout.itemAtPosition(i, j + 1).widget().layout().itemAt(1).widget().stateChanged.connect(lambda state = self.balance_control_qscrollarea_layout.itemAtPosition(i, j + 1).widget().layout().itemAt(1).widget(), cmu_id = i + 1, cell_id = j + 1: self._balance_ctl(state, cmu_id, cell_id))    

    def can_connect_error(self):
        QMessageBox.critical(self, "连接状态" if SD.SYSTEM_LANGUAGE == 0 else "Connect State", "CAN 连接失败！" if SD.SYSTEM_LANGUAGE == 0 else "CAN Connect Error！")


    def _balance_ctl(self, state, cmu_id, cell_id):
        # print(cmu_id, cell_id, state)
        self.balance_control_signal.emit(cmu_id, cell_id, state)