#!D:/IDE/anaconda3/envs/Microscope_Scan python
# -*- coding: UTF-8 -*-
"""
@FileName：MicroscopeInterfaceBase.py
@Description：
@Author：XSH
@Time：2025/12/12 11:50
@Department：研发部
@Copyright：©2025 乐辰生物
"""
from typing import Union
from PyQt5 import Qt
import clr
from AppSettings.Settings           import *
from abc                            import abstractmethod
from AppInterfaces.ImplInterface    import ImplInterface
from AppContexts.Contexts           import *

class MicroscopeInterfaceBase(ImplInterface):
    Parameter   : (ZeissContext, LeiCaContext)  # 显微镜参数类
    Manager     : 'IMicroscope'                 # 显微镜管理器
    Controller  : "IMicController"              # 显微镜控制器

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

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

    @property
    def hasController  (self) -> bool:
        return hasattr(self, 'Controller'  ) and self.Controller is not None

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

    @property
    def isAvailable(self) -> bool:
        return self.isValid and self.Manager.IsAvailable

    @property
    def IsOpen(self) -> bool:
        return self.isValid and self.Manager.IsOpen

    @property
    def isEnabled(self) -> bool:
        return self.isValid and self.Manager.IsOpen

    @property
    def isDisable(self) -> bool:
        return not self.isEnabled


    @abstractmethod
    def Open(self, timeOut: int = 1000, **args) -> int:
        raise Exception('Interface Instance error')

    @abstractmethod
    def Close(self, timeOut: int = 1000, **args) -> int:
        raise Exception('Interface Instance error')

    def ReOpen(self, timeOut: int = 1000, **args) -> int:
        self.Close(timeOut, **args)
        return self.Open(timeOut, **args)

    @abstractmethod
    def initManager(self, **args):
        raise Exception('Interface Instance error')

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    @abstractmethod
    def set_config_mode(self, enable: (int, bool) = 0):
        """设置配置模式 0: 配置模式关闭 1：配置模式开启"""
        raise Exception('Interface Instance error')

    @abstractmethod
    def get_config_mode(self):
        """设置配置模式 0: 配置模式关闭 1：配置模式开启"""
        raise Exception('Interface Instance error')

    @abstractmethod
    def home(self) -> int:
        return self.initMicroscope()

    @abstractmethod
    def waithome(self) -> int:
        return self.initMicroscope()

    @abstractmethod
    def move(self, value: (int, float), mode: int = 1, **args) -> int:
        raise Exception('Interface Instance error')

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

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

    @abstractmethod
    def rel_waitmove(self, value: (int, float), mode: int = 1, **args):
        raise Exception('Interface Instance error')

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    @abstractmethod
    def waitmove_lower_limit(self, **args):
        raise Exception('Interface Instance error')

    @abstractmethod
    def waitmove_upper_limit(self, **args) -> int:
        raise Exception('Interface Instance error')

    @abstractmethod
    def waitmove_h_lower_limit(self, **args) -> int:
        raise Exception('Interface Instance error')

    @abstractmethod
    def waitmove_h_upper_limit(self, **args) -> int:
        raise Exception('Interface Instance error')

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

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

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

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

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

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

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

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

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

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

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

    @abstractmethod
    def changedObjective(self, level: int, is_lower_limit: bool = False, **args) -> int:
        raise Exception('Interface Instance error')

    @abstractmethod
    def set_objective(self, level: int, is_lower_limit: bool = False, **args) -> int:
        raise Exception('Interface Instance error')

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

    @abstractmethod
    def changedLightIntensity(self, level: int, **args) -> int:
        raise Exception('Interface Instance error')

    @abstractmethod
    def set_z_upper_limit(self, limit=-1) -> int:
        raise Exception('Interface Instance error')

    @abstractmethod
    def set_z_lower_limit(self, limit=-1) -> int:
        raise Exception('Interface Instance error')

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

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

    @abstractmethod
    def init_objective_range(self, **args) -> int:
        raise Exception('Interface Instance error')

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

    @abstractmethod
    def wait_init_z_range(self) -> int:
        raise Exception('Interface Instance error')
    @abstractmethod
    def init_z_speed(self, level: int) -> int:
        raise Exception('Interface Instance error')

    @abstractmethod
    def set_z_range(self, min_limit: int = -1, max_limit: int = -1) -> int:
        raise Exception('Interface Instance error')

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

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

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

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

    @abstractmethod
    def set_z_speed(self, speed: (float, int)):
        raise Exception('Interface Instance error')

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    @abstractmethod
    def set_objective_patho_mode(self, mode: int = 0) -> int:
        raise Exception('Interface Instance error')

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



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

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

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

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

    @abstractmethod
    def POS_CONST_Z(self, speed: int, target: int = None, **args) -> int:
        raise Exception('Interface Instance error')

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

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

    @abstractmethod
    def set_z_calibrate(self, level: int = None) -> int:
        raise Exception('Interface Instance error')

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

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

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

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

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

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

    @abstractmethod
    def changedLAMP(self, value: int, **args) -> int:
        raise Exception('Interface Instance error')

    @abstractmethod
    def changedLFBL(self, value: int, **args):
        raise Exception('Interface Instance error')

    @abstractmethod
    def changedAPBL(self, value: int, **args) -> int:
        raise Exception('Interface Instance error')

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

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

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

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

    @abstractmethod
    def get_lamp_enable(self, **args) -> int:
        raise Exception('Interface Instance error')

    @abstractmethod
    def get_apbl(self, **args) -> (int, int):
        raise Exception('Interface Instance error')

    @abstractmethod
    def get_lfbl(self, **args) -> (int, int):
        raise Exception('Interface Instance error')

    @abstractmethod
    def get_lamp(self, **args) -> (int, int):
        raise Exception('Interface Instance error')

    @abstractmethod
    def set_tl_rl_switch(self, enable: (int, bool), **args):
        raise Exception('Interface Instance error')

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

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

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

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

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

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

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

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

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