#!D:/IDE/anaconda3/envs python
# -*- coding: UTF-8 -*-
"""
 @Author: Administrator
 @FileName：GroupSlideStatusView.py\n
 @DateTime: 2025/7/25 14:42\n
 @Author  ：xsh
"""
from AppInterfaces.UiInterface import UiInterface, AppUtils
from AppCore.Levels import *
from AppUtils.UiUtils import UiUtils
from AppHeadEnd.Commons.SlideViews.SlideStatusView import SlideStatusView

class GroupSlideStatusView(Qt.QWidget, UiInterface):
    ChamberGroup        : Qt.QGroupBox              #
    GroupStatus         : list[SlideStatusView]     #
    ChamberGroupNumber  : int                       #

    def __init__(self, chamber_number: int = 1, parent: Qt.QWidget = None):
        super(GroupSlideStatusView, self).__init__(parent=parent)
        self.Initialization('GroupSlideStatusView', self, parent)
        self.initWidgetStyle()
        self.ChamberGroupNumber = chamber_number if isinstance(chamber_number, int) else 1
        self.flushFrame()

    def flushFrame(self):
        try:
            self.ChamberGroup.setTitle('片仓{:<2d}'.format(self.ChamberGroupNumber))
            for item in self.GroupStatus:
                item.Model.ChamberGroupNumber = self.ChamberGroupNumber
                item.flushFrame()
        except Exception as e:
            AppUtils.print_Exception(e)

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

    def initWindows(self):
        self.initChamberGroup        ()
        self.initGroupStatus         ()
        self.initChamberGroupNumber  ()

    def initChamberGroup        (self):
        self.initChamberGroupNumber()
        if not self.hasChamberGroup      :
            self.ChamberGroup       = Qt.QGroupBox(self)
        self.ChamberGroup.setTitle('片仓{:<2d}'.format(self.ChamberGroupNumber))

    def initGroupStatus         (self):
        self.initChamberGroup()
        if not self.hasGroupStatus       :
            self.GroupStatus        = [
                SlideStatusView(row_number=1 ),
                SlideStatusView(row_number=2 ),
                SlideStatusView(row_number=3 ),
                SlideStatusView(row_number=4 ),
                SlideStatusView(row_number=5 ),
                SlideStatusView(row_number=6 ),
                SlideStatusView(row_number=7 ),
                SlideStatusView(row_number=8 ),
                SlideStatusView(row_number=9 ),
                SlideStatusView(row_number=10),
                SlideStatusView(row_number=11),
                SlideStatusView(row_number=12),
                SlideStatusView(row_number=13),
                SlideStatusView(row_number=14),
                SlideStatusView(row_number=15),
                SlideStatusView(row_number=16),
                SlideStatusView(row_number=17),
                SlideStatusView(row_number=18),
                SlideStatusView(row_number=19),
                SlideStatusView(row_number=20),
            ]
        for index, item in enumerate(self.GroupStatus):
            item.Model.ChamberGroupNumber = self.ChamberGroupNumber
            item.Model.ChamberRowNumber   = index + 1
            item.flushFrame()
    def initChamberGroupNumber  (self):
        if not self.hasChamberGroupNumber:
            self.ChamberGroupNumber = 1

    def initChamberStatus(self):
        for index, item in enumerate(self.GroupStatus):
            item.Model.ChamberGroupNumber = self.ChamberGroupNumber
            item.Model.ChamberRowNumber   = index + 1
            item.initChamberStatus()
    def initLayouts(self):
        if self.hasWindowLayouts:
            return
        self.initWindows()
        layout: Qt.QVBoxLayout = Qt.QVBoxLayout()
        layout.setSpacing(1)
        layout.setContentsMargins( 1, 10, 1, 1)
        g_layout: Qt.QVBoxLayout = Qt.QVBoxLayout()
        g_layout.setSpacing(0)
        g_layout.setContentsMargins(10, 25, 10, 10)
        for item in self.GroupStatus:
            g_layout.addWidget(item, 1)
        self.ChamberGroup.setLayout(g_layout)
        layout.addWidget(self.ChamberGroup)
        self.setLayout(layout)


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

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

    @property
    def hasChamberGroupNumber  (self) -> bool:
        return hasattr(self, 'ChamberGroupNumber') and isinstance(self.ChamberGroupNumber  , int              )

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


    def statusChanged(self, chamber_number: int):
        self.initWindows()
        self.ChamberGroupNumber = chamber_number



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

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

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

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

