#!D:/IDE/anaconda3/envs/Microscope_Scan python
# -*- coding: UTF-8 -*-
"""
@FileName：StatusOperation.py
@Description：
@Author：XSH
@Time：2025/12/20 16:21
@Department：研发部
@Copyright：©2025 乐辰生物
"""
from abc                        import abstractmethod                       #
from PyQt5                      import Qt                                   #
from AppInterfaces.UiInterface  import UiInterface                          #
from AppInterfaces.globalSlot   import globalSlot, AppSlot                  #
from AppCore.Contexts           import *                                    #
from AppCore.DBModels           import *                                    #
from AppCore.Levels             import *                                    #
from AppUtils.utils             import *                                    #
from AppSettings.Settings       import *                                    #
from AppInterfaces.Interfaces   import ModBusImplInterface                  #
from AppUtils.HardWareUnit import HardWareUnit

class StatusOperation(Qt.QWidget, UiInterface):
    Manager             : ModBusImplInterface   #
    Detection           : DetectionContext      #
    HookParameter       : HookContext           #
    InfoOilPump         : Qt.QPushButton        #   '油泵开关　'
    EditOilPump         : Qt.QLabel             #   '油泵开关　'
    InfoYoutube         : Qt.QPushButton        #   '油管状态　'
    EditYoutube         : Qt.QLabel             #   '油管状态　'
    InfoHookFrontLimit  : Qt.QPushButton        #   '挂钩前限位'
    EditHookFrontLimit  : Qt.QLabel             #   '挂钩前限位'
    InfoHookBackLimit   : Qt.QPushButton        #   '挂钩后限位'
    EditHookBackLimit   : Qt.QLabel             #   '挂钩后限位'
    InfoHookTrayLimit   : Qt.QPushButton        #   '载玻台托盘'
    EditHookTrayLimit   : Qt.QLabel             #   '载玻台托盘'
    StatusGroup         : Qt.QGroupBox          #   ''
    StatusTimer         : Qt.QTimer             #
    def __init__(self, parent: Qt.QWidget = None):
        super(StatusOperation, self).__init__(parent)
        self.Initialization("StatusOperation", self, parent)


    def initValue(self):
        super(StatusOperation, self).initValue()
        self.initStatusGroup         ()
        self.initInfoOilPump         ()
        self.initEditOilPump         ()
        self.initInfoYoutube         ()
        self.initEditYoutube         ()
        self.initInfoHookFrontLimit  ()
        self.initEditHookFrontLimit  ()
        self.initInfoHookBackLimit   ()
        self.initEditHookBackLimit   ()
        self.initInfoHookTrayLimit   ()
        self.initEditHookTrayLimit   ()



    def initProperty(self):
        super(StatusOperation, self).initProperty()
        self.InfoOilPump         .setText('油泵开关　')
        self.InfoYoutube         .setText('油管状态　')
        self.InfoHookTrayLimit   .setText('载玻台托盘')
        self.InfoHookFrontLimit  .setText('挂钩前限位')
        self.InfoHookBackLimit   .setText('挂钩后限位')

        self.InfoOilPump         .setFlat(True)
        self.InfoYoutube         .setFlat(True)
        self.InfoHookTrayLimit   .setFlat(True)
        self.InfoHookFrontLimit  .setFlat(True)
        self.InfoHookBackLimit   .setFlat(True)

        self.InfoOilPump         .setFixedHeight(35)
        self.InfoYoutube         .setFixedHeight(35)
        self.InfoHookFrontLimit  .setFixedHeight(35)
        self.InfoHookBackLimit   .setFixedHeight(35)
        self.InfoHookTrayLimit   .setFixedHeight(35)

        self.EditOilPump         .setFixedHeight(35)
        self.EditYoutube         .setFixedHeight(35)
        self.EditHookFrontLimit  .setFixedHeight(35)
        self.EditHookBackLimit   .setFixedHeight(35)
        self.EditHookTrayLimit   .setFixedHeight(35)

        self.EditOilPump         .setFixedWidth(35)
        self.EditYoutube         .setFixedWidth(35)
        self.EditHookFrontLimit  .setFixedWidth(35)
        self.EditHookBackLimit   .setFixedWidth(35)
        self.EditHookTrayLimit   .setFixedWidth(35)

        self.changedStatus(self.EditOilPump         , False)
        self.changedStatus(self.EditYoutube         , False)
        self.changedStatus(self.EditHookFrontLimit  , False)
        self.changedStatus(self.EditHookBackLimit   , False)
        self.changedStatus(self.EditHookTrayLimit   , False)

    def changedStatus(self, status: Qt.QLabel, enable: bool):
        if not status:
            return
        setattr(status, 'Status', status)
        UiUtils.setColorStyle(status, enable)

    def setOilPumpStatus          (self, enable: (bool, int)):
        status = True if isinstance(enable, (bool, int)) and enable else False
        setattr(self.EditOilPump         , 'Status', status)
        UiUtils.setColorStyle(self.EditOilPump         , status)

    def setYoutubeStatus          (self, enable: (bool, int)):
        status = True if isinstance(enable, (bool, int)) and enable else False
        setattr(self.EditYoutube         , 'Status', status)
        UiUtils.setColorStyle(self.EditYoutube         , status)

    def setHookFrontLimitStatus   (self, enable: (bool, int)):
        status = True if isinstance(enable, (bool, int)) and enable else False
        setattr(self.EditHookTrayLimit   , 'Status', status)
        UiUtils.setColorStyle(self.EditHookTrayLimit   , status)

    def setHookBackLimitStatus    (self, enable: (bool, int)):
        status = True if isinstance(enable, (bool, int)) and enable else False
        setattr(self.EditHookFrontLimit  , 'Status', status)
        UiUtils.setColorStyle(self.EditHookFrontLimit  , status)

    def setHookTrayLimitStatus    (self, enable: (bool, int)):
        status = True if isinstance(enable, (bool, int)) and enable else False
        setattr(self.EditHookBackLimit   , 'Status', status)
        UiUtils.setColorStyle(self.EditHookBackLimit   , status)


    def initLayouts(self):
        if self.hasWindowLayouts:
            return
        main_layout: Qt.QVBoxLayout = Qt.QVBoxLayout()
        main_layout.setSpacing(1)
        main_layout.setContentsMargins(1, 1, 1, 1)
        # main_layout.setAlignment(Qt.Qt.AlignCenter)

        group_layout: Qt.QGridLayout = Qt.QGridLayout()
        group_layout.setSpacing(1)
        group_layout.setContentsMargins(5, 5, 5, 5)

        group_layout.addWidget(self.InfoOilPump         , 0, 0, 1, 2)
        group_layout.addWidget(self.EditOilPump         , 0, 2, 1, 1)
        group_layout.addWidget(self.InfoYoutube         , 1, 0, 1, 2)
        group_layout.addWidget(self.EditYoutube         , 1, 2, 1, 1)
        group_layout.addWidget(self.InfoHookTrayLimit   , 2, 0, 1, 2)
        group_layout.addWidget(self.EditHookTrayLimit   , 2, 2, 1, 1)
        group_layout.addWidget(self.InfoHookFrontLimit  , 3, 0, 1, 2)
        group_layout.addWidget(self.EditHookFrontLimit  , 3, 2, 1, 1)
        group_layout.addWidget(self.InfoHookBackLimit   , 4, 0, 1, 2)
        group_layout.addWidget(self.EditHookBackLimit   , 4, 2, 1, 1)

        self.StatusGroup.setLayout(group_layout)

        main_layout.addWidget(self.StatusGroup)

        self.setLayout(main_layout)


    # def show(self) -> None:
    #     self.StatusGroup.show()
    #     return super(StatusOperation, self).show()
    def initInfoOilPump         (self):
        if not self.hasInfoOilPump         :
            self.InfoOilPump          = Qt.QPushButton  (self)

    def initEditOilPump         (self):
        if not self.hasEditOilPump         :
            self.EditOilPump          = Qt.QLabel       (self)

    def initInfoYoutube         (self):
        if not self.hasInfoYoutube         :
            self.InfoYoutube          = Qt.QPushButton  (self)

    def initEditYoutube         (self):
        if not self.hasEditYoutube         :
            self.EditYoutube          = Qt.QLabel       (self)

    def initInfoHookFrontLimit  (self):
        if not self.hasInfoHookFrontLimit  :
            self.InfoHookFrontLimit   = Qt.QPushButton  (self)

    def initEditHookFrontLimit  (self):
        if not self.hasEditHookFrontLimit  :
            self.EditHookFrontLimit   = Qt.QLabel       (self)

    def initInfoHookBackLimit   (self):
        if not self.hasInfoHookBackLimit   :
            self.InfoHookBackLimit    = Qt.QPushButton  (self)

    def initEditHookBackLimit   (self):
        if not self.hasEditHookBackLimit   :
            self.EditHookBackLimit    = Qt.QLabel       (self)

    def initInfoHookTrayLimit   (self):
        if not self.hasInfoHookTrayLimit   :
            self.InfoHookTrayLimit    = Qt.QPushButton  (self)

    def initEditHookTrayLimit   (self):
        if not self.hasEditHookTrayLimit   :
            self.EditHookTrayLimit    = Qt.QLabel       (self)

    def initStatusGroup         (self):
        if not self.hasStatusGroup         :
            self.StatusGroup          = Qt.QGroupBox ()














    @property
    def hasInfoOilPump         (self) -> bool:
        return self.has_field('InfoOilPump'        , Qt.QPushButton)

    @property
    def hasEditOilPump         (self) -> bool:
        return self.has_field('EditOilPump'        , Qt.QLabel     )

    @property
    def hasInfoYoutube         (self) -> bool:
        return self.has_field('InfoYoutube'        , Qt.QPushButton)

    @property
    def hasEditYoutube         (self) -> bool:
        return self.has_field('EditYoutube'        , Qt.QLabel     )

    @property
    def hasInfoHookFrontLimit  (self) -> bool:
        return self.has_field('InfoHookFrontLimit' , Qt.QPushButton)

    @property
    def hasEditHookFrontLimit  (self) -> bool:
        return self.has_field('EditHookFrontLimit' , Qt.QLabel     )

    @property
    def hasInfoHookBackLimit   (self) -> bool:
        return self.has_field('InfoHookBackLimit'  , Qt.QPushButton)

    @property
    def hasEditHookBackLimit   (self) -> bool:
        return self.has_field('EditHookBackLimit'  , Qt.QLabel     )

    @property
    def hasInfoHookTrayLimit   (self) -> bool:
        return self.has_field('InfoHookTrayLimit'  , Qt.QPushButton)

    @property
    def hasEditHookTrayLimit   (self) -> bool:
        return self.has_field('EditHookTrayLimit'  , Qt.QLabel     )

    @property
    def hasStatusGroup         (self) -> bool:
        return self.has_field('StatusGroup'        , Qt.QGroupBox  )

    @property
    def hasDetection     (self) -> bool:
        return self.has_field('Detection'       , DetectionContext  )
    @property
    def hasHookParameter (self) -> bool:
        return self.has_field('HookParameter'   , HookContext       )

    @property
    def hasManager      (self) -> bool:
        return self.has_field('Manager'         ,     ModBusImplInterface)


    def initManager(self):
        if not self.hasManager:
            from AppCore.Impls import ModBusImpl
            from AppSettings.AppSetting import AppConfig
            context: ModBusContext = ModBusContext()
            context.initParameter(**AppConfig.Manipulator.ModBus.serialize())
            self.Manager = ModBusImpl(context)
        if not self.Manager.IsOpen:
            self.Manager.ReOpen()
    def deleManager(self):
        if self.hasManager:
            try:
                self.Manager.Close()
                self.Manager.deleManager()
                del self.Manager
            except Exception as e:
                AppUtils.print_Exception(e)
        self.Manager = None

    def initDetection(self):
        if not self.hasDetection:
            from AppSettings.AppSetting import AppConfig
            self.Detection = DetectionContext()
            self.Detection.initParameter(**AppConfig.Manipulator.Detection.serialize())

    def initHookParameter(self):
        if not self.hasHookParameter:
            from AppSettings.AppSetting import AppConfig
            self.HookParameter = HookContext()
            self.HookParameter.initParameter(**AppConfig.Manipulator.AxisHConfig.serialize())

    def initStatusTimer(self):
        if not self.hasStatusTimer:
            TYPE: Qt.Qt.ConnectionType = Qt.Qt.ConnectionType(Qt.Qt.AutoConnection | Qt.Qt.UniqueConnection)
            self.StatusTimer = Qt.QTimer()
            self.StatusTimer.timeout.connect(self.flushStatus, TYPE)
            self.StatusTimer.setInterval(2000)

    @property
    def hasStatusTimer(self):
        return self.has_field('StatusTimer'       , Qt.QTimer      )

    @property
    def statustimer(self) -> Qt.QTimer:
        self.initStatusTimer()
        return self.StatusTimer

    def start(self):
        self.initManager        ()
        self.initDetection      ()
        self.initHookParameter  ()
        self.initStatusTimer    ()
        self.statustimer.start  ()

    def stop(self):
        if not self.hasStatusTimer:
            return
        self.statustimer.stop()
        self.deleManager()

    def flushStatus(self):
        self.initManager        ()
        self.initDetection      ()
        self.initHookParameter  ()
        OilPump         : int = HardWareUnit.ReadBool(self.Manager, self.Detection.OilPumpRegister      )
        Youtube         : int = HardWareUnit.ReadBool(self.Manager, self.Detection.OilStatusRegister    )
        HookTrayLimit   : int = HardWareUnit.ReadBool(self.Manager, self.HookParameter.FrontLimit       )
        HookFrontLimit  : int = HardWareUnit.ReadBool(self.Manager, self.HookParameter.BackLimit        )
        HookBackLimit   : int = HardWareUnit.ReadBool(self.Manager, self.HookParameter.DetectorRegister )

        setattr(self.EditOilPump         , 'Status', OilPump         )
        setattr(self.EditYoutube         , 'Status', Youtube         )
        setattr(self.EditHookTrayLimit   , 'Status', HookTrayLimit   )
        setattr(self.EditHookFrontLimit  , 'Status', HookFrontLimit  )
        setattr(self.EditHookBackLimit   , 'Status', HookBackLimit   )
        UiUtils.setColorStyle(self.EditOilPump         , OilPump         )
        UiUtils.setColorStyle(self.EditYoutube         , Youtube         )
        UiUtils.setColorStyle(self.EditHookTrayLimit   , HookTrayLimit   )
        UiUtils.setColorStyle(self.EditHookFrontLimit  , HookFrontLimit  )
        UiUtils.setColorStyle(self.EditHookBackLimit   , HookBackLimit   )




