#!D:\IDE\Anaconda3\envs\Microscope_Scan python
# -*- coding: UTF-8 -*-
"""
@FileName   : SlideTablePickBusiness.py
@Account    : Administrator
@Time       : 2025/9/3 10:22
@Author     : xsh
@Description: 
"""
from PyQt5                                  import Qt
from abc                                    import abstractmethod, abstractclassmethod, abstractstaticmethod
from AppInterfaces.utils                    import *
from AppSettings.utils                      import *
from AppCore.Levels                         import *
from AppCore.Managers                       import *
from AppCore.Impls                          import *
from AppCore.Requests                       import *
from AppCore.DBModels                       import *
from AppUtils.utils                         import *
from AppCore.Events                         import *
from AppBackEnd.AppEventBusBusiness         import *
from AppCore.Readers                        import *
from AppSettings.AppGlobalStatus            import AppGlobalStatus, AppStatus
from AppSettings.AppGlobalAlgorithm         import AppGlobalAlgorithm
from AppCore.AppAlgorithms.ImageAlgorithm   import ImageAlgorithm
from AppBackEnd.AppBusinessController       import AppBusinessController, AppBusinessInterface
class SlideTablePickBusiness(Qt.QObject, AppBusinessController, SingletonInterface):
    Locker      : Qt.QMutex = Qt.QMutex()
    @classmethod
    def GetInstance(cls, *args, **kwargs) -> 'SlideTablePickBusiness':
        return super(SlideTablePickBusiness, cls).GetInstance(*args, **kwargs)
    def __init__(self, parent: AppBusinessInterface = None):
        super(SlideTablePickBusiness, self).__init__(parent=parent if parent is not None else parent.m_Control)
        self.initialize()
        self.ScanLevel = ScanStatusLevel.OilDripping_Status
        self.initModules(parent)

    def initModules(self, parent: AppBusinessInterface):
        if isinstance(parent, AppBusinessInterface):
            self.Config                             = parent.config                       #
            self.SystemConfig                       = parent.system                       #
            self.HardWare                           = parent.devices                      #
            self.GlobalStatus                       = parent.status                       #
            self.Application                        = parent.application                  #
            self.Algorithm                          = parent.algorithm                    #
            self.EventBus                           = parent.eventBus                     #
            self.Process                            = parent.process                      #
            self.HitBotConfig                       = parent.hitbotConfig                 #

    def isValid(self) -> bool:
        return self.hasHitBot and self.hasGlobalStatus and self.hasSystemConfig and self.hasConfig

    @property
    def isAvailable(self) -> bool:
        return self.hasHitBot and self.hitbot.isAvailable

    def Done(self, ChamberNumber: int, *args, **kwargs):
        isWrong: int = RECODE_FAILURE
        isSafe      : bool  = AppConvert.ObjectConvert(kwargs.get('isSafe'      , True), bool   )
        RowNumber   : int   = AppConvert.ObjectConvert(kwargs.get('RowNumber'   , None), int    )
        TrayNumber  : int   = AppConvert.ObjectConvert(kwargs.get('TrayNumber'  , None), int    )
        isExit      : bool  = AppConvert.ObjectConvert(kwargs.get('isExit'      , True), bool   )
        try:
            if isinstance(ChamberNumber, int) and isinstance(RowNumber, int) and isinstance(TrayNumber, int):
                isWrong = self.SlideTableSamplePicking(ChamberNumber, RowNumber, TrayNumber, isSafe, isExit)

        except Exception as e:
            AppUtils.print_Exception(e)
        if self.status.isExit:
            self.status.setScanningStatus(ScanStatusLevel.FinishScanning_Status)
        # elif self.status.isJumpScanning():
        #     self.status.setScanningStatus(ScanStatusLevel.JumpScanning_Status)
        elif not ScanStatusLevel.isHitBotWarning(self.status.scanning_level):
            self.status.changedChamberScanningStatus(ChamberNumber, RowNumber, ScanStatusLevel.PickTraySlide_Warning_Status)
            self.status.setScanningStatus(ScanStatusLevel.PickTraySlide_Warning_Status)
        return

    def WaitDone(self, ChamberNumber: int, *args, **kwargs) -> int:
        isWrong: int = RECODE_FAILURE
        isSafe      : bool  = AppConvert.ObjectConvert(kwargs.get('isSafe'      , True), bool   )
        RowNumber   : int   = AppConvert.ObjectConvert(kwargs.get('RowNumber'   , None), int    )
        TrayNumber  : int   = AppConvert.ObjectConvert(kwargs.get('TrayNumber'  , None), int    )
        isExit      : bool  = AppConvert.ObjectConvert(kwargs.get('isExit'      , True), bool   )
        try:
            if isinstance(ChamberNumber, int) and isinstance(RowNumber, int) and isinstance(TrayNumber, int):
                isWrong = self.SlideTableSamplePicking(ChamberNumber, RowNumber, TrayNumber, isSafe, isExit)
        except Exception as e:
            AppUtils.print_Exception(e)
        if self.status.isExit:
            self.status.setScanningStatus(ScanStatusLevel.FinishScanning_Status)
        # elif self.status.isJumpScanning():
        #     self.status.setScanningStatus(ScanStatusLevel.JumpScanning_Status)
        # elif RECODE_SUCCEED == isWrong:
        #     self.status.setScanningStatus(self.ScanLevel)
        elif not ScanStatusLevel.isHitBotWarning(self.status.scanning_level):
            self.status.changedChamberScanningStatus(ChamberNumber, RowNumber, ScanStatusLevel.PickTraySlide_Warning_Status)
            self.status.setScanningStatus(ScanStatusLevel.PickTraySlide_Warning_Status)
        return isWrong



    def SlideTableSamplePicking(self, chamber_number: int, row_number: int, tray_number: int, isSafe: bool = True, isExit: bool = True) -> bool:

        isWrong                 : int                       = RECODE_FAILURE
        isWrong                 = self.readySlideTableSamplePicking(chamber_number, row_number, tray_number)
        self.status.setSlideTableStatus(chamber_number, row_number, tray_number)
        if RECODE_SUCCEED != isWrong:
            return isWrong
        if isSafe:
            self.hitbot.moveToSafePosition()
        """处理暂停 退出"""
        while self.status.waitForPauseRunning():
            AppTimeLoop.processEvents(self.system.WarningWaitTimes())
        if isExit:
            if self.status.waitForExitRunning():
                return RECODE_SUCCEED


        isWrong = self.waitForSlideTablePicking(chamber_number, row_number, tray_number)
        tray_status = False if RECODE_SUCCEED == isWrong and not ScanStatusLevel.isHitBotAlarm(self.status.scanning_level) else True
        self.status.setTrayStatus(tray_number, tray_status)


        if RECODE_SUCCEED == isWrong:
            """如果所有动作完成 把夹爪状态改为0 """
            self.hitbotConfig.setIsClamp(True)
        else:
            """如果所有动作完成 把夹爪状态改为0 """
            self.hitbotConfig.setIsClamp(False)
        self.status.setSlideTableStatus(chamber_number, row_number, tray_number)
        """如果机械臂错误 直接退出"""
        if ScanStatusLevel.isHitBotAlarm(self.status.scanning_level):
            return RECODE_FAILURE

        if isSafe and RECODE_SUCCEED == isWrong:
            self.hitbot.moveToSafePosition()
        """处理暂停 退出"""
        while self.status.waitForPauseRunning():
            AppTimeLoop.processEvents(self.system.WarningWaitTimes())
        if isExit:
            if self.status.waitForExitRunning():
                return RECODE_SUCCEED
        return isWrong

    def readySlideTableSamplePicking(self, chamber_number: int, row_number: int, tray_number: int) -> int:
        isWrong             : int                           = RECODE_FAILURE
        MinChamberNumber    : int
        MaxChamberNumber    : int
        MinRowNumber        : int
        MaxRowNumber        : int
        MinRowNumber    , MaxRowNumber       = self.system.getRowRange      ()
        MinChamberNumber, MaxChamberNumber   = self.system.getChamberRange  ()
        if MinChamberNumber <= chamber_number <= MaxChamberNumber and MinRowNumber <= row_number <= MaxRowNumber:
            try:
                """如果当前夹抓已经夹到玻片 退出"""
                if self.hitbotConfig.hitbotIsClamp():
                    if self.hitbot.IsGrab():
                        return RECODE_FAILURE
                    self.hitbot.SetGripperON()
                    self.hitbotConfig.setIsClamp(False)
                if not self.isValid():
                    return RECODE_FAILURE
                if not self.isAvailable:
                    return RECODE_FAILURE
                isWrong = RECODE_SUCCEED
            except Exception as e:
                AppUtils.print_Exception(e)
                isWrong = RECODE_FAILURE
        return isWrong

