#!D:/IDE/anaconda3/envs/Microscope_Scan python
# -*- coding: UTF-8 -*-
"""
@FileName：EventBusInterface.py
@Description：
@Author：XSH
@Time：2025/11/15 11:35
@Department：研发部
@Copyright：©2025 乐辰生物
"""
from PyQt5                                                  import Qt
from abc                                                    import abstractmethod
from AppInterfaces.utils                                    import *
from AppCore.Levels                                         import *
from AppCore.Requests                                       import *
from AppUtils import AppUtils
from AppInterfaces.ThreadPoolInterface                      import ThreadPoolInterface, PoolUtil    #
from AppInterfaces.AppEventInterfaces.AppEventInterface     import AppEventInterface

class EventBusInterface(AppEventInterface):
    UiEvent                         : AppEventInterface  #
    WebEvent                        : AppEventInterface  #
    Focusing                        : AppEventInterface  #
    Scanning                        : AppEventInterface  #
    CameraScanning                  : AppEventInterface  #
    CameraFocusing                  : AppEventInterface  #
    WebServerEvent                  : AppEventInterface  #
    CellFocusing                    : AppEventInterface  #

    def SlotApplicationSignal(self, info: object, value: object):
        try:
            self.StopWork()
        except Exception as e:
            AppUtils.print_Exception    (e)

    @property
    def isAvailable(self) -> bool:
        return self.hasUiEvent         and self.hasWebEvent        and self.hasFocusing        and self.hasScanning        and self.hasCameraScanning  and self.hasCameraFocusing  and self.hasWebServerEvent  and self.hasCellFocusing

    def SendEvent(self, value, **args):
        pass

    def WaitDone(self, item, *args, **kwargs):
        pass


    def StartWork(self):
        self.initPools           ()
        self.startUiEvent        ()
        self.startWebEvent       ()
        self.startFocusing       ()
        self.startScanning       ()
        self.startCameraScanning ()
        self.startCameraFocusing ()
        self.startWebServerEvent ()
        self.startCellFocusing   ()


    def StopWork(self):
        self.stopUiEvent        ()
        self.stopWebEvent       ()
        self.stopFocusing       ()
        self.stopScanning       ()
        self.stopCameraScanning ()
        self.stopCameraFocusing ()
        self.stopWebServerEvent ()
        self.stopCellFocusing   ()
        self.delePools()


    def ResetWork(self):
        self.StopWork   ()
        self.StartWork  ()



    def appendItem(self, item, *args, **kwargs):
        pass

    def ReSet(self, **args):
        self.delePools  ()
        self.Start      ()


    def initPools(self):
        self.initUiEvent        ()
        self.initWebEvent       ()
        self.initFocusing       ()
        self.initScanning       ()
        self.initCameraScanning ()
        self.initCameraFocusing ()
        self.initWebServerEvent ()
        self.initCellFocusing   ()


    def delePools(self):
        self.deleUiEvent        ()
        self.deleWebEvent       ()
        self.deleFocusing       ()
        self.deleScanning       ()
        self.deleCameraScanning ()
        self.deleCameraFocusing ()
        self.deleWebServerEvent ()
        self.deleCellFocusing   ()

    def Start(self):
        self.initPools           ()
        self.startUiEvent        ()
        self.startWebEvent       ()
        self.startFocusing       ()
        self.startScanning       ()
        self.startCameraScanning ()
        self.startCameraFocusing ()
        self.startWebServerEvent ()
        self.startCellFocusing   ()

    def Stop(self):
        self.stopUiEvent        ()
        self.stopWebEvent       ()
        self.stopFocusing       ()
        self.stopScanning       ()
        self.stopCameraScanning ()
        self.stopCameraFocusing ()
        self.stopWebServerEvent ()
        self.stopCellFocusing   ()
        self.delePools()
    def WebSocketTransmitStartScanning(self):
        from AppSettings.AppStatusSetting import AppStatusSetting, AppStatus
        request: Request = Request.APIInstance(WebAPILevel.startScan)
        request.data = AppStatus.result_start_scanningstatus()
        self.initWebEvent()
        if self.hasWebEvent:
            self.webevent.SendEvent(request)


    def WebSocketTransmitFinishScanning(self):
        from AppSettings.AppStatusSetting import AppStatusSetting, AppStatus
        request: Request = Request.APIInstance(WebAPILevel.scanComplete)
        request.data = AppStatus.result_finish_scanningstatus()
        self.initWebEvent()
        if self.hasWebEvent:
            self.webevent.SendEvent(request)


    def WebSocketPushScanningStatus(self):
        from AppSettings.AppStatusSetting import AppStatusSetting, AppStatus
        request: Request = Request.APIInstance(WebAPILevel.getScanningStatus)
        request.data = AppStatus.result_start_scanningstatus()
        self.initWebEvent()
        if self.hasWebEvent:
            self.webevent.SendEvent(request)


    def WebSocketPushScanningData(self):
        from AppSettings.AppStatusSetting import AppStatusSetting, AppStatus
        request: Request = Request.APIInstance(WebAPILevel.getScanningData)
        request.data = AppStatus.scanningstatus()
        pushtemplate: Request = Request.APIInstance(WebAPILevel.pushTemplate)
        pushtemplate.data = AppStatus.pushtemplate()
        self.initWebEvent()
        if self.hasWebEvent:
            self.webevent.SendEvent(pushtemplate)
            AppTimeLoop.processGlobalEvents(1000)
            self.webevent.SendEvent(request)

    def WebSocketTransmitExit(self):
        request: Request = Request.APIInstance(WebAPILevel.stopScan)
        request.data = {'RunStatus': RunStatusLevel.ExitRunning}
        self.initWebEvent()
        self.initWebEvent()
        if self.hasWebEvent:
            self.webevent.SendEvent(request)

    def WebSocketPushJumpScanningData(self):
        from AppSettings.AppStatusSetting import AppStatusSetting, AppStatus
        request: Request = Request.APIInstance(WebAPILevel.jumpScanning)
        request.data = {'JumpScanning': AppStatus.JumpScanning}
        self.initWebEvent()
        if self.hasWebEvent:
            self.webevent.SendEvent(request)



    def WebSocketPushExitScanningData(self):
        from AppSettings.AppStatusSetting import AppStatusSetting, AppStatus
        request: Request = Request.APIInstance(WebAPILevel.exitScanning)
        request.data = {'exitScanning': AppStatus.isExit}
        self.initWebEvent()
        if self.hasWebEvent:
            self.webevent.SendEvent(request)


    def WebSocketTransmitChamberStatus(self):
        from AppSettings.AppStatusSetting import AppStatusSetting, AppStatus
        request: Request = Request.APIInstance(WebAPILevel.getChamberStatus)
        request.data = AppStatus.result_chamber_status()
        self.initWebEvent()
        if self.hasWebEvent:
            self.webevent.SendEvent(request)





    @abstractmethod
    def initUiEvent                         (self):
        pass

    @abstractmethod
    def initWebEvent                        (self):
        pass

    @abstractmethod
    def initFocusing                        (self):
        pass

    @abstractmethod
    def initScanning                        (self):
        pass

    @abstractmethod
    def initCameraScanning                  (self):
        pass

    @abstractmethod
    def initCameraFocusing                  (self):
        pass

    @abstractmethod
    def initWebServerEvent                  (self):
        pass

    @abstractmethod
    def initCellFocusing                    (self):
        pass

    def startUiEvent                         (self):
        if not self.hasUiEvent                         :
            self.initUiEvent                         ()
        if self.hasUiEvent                         :
            self.UiEvent                         .Start()

    def startWebEvent                        (self):
        if not self.hasWebEvent                        :
            self.initWebEvent                        ()
        if self.hasWebEvent                        :
            self.WebEvent                        .Start()

    def startFocusing                        (self):
        if not self.hasFocusing                        :
            self.initFocusing                        ()
        if self.hasFocusing                        :
            self.Focusing                        .Start()

    def startScanning                        (self):
        if not self.hasScanning                        :
            self.initScanning                        ()
        if self.hasScanning                        :
            self.Scanning                        .Start()

    def startCameraScanning                  (self):
        if not self.hasCameraScanning                  :
            self.initCameraScanning                  ()
        if self.hasCameraScanning                  :
            self.CameraScanning                  .Start()

    def startCameraFocusing                  (self):
        if not self.hasCameraFocusing                  :
            self.initCameraFocusing                  ()
        if self.hasCameraFocusing                  :
            self.CameraFocusing                  .Start()

    def startWebServerEvent                  (self):
        if not self.hasWebServerEvent                  :
            self.initWebServerEvent                  ()
        if self.hasWebServerEvent                  :
            self.WebServerEvent                  .Start()

    def startCellFocusing                    (self):
        if not self.hasCellFocusing                    :
            self.initCellFocusing                    ()
        if self.hasCellFocusing                    :
            self.CellFocusing                    .Start()

    def stopUiEvent                         (self):
        if self.hasUiEvent                         :
            self.UiEvent                         .Stop()

    def stopWebEvent                        (self):
        if self.hasWebEvent                        :
            self.WebEvent                        .Stop()

    def stopFocusing                        (self):
        if self.hasFocusing                        :
            self.Focusing                        .Stop()

    def stopScanning                        (self):
        if self.hasScanning                        :
            self.Scanning                        .Stop()

    def stopCameraScanning                  (self):
        if self.hasCameraScanning                  :
            self.CameraScanning                  .Stop()

    def stopCameraFocusing                  (self):
        if self.hasCameraFocusing                  :
            self.CameraFocusing                  .Stop()

    def stopWebServerEvent                  (self):
        if self.hasWebServerEvent                  :
            self.WebServerEvent                  .Stop()

    def stopCellFocusing                    (self):
        if self.hasCellFocusing                    :
            self.CellFocusing                    .Stop()

    def deleUiEvent                         (self):
        try:
            if self.hasUiEvent                         :
                self.UiEvent                         .Stop()
        except Exception as e:
            AppUtils.print_Exception(e)
        self.UiEvent                          = None

    def deleWebEvent                        (self):
        try:
            if self.hasWebEvent                        :
                self.WebEvent                        .Stop()
        except Exception as e:
            AppUtils.print_Exception(e)
        self.WebEvent                         = None

    def deleFocusing                        (self):
        try:
            if self.hasFocusing                        :
                self.Focusing                        .Stop()
        except Exception as e:
            AppUtils.print_Exception(e)
        self.Focusing                         = None

    def deleScanning                        (self):
        try:
            if self.hasScanning                        :
                self.Scanning                        .Stop()
        except Exception as e:
            AppUtils.print_Exception(e)
        self.Scanning                         = None

    def deleCameraScanning                  (self):
        try:
            if self.hasCameraScanning                  :
                self.CameraScanning                  .Stop()
        except Exception as e:
            AppUtils.print_Exception(e)
        self.CameraScanning                   = None

    def deleCameraFocusing                  (self):
        try:
            if self.hasCameraFocusing                  :
                self.CameraFocusing                  .Stop()
        except Exception as e:
            AppUtils.print_Exception(e)
        self.CameraFocusing                   = None

    def deleWebServerEvent                  (self):
        try:
            if self.hasWebServerEvent                  :
                self.WebServerEvent                  .Stop()
        except Exception as e:
            AppUtils.print_Exception(e)
        self.WebServerEvent                   = None

    def deleCellFocusing                    (self):
        try:
            if self.hasCellFocusing                    :
                self.CellFocusing                    .Stop()
        except Exception as e:
            AppUtils.print_Exception(e)
        self.CellFocusing                     = None

    @property
    def uievent               (self) -> AppEventInterface:
        return None if not self.hasUiEvent         else self.UiEvent                   #

    @property
    def webevent              (self) -> AppEventInterface:
        return None if not self.hasWebEvent        else self.WebEvent                  #

    @property
    def focusingevent         (self) -> AppEventInterface:
        return None if not self.hasFocusing        else self.Focusing                  #

    @property
    def scanningevent         (self) -> AppEventInterface:
        return None if not self.hasScanning        else self.Scanning                  #

    @property
    def camerascanningevent   (self) -> AppEventInterface:
        return None if not self.hasCameraScanning  else self.CameraScanning            #

    @property
    def camerafocusingevent   (self) -> AppEventInterface:
        return None if not self.hasCameraFocusing  else self.CameraFocusing            #

    @property
    def webserverevent        (self) -> AppEventInterface:
        return None if not self.hasWebServerEvent  else self.WebServerEvent            #

    @property
    def cellfocusingevent     (self) -> AppEventInterface:
        return None if not self.hasCellFocusing    else self.CellFocusing              #










    @property
    def hasUiEvent        (self) -> bool:
        return hasattr(self, 'UiEvent'       ) and isinstance(self.UiEvent       , AppEventInterface)

    @property
    def hasWebEvent       (self) -> bool:
        return hasattr(self, 'WebEvent'      ) and isinstance(self.WebEvent      , AppEventInterface)

    @property
    def hasFocusing       (self) -> bool:
        return hasattr(self, 'Focusing'      ) and isinstance(self.Focusing      , AppEventInterface)

    @property
    def hasScanning       (self) -> bool:
        return hasattr(self, 'Scanning'      ) and isinstance(self.Scanning      , AppEventInterface)

    @property
    def hasCameraScanning (self) -> bool:
        return hasattr(self, 'CameraScanning') and isinstance(self.CameraScanning, AppEventInterface)

    @property
    def hasCameraFocusing (self) -> bool:
        return hasattr(self, 'CameraFocusing') and isinstance(self.CameraFocusing, AppEventInterface)

    @property
    def hasWebServerEvent (self) -> bool:
        return hasattr(self, 'WebServerEvent') and isinstance(self.WebServerEvent, AppEventInterface)

    @property
    def hasCellFocusing   (self) -> bool:
        return hasattr(self, 'CellFocusing'  ) and isinstance(self.CellFocusing  , AppEventInterface)




