#!D:/IDE/anaconda3/envs/Microscope_Scan python
# -*- coding: UTF-8 -*-
"""
@FileName：AxisImplInterface.py
@Description：
@Author：XSH
@Time：2025/11/11 12:52
@Department：研发部
@Copyright：©2025 乐辰生物
"""
from PyQt5 import Qt
from abc import abstractmethod
from AppSettings.Settings import *
from AppInterfaces.ImplInterface import ImplInterface
from AppContexts.Contexts import AxisContext, AxisRegisterContext, ModBusRegister
from AppInterfaces.AppImplInterfaces.ModBusPoolInterface import ModBusPoolInterface, ModBusImplInterface
from AppCore.AppLevelModels.ManipulatorLevel import AxisLevel
class AxisImplInterface(ImplInterface):
    Manager             : ModBusPoolInterface       #
    Parameter           : AxisContext               #
    Level               : int                       #
    is_IsInitialize     : bool                      #


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

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

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

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

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


    @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, AxisContext)

    @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 and self.isInitialize

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

    @abstractmethod
    def reload_velocity(self):
        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')

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

    @abstractmethod
    def hasAxisHome(self) -> int:
        raise Exception('Interface Instance error')

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

    @abstractmethod
    def move(self, position: float) -> int:
        raise Exception('Interface Instance error')

    @abstractmethod
    def rel_move(self, value: (int, float)) -> int:
        raise Exception('Interface Instance error')

    @abstractmethod
    def rel_waitmove(self, value: (int, float)) -> int:
        raise Exception('Interface Instance error')

    @abstractmethod
    def waitmove(self, position: float) -> int:
        raise Exception('Interface Instance error')

    @abstractmethod
    def waitForReturning(self, waittimes: int = 240000) -> int:
        raise Exception('Interface Instance error')

    @abstractmethod
    def waitForMoving(self, waittimes: int) -> int:
        raise Exception('Interface Instance error')

    @abstractmethod
    def waitmoveComplete(self, position: float) -> int:
        raise Exception('Interface Instance error')

    @abstractmethod
    def waitForReadOnlyRegister(self, index: int, enable: bool = True, waittimes: int = 60000) -> int:
        raise Exception('Interface Instance error')

    @abstractmethod
    def waitForStatusRegister(self, index: int, enable: bool = True, waittimes: int = 60000) -> int:
        raise Exception('Interface Instance error')

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

    @abstractmethod
    def setEnable(self, enable: bool) -> int:
        raise Exception('Interface Instance error')

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

    @abstractmethod
    def setPower(self, enable: bool) -> int:
        raise Exception('Interface Instance error')

    @abstractmethod
    def Reset     (self, enable: bool, delay: int = 300) -> int:
        raise Exception('Interface Instance error')

    @abstractmethod
    def position(self) -> float:
        raise Exception('Interface Instance error')

    @abstractmethod
    def getPosition(self) -> (int, float):
        raise Exception('Interface Instance error')

    @abstractmethod
    def get_position(self) -> (int, float):
        pass
    @abstractmethod
    def isEnabled      (self) -> bool:
        raise Exception('Interface Instance error')

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

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

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

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

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

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

    @abstractmethod
    def setHome(self, enable: bool) -> int:
        raise Exception('Interface Instance error')

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

    @abstractmethod
    def setMove(self, enable: bool, isDelay: bool = True) -> int:
        raise Exception('Interface Instance error')

    @abstractmethod
    def setScanMoveEnable(self, enable: bool) -> int:
        raise Exception('Interface Instance error')

    @abstractmethod
    def setMoveTarget(self, target: (int, float), isDelay: bool = True) -> int:
        raise Exception('Interface Instance error')

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

    @abstractmethod
    def setForward(self, enable: bool) -> int:
        raise Exception('Interface Instance error')

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

    @abstractmethod
    def setBackward(self, enable: bool) -> int:
        raise Exception('Interface Instance error')

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

    @abstractmethod
    def setReset(self, enable: bool) -> int:
        raise Exception('Interface Instance error')

    @abstractmethod
    def setMoveParameter(self, position: float, speed: float = None, acc: float = None, dec: float = None, isDelay: bool = False) -> int:
        raise Exception('Interface Instance error')

    @abstractmethod
    def changedMoveParameter(self, speed: float = None, acc: float = None, dec: float = None, isDelay: bool = False) -> int:
        raise Exception('Interface Instance error')

    @abstractmethod
    def changedJopParameter(self, speed: float = None, acc: float = None, dec: float = None, isDelay: bool = False) -> int:
        raise Exception('Interface Instance error')

    @abstractmethod
    def setScanMove(self, position: float) -> int:
        raise Exception('Interface Instance error')


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

    @abstractmethod
    def new_handler(self) -> ModBusImplInterface:
        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 new_handler(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')
