#!D:/IDE/anaconda3/envs python
# -*- coding: UTF-8 -*-
"""
 @Author: Administrator
 @FileName：ChamberStatusView.py\n
 @DateTime: 2025/7/25 15:16\n
 @Author  ：xsh
"""
from AppInterfaces.UiInterface import UiInterface
from AppCore.Levels import *
from AppUtils.UiUtils import UiUtils
from AppHeadEnd.Commons.SlideViews.GroupSlideStatusView import GroupSlideStatusView
from AppInterfaces.SingletonInterface import SingletonInterface
from AppInterfaces.globalSlot import globalSlot, AppSlot
class ChamberStatusView(Qt.QScrollArea, UiInterface, SingletonInterface):
    Locker = Qt.QMutex()
    currentLStatusChanged: Qt.pyqtSignal = Qt.pyqtSignal(object, object, object)
    currentRStatusChanged: Qt.pyqtSignal = Qt.pyqtSignal(object, object, object)
    currentCodeChanged   : Qt.pyqtSignal = Qt.pyqtSignal(object, object, object)
    GroupStatus         : list[GroupSlideStatusView]    #
    ChamberBox          : Qt.QGroupBox                  #
    def __init__(self, parent: Qt.QWidget = None):
        super(ChamberStatusView, self).__init__(parent=parent)
        self.Initialization('ChamberStatusView', self, parent)
        self.initWidgetStyle    ()
        self.initWindowIcon     ()
        self.setWindowTitle('片仓状态')
        self.flushFrame         ()

    def flushFrame(self):
        if self.hasGroupStatus:
            for group in self.GroupStatus:
                group.flushFrame()

    def flushGroup(self, chamber_number: int):
        try:
            chamber: int = min(10, max(1, chamber_number)) - 1
            self.GroupStatus[chamber].flushFrame()
        except Exception as e:
            pass

    def flushAllGroup(self):
        try:
            for group in self.GroupStatus:
                group.flushFrame()
        except Exception as e:
            pass

    @classmethod
    def GetInstance(cls, *args, **kwargs) -> 'ChamberStatusView':
        return super(ChamberStatusView, cls).GetInstance(*args, **kwargs)

    def initValue(self):
        super(ChamberStatusView, self).initValue()
        self.initWindows()

    def initProperty(self):
        super(ChamberStatusView, self).initProperty()
        self.setFixedSize(1440, 900)

    def initConnect(self):
        super(ChamberStatusView, self).initConnect()
        TYPE = Qt.Qt.ConnectionType(Qt.Qt.AutoConnection | Qt.Qt.UniqueConnection)
        self.currentLStatusChanged.connect(slot=self.currentLStatusChangedSlot, type=TYPE)
        self.currentRStatusChanged.connect(slot=self.currentRStatusChangedSlot, type=TYPE)
        self.currentCodeChanged   .connect(slot=self.currentCodeChangedSlot   , type=TYPE)
        AppSlot.currentSlideLStateChanged.connect(slot=self.currentLStatusChangedSlot   , type=TYPE)
        AppSlot.currentSlideRStateChanged.connect(slot=self.currentRStatusChangedSlot   , type=TYPE)
        AppSlot.currentSlideCodeChanged  .connect(slot=self.currentCodeChangedSlot      , type=TYPE)
        AppSlot.currentSlideStateChanged .connect(slot=self.currentSlideStateChangedSlot, type=TYPE)
        AppSlot.InitCheckStatusSignal    .connect(slot=self.SlotInitCheckStatusSignal   , type=TYPE)


    def SlotInitCheckStatusSignal(self, chamber_number: int, row_number: int):
        try:
            self.initWindows()
            chamber: int = min(10, max(1, chamber_number)) - 1
            self.GroupStatus[chamber].initChamberStatus()
        except Exception as e:
            pass

    def currentLStatusChangedSlot (self, chamber_number: int, row_number: int, status: (bool, int, str)):
        self.set_l_status(chamber_number, row_number, status)
    def currentRStatusChangedSlot (self, chamber_number: int, row_number: int, status: (bool, int, str)):
        self.set_r_status(chamber_number, row_number, status)
    def currentCodeChangedSlot    (self, chamber_number: int, row_number: int, status: (bool, int, str)):
        self.set_code_string(chamber_number, row_number, status)

    def currentSlideStateChangedSlot(self, chamber_number: int, l_status: list[int], r_status: list[int]):
        self.changedStatus(chamber_number)

    def initLayouts(self):
        if self.hasWindowLayouts:
            return
        layout: Qt.QVBoxLayout = Qt.QVBoxLayout()
        layout.setSpacing           (1          )
        layout.setContentsMargins   (5, 10, 5, 5 )

        g_layout: Qt.QVBoxLayout = Qt.QVBoxLayout()
        g_layout.setSpacing(1)
        g_layout.setContentsMargins(10, 20, 10, 10)
        t_layout: Qt.QHBoxLayout = Qt.QHBoxLayout()
        b_layout: Qt.QHBoxLayout = Qt.QHBoxLayout()
        t_layout.setSpacing(1)
        t_layout.setContentsMargins(5, 5, 5, 5)
        b_layout.setSpacing(1)
        b_layout.setContentsMargins(5, 5, 5, 5)

        t_layout.addWidget(self.GroupStatus[5])
        t_layout.addWidget(self.GroupStatus[6])
        t_layout.addWidget(self.GroupStatus[7])
        t_layout.addWidget(self.GroupStatus[8])
        t_layout.addWidget(self.GroupStatus[9])

        b_layout.addWidget(self.GroupStatus[0])
        b_layout.addWidget(self.GroupStatus[1])
        b_layout.addWidget(self.GroupStatus[2])
        b_layout.addWidget(self.GroupStatus[3])
        b_layout.addWidget(self.GroupStatus[4])

        g_layout.addLayout(t_layout)
        g_layout.addLayout(b_layout)

        self.ChamberBox.setLayout(g_layout)
        layout.addWidget(self.ChamberBox)
        self.setLayout(layout)
        self.setWidget(self.ChamberBox)



    def initGroupStatus(self):
        if not self.hasGroupStatus:
            self.GroupStatus = [
                GroupSlideStatusView(chamber_number=1 , parent=self),
                GroupSlideStatusView(chamber_number=2 , parent=self),
                GroupSlideStatusView(chamber_number=3 , parent=self),
                GroupSlideStatusView(chamber_number=4 , parent=self),
                GroupSlideStatusView(chamber_number=5 , parent=self),
                GroupSlideStatusView(chamber_number=6 , parent=self),
                GroupSlideStatusView(chamber_number=7 , parent=self),
                GroupSlideStatusView(chamber_number=8 , parent=self),
                GroupSlideStatusView(chamber_number=9 , parent=self),
                GroupSlideStatusView(chamber_number=10, parent=self),
            ]
        for group in self.GroupStatus:
            group.flushFrame()
    def initChamberBox(self):
        if not self.hasChamberBox:
            self.ChamberBox = Qt.QGroupBox(self)
        self.ChamberBox.setTitle('片仓状态')

    def initWindows(self):
        self.initGroupStatus()
        self.initChamberBox ()
    @property
    def hasGroupStatus(self) -> bool:
        return hasattr(self, 'GroupStatus') and isinstance(self.GroupStatus, list)

    @property
    def hasChamberBox(self) -> bool:
        return hasattr(self, 'ChamberBox') and isinstance(self.ChamberBox, Qt.QGroupBox)


    def set_l_status(self, chamber_number: int, row_number: int, status: (bool, int)):
        self.initWindows()
        chamber : int = min(10, max(1, chamber_number)) - 1
        row     : int = min(20, max(1, row_number)) - 1
        self.GroupStatus[chamber].set_l_status(chamber + 1, row + 1, status)


    def set_r_status(self, chamber_number: int, row_number: int, status: (bool, int)):
        self.initWindows()
        chamber : int = min(10, max(1, chamber_number)) - 1
        row     : int = min(20, max(1, row_number)) - 1
        self.GroupStatus[chamber].set_r_status(chamber + 1, row + 1, status)

    def changedStatus(self, chamber_number: int):
        self.initWindows()
        chamber: int = min(10, max(1, chamber_number)) - 1
        self.GroupStatus[chamber].statusChanged(chamber + 1)

    def set_code_string(self, chamber_number: int, row_number: int, code: str):
        self.initWindows()
        chamber : int = min(10, max(1, chamber_number)) - 1
        row     : int = min(20, max(1, row_number)) - 1
        self.GroupStatus[chamber].set_code_string(chamber + 1, row + 1, code)


    def set_row_number(self, chamber_number: int, row_number: int):
        self.initWindows()
        chamber : int = min(10, max(1, chamber_number)) - 1
        row     : int = min(20, max(1, row_number)) - 1
        self.GroupStatus[chamber].set_row_number(chamber + 1, row + 1)


    def initWidgetStyle(self):
        self.setStyleSheet(UiUtils.getWidgetStyle())


