#!D:/IDE/anaconda3/envs/Microscope_Scan python
# -*- coding: UTF-8 -*-
"""
@FileName：DetectionImplInterface.py
@Description：
@Author：XSH
@Time：2025/11/11 12:53
@Department：研发部
@Copyright：©2025 乐辰生物
"""
from PyQt5 import Qt
from abc import abstractmethod
from AppInterfaces.ImplInterface import ImplInterface
from AppContexts.Contexts import DetectionContext, AxisRegisterContext, ModBusRegister, AxisContext
from AppInterfaces.AppImplInterfaces.ModBusPoolInterface import ModBusPoolInterface, ModBusImplInterface
from AppCore.AppLevelModels.ManipulatorLevel import AxisLevel

class DetectionImplInterface(ImplInterface):
    Manager     : ModBusPoolInterface       #
    Parameter   : DetectionContext          #
    Level       : int                       #

    @property
    def parameter(self) -> DetectionContext:
        return self.Parameter if self.hasParameter else None

    def setParameter(self, value):
        self.Parameter = value if isinstance(value, DetectionContext) else None

    def setManager(self, manager: ModBusPoolInterface):
        self.Manager = manager

    @property
    def manager(self) -> ModBusPoolInterface:
        return self.Manager if self.hasManager else None

    @property
    def hasManager(self) -> bool:
        return hasattr(self, 'Manager') and self.Manager is not None and isinstance(self.Manager, ModBusPoolInterface)

    @property
    def hasParameter(self) -> bool:
        return hasattr(self, 'Parameter') and isinstance(self.Parameter, DetectionContext)

    @property
    def level(self) -> int:
        return self.Level if self.hasLevel else AxisLevel.Detection

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

    @property
    def isValid(self) -> bool:
        return self.hasParameter and self.hasManager and self.hasLevel

    @property
    def IsOpen(self) -> bool:
        if self.isValid:
            handler: ModBusImplInterface = self.readhandler
            return isinstance(handler, ModBusImplInterface) and handler.isAvailable
        return False

    @property
    def isDisable(self) -> bool:
        return not self.isValid or not self.IsOpen

    @property
    def isAvailable(self) -> bool:
        return self.IsOpen

    @abstractmethod
    def setLevel(self, level: int, **args):
        pass

    def Open(self, **args) -> int:
        return RECODE_SUCCEED if self.IsOpen else RECODE_FAILURE

    def Close(self, **args) -> int:
        return RECODE_SUCCEED if not self.IsOpen else RECODE_FAILURE

    def initManager(self, **args):
        pass

    def deleManager(self):
        pass

    @abstractmethod
    def initContext(self):
        raise Exception('Interface Instance error')

    @property
    @abstractmethod
    def handler(self) -> ModBusImplInterface:
        raise Exception('Interface Instance error')

    @property
    @abstractmethod
    def readhandler(self) -> ModBusImplInterface:
        raise Exception('Interface Instance error')

    @property
    @abstractmethod
    def detectorhandler(self) -> ModBusImplInterface:
        raise Exception('Interface Instance error')

    @property
    @abstractmethod
    def slow_handler(self) -> ModBusImplInterface:
        raise Exception('Interface Instance error')

    @property
    @abstractmethod
    def scan_handler(self) -> ModBusImplInterface:
        raise Exception('Interface Instance error')

    @property
    @abstractmethod
    def drop_handler(self) -> ModBusImplInterface:
        raise Exception('Interface Instance error')

    @property
    @abstractmethod
    def hook_handler(self) -> ModBusImplInterface:
        raise Exception('Interface Instance error')

    @property
    @abstractmethod
    def new_handler(self) -> ModBusImplInterface:
        raise Exception('Interface Instance error')
    @abstractmethod
    def oil_switch(self, enable: (bool, int)) -> int:
        """打开油泵"""
        raise Exception('Interface Instance error')

    @abstractmethod
    def set_oil_speed(self, speed: int) -> int:
        """设置油泵速度"""
        raise Exception('Interface Instance error')

    @abstractmethod
    def get_oil_speed(self) -> (int, int):
        """获取油泵速度"""
        raise Exception('Interface Instance error')

    @abstractmethod
    def get_oil_status(self) ->  bool:
        """获取油管状态"""
        raise Exception('Interface Instance error')

    @abstractmethod
    def get_oil_switch(self) -> bool:
        """获取油泵状态"""
        raise Exception('Interface Instance error')

    @abstractmethod
    def get_door_status(self) -> bool:
        raise Exception('Interface Instance error')

    @abstractmethod
    def is_close_the_door(self) -> bool:
        raise Exception('Interface Instance error')

    @abstractmethod
    def get_chamber_status(self) -> bool:
        """获取片仓状态"""
        raise Exception('Interface Instance error')


    @abstractmethod
    def buzzer_switch(self, enable: (int, bool)) -> int:
        """打开蜂鸣器"""
        raise Exception('Interface Instance error')

    @abstractmethod
    def get_buzzer_switch(self) -> int:
        """打开蜂鸣器"""
        raise Exception('Interface Instance error')


