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


class HookImplInterface(ImplInterface):
    Manager     : ModBusPoolInterface       #
    Parameter   : HookContext               #
    Level       : int                       #

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

    def setParameter(self, value):
        self.Parameter = value if isinstance(value, HookContext) 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, HookContext)

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

    @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')

    @abstractmethod
    def set_h_clear_zero(self, enable: (int, bool) = True) -> int:
        pass

    @abstractmethod
    def set_h_force_quit(self, enable: (int, bool) = True) -> int:
        pass

    @abstractmethod
    def wait_force_quit(self, waittimes: int = 1000, **args):
        pass

    @abstractmethod
    def pick_tray(self, layer: int) -> int:
        pass

    @abstractmethod
    def wait_pick_tray(self, layer: int) -> int:
        pass

    @abstractmethod
    def waitForPickTrayComplete(self) -> int:
        pass

    @abstractmethod
    def drop_tray(self, layer: int) -> int:
        pass


    @abstractmethod
    def wait_drop_tray(self, layer: int) -> int:
        pass

    @abstractmethod
    def reset_hook(self) -> int:
        pass

    @abstractmethod
    def waitForDropTrayComplete(self) -> int:
        pass

    @abstractmethod
    def wait_check_tray(self, layer: int) -> int:
        pass

    @abstractmethod
    def waitForDetectorTrayComplete(self) -> int:
        pass

    @abstractmethod
    def wait_check_chamber(self) -> int:
        pass

    @abstractmethod
    def wait_check_chamber_box(self, **args) -> int:
        pass
    @abstractmethod
    def waitForDetectorChamberComplete(self) -> int:
        pass

    @abstractmethod
    def move_scan_code_position(self) -> int:
        pass

    @abstractmethod
    def waitmove_scan_code_position(self, layer: int) -> int:
        pass

    @abstractmethod
    def move_scan_code_complete(self) -> int:
        pass

    @abstractmethod
    def waitmove_scan_code_complete(self) -> int:
        pass

    @abstractmethod
    def waitForScanCodePickComplete(self) -> int:
        pass

    @abstractmethod
    def wait_empty_hook_return(self) -> int:
        pass

    @abstractmethod
    def waitForEmptyTrayReturnComplete(self) -> int:
        pass

    @abstractmethod
    def forward(self, enable: (int, bool)) -> int:
        pass

    @abstractmethod
    def backward(self, enable: (int, bool)) -> int:
        pass

    @abstractmethod
    def get_forward_status(self) -> int:
        pass

    @abstractmethod
    def get_backward_status(self) -> int:
        pass

    @abstractmethod
    def get_back_limit_status(self) -> int:
        pass

    @abstractmethod
    def get_front_limit_status(self) -> int:
        pass

    @abstractmethod
    def Reset(self, enable: (int, bool) = True) -> int:
        pass


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

    def get_h_position(self) -> (int, float):
        return self.getPosition()

    @abstractmethod
    def getVelocity(self) -> (int, float):
        return self.get_h_speed()

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

    @abstractmethod
    def get_layer_hover_height(self) -> (int, float):
        pass

    @abstractmethod
    def set_layer_hover_height(self, value: (int, float)) -> int:
        pass

    @abstractmethod
    def get_layer_drag_height(self) -> (int, float):
        pass

    @abstractmethod
    def set_layer_drag_height(self, value: (int, float)) -> int:
        pass

    @abstractmethod
    def get_layer_spacing_height(self) -> (int, float):
        pass

    @abstractmethod
    def set_layer_spacing_height(self, value: (int, float)) -> int:
        pass

    @abstractmethod
    def get_layer_axis_x_position(self) -> (int, float):
        pass

    @abstractmethod
    def set_layer_axis_x_position(self, value: (int, float)) -> int:
        pass

    @abstractmethod
    def get_layer_axis_y_position(self) -> (int, float):
        pass

    @abstractmethod
    def set_layer_axis_y_position(self, value: (int, float)) -> int:
        pass

    @abstractmethod
    def get_layer_axis_x_detector_position(self) -> (int, float):
        pass

    @abstractmethod
    def set_layer_axis_x_detector_position(self, value: (int, float)) -> int:
        pass

    @abstractmethod
    def get_layer_axis_y_detector_position(self) -> (int, float):
        pass

    @abstractmethod
    def set_layer_axis_y_detector_position(self, value: (int, float)) -> int:
        pass

    @abstractmethod
    def get_layer_scancode_position(self) -> (int, float):
        pass

    @abstractmethod
    def set_layer_scancode_position(self, value: (int, float)) -> int:
        pass

    @abstractmethod
    def get_layer_scancode_spacing(self) -> (int, float):
        pass

    @abstractmethod
    def set_layer_scancode_spacing(self, value: (int, float)) -> int:
        pass

    @abstractmethod
    def get_layer_index(self) -> (int, int):
        pass

    @abstractmethod
    def set_layer_index(self, index: int) -> int:
        pass

    @abstractmethod
    def get_layer_detector_position(self) -> (int, float):
        pass

    @abstractmethod
    def set_layer_detector_position(self, value: (int, float)) -> int:
        pass

    @abstractmethod
    def get_lower_limit_status             (self) -> int:
        pass

    @abstractmethod
    def get_upper_limit_status             (self) -> int:
        pass

    @abstractmethod
    def get_place_status                   (self) -> int:
        pass

    @abstractmethod
    def get_detector_status                (self) -> (int, int):
        pass

    @abstractmethod
    def get_pick_tray_status               (self) -> int:
        pass

    @abstractmethod
    def get_drop_tray_status               (self) -> int:
        pass

    @abstractmethod
    def get_tray_detector_status           (self) -> int:
        pass

    @abstractmethod
    def get_chamber_detector_status        (self) -> int:
        pass

    @abstractmethod
    def get_pick_scan_code_status          (self) -> int:
        pass

    @abstractmethod
    def get_empty_tray_return_status       (self) -> int:
        pass

    @abstractmethod
    def get_forward_warning_status         (self) -> int:
        pass

    @abstractmethod
    def get_backward_warning_status         (self) -> int:
        pass

    @abstractmethod
    def get_layer_detector_status           (self, layer: int) -> int:
        pass

    @abstractmethod
    def get_layer_status(self) -> list:
        pass

    @abstractmethod
    def get_layer_status_(self) -> list:
        pass

    @abstractmethod
    def get_hook_back_limit_status(self) -> int:
        """获取挂钩后限位状态"""
        raise Exception('Interface Instance error')

    @abstractmethod
    def get_hook_front_limit_status(self) -> int:
        """获取挂钩前限位状态"""
        raise Exception('Interface Instance error')

    @abstractmethod
    def get_tray_place_status(self) -> int:
        """获取载玻台托盘状态"""
        raise Exception('Interface Instance error')

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

    @abstractmethod
    def waitmove_to_back(self) -> int:
        """移动到后限位"""
        pass

    @abstractmethod
    def waitmove_to_front(self) -> int:
        """移动到前限位"""
        pass

    @abstractmethod
    def waitForBackComplete(self) -> int:
        """移动到后限位"""
        pass

    @abstractmethod
    def waitForFrontComplete(self) -> int:
        """移动到前限位"""
        pass

    @abstractmethod
    def waitHome(self) -> int:
        pass
    @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:
        pass
