#!D:/IDE/anaconda3/envs python
# -*- coding: UTF-8 -*-
"""
 @Author: Administrator
 @FileName：SlideStatusView.py\n
 @DateTime: 2025/7/25 13:06\n
 @Author  ：xsh
"""
from abc import abstractmethod
from PyQt5 import Qt
from AppInterfaces.UiInterface import UiInterface, AppConvert, AppUtils
from AppCore.Contexts import *
from AppCore.DBModels import *
from AppCore.Levels import *
from AppUtils.UiUtils import UiUtils

class SlideStatusView(Qt.QWidget, UiInterface):
    Model               : SlideModel
    ChamberRowNumber    : Qt.QLabel
    LState              : Qt.QLabel
    CodeString          : Qt.QLabel
    RState              : Qt.QLabel

    def __init__(self, row_number: int = 1, parent: Qt.QWidget = None):
        super(SlideStatusView, self).__init__(parent=parent)
        self.Initialization('SlideStatusView', self, parent)
        self.initWidgetStyle()
        self.Model.ChamberRowNumber = row_number if isinstance(row_number, int) else 1
        self.flushFrame()


    def setModel(self, model: SlideModelInterface):
        if not model:
            return
        self.initModel()
        self.Model.as_field(model.to_field())
        self.flushFrame()

    def set_l_status(self, chamber_number: int, row_number: int, status: (bool, int)):
        self.initModel()
        self.Model.ChamberGroupNumber = chamber_number
        self.Model.ChamberRowNumber = row_number
        self.Model.LState = status
        self.flushFrame()

    def set_r_status(self, chamber_number: int, row_number: int, status: (bool, int)):
        self.initModel()
        self.Model.ChamberGroupNumber = chamber_number
        self.Model.ChamberRowNumber = row_number
        self.Model.RState = status
        self.flushFrame()

    def set_code_string(self, chamber_number: int, row_number: int, code: str):
        self.initModel()
        self.Model.ChamberGroupNumber = chamber_number
        self.Model.ChamberRowNumber = row_number
        self.Model.CodeString = f'{code}'
        self.flushFrame()

    def set_row_number(self, chamber_number: int, row_number: int):
        self.initModel()
        self.Model.ChamberGroupNumber = chamber_number
        self.Model.ChamberRowNumber = row_number
        self.flushFrame()

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

    def initValue(self):
        super(SlideStatusView, self).initValue()
        self.initModel           ()
        self.initChamberRowNumber()
        self.initLState          ()
        self.initCodeString      ()
        self.initRState          ()


    def initProperty(self):
        super(SlideStatusView, self).initProperty()
        self.ChamberRowNumber   .setFixedSize(40    , 25)
        self.LState             .setFixedSize(25    , 25)
        self.CodeString         .setFixedSize(200   , 25)
        self.RState             .setFixedSize(25    , 25)


    def initLayouts(self):
        if self.hasWindowLayouts:
            return
        layout: Qt.QHBoxLayout = Qt.QHBoxLayout()
        layout.setSpacing(0)
        layout.setContentsMargins(0, 0, 0, 0)
        layout.addWidget(self.ChamberRowNumber )
        layout.addWidget(self.LState           )
        layout.addWidget(self.CodeString       )
        layout.addWidget(self.RState           )
        self.setLayout(layout)

    def flushFrame(self):
        self.initModel()
        UiUtils.setColorStyle(self.LState, self.Model.leftCheck ())
        UiUtils.setColorStyle(self.RState, self.Model.rightCheck())
        self.ChamberRowNumber.setText('{:^2d}'.format(self.Model.ChamberRowNumber if self.Model.has_ChamberRowNumber else 0))
        self.CodeString.setText('{0}'.format(self.Model.CodeString if self.Model.hasCodeString else ''))

    def initModel            (self):
        if not self.hasModel           :
            self.Model            = SlideModel(ChamberRowNumber = 1 ,LState           = 0 ,CodeString       = '',RState           = 0 )
        if not self.hasModel:
            model: ChamberStatusModel = ChamberStatusModel.getStatusModel(self.Model.ChamberGroupNumber, self.Model.ChamberRowNumber)
            if not model:
                return
            self.Model = model.copy()



    def initChamberRowNumber (self):
        if not self.hasChamberRowNumber:
            self.ChamberRowNumber = Qt.QLabel (self)
    def initLState           (self):
        if not self.hasLState          :
            self.LState           = Qt.QLabel (self)
    def initCodeString       (self):
        if not self.hasCodeString      :
            self.CodeString       = Qt.QLabel (self)
    def initRState           (self):
        if not self.hasRState          :
            self.RState           = Qt.QLabel (self)
    def initChamberStatus(self):
        self.initModel()
        self.Model.LState = 0
        self.Model.RState = 0
        self.Model.CodeString = ''
        self.flushFrame()
    @property
    def hasModel               (self) -> bool:
        return hasattr(self, 'Model'            ) and isinstance(self.Model               , SlideModelInterface)

    @property
    def hasChamberRowNumber    (self) -> bool:
        return hasattr(self, 'ChamberRowNumber' ) and isinstance(self.ChamberRowNumber    , Qt.QLabel)

    @property
    def hasLState              (self) -> bool:
        return hasattr(self, 'LState'           ) and isinstance(self.LState              , Qt.QLabel)

    @property
    def hasCodeString          (self) -> bool:
        return hasattr(self, 'CodeString'       ) and isinstance(self.CodeString          , Qt.QLabel)

    @property
    def hasRState              (self) -> bool:
        return hasattr(self, 'RState'           ) and isinstance(self.RState              , Qt.QLabel)
