#!D:/IDE/anaconda3/envs/Microscope_Scan python
# -*- coding: UTF-8 -*-
"""
@FileName：ManipulatorReader.py
@Description：
@Author：XSH
@Time：2025/11/14 18:32
@Department：研发部
@Copyright：©2025 乐辰生物
"""
from PyQt5                          import Qt
from AppSettings.Settings           import *
from AppCore.Levels                 import *
from AppInterfaces.utils            import *
from AppInterfaces.ImplInterfaces   import *
from AppContexts.Contexts import AxisContext, AxisRegisterContext, DetectionContext, DetectionRegisterContext, HookRegisterContext, HookContext
from AppInterfaces.ReaderInterfaces import ManipulatorReaderInterface


class ManipulatorReader(Qt.QObject, ManipulatorReaderInterface):
    
    def __init__(self, parent: Qt.QObject = None, **args):
        super(ManipulatorReader, self).__init__(parent=parent)
        self.Initialization('ManipulatorReader', self, parent)
        self.setManager(args.get('pool', None))

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

    def initValue(self):
        super(ManipulatorReader, self).initValue()

        self.initAxisX    ()
        self.initAxisY    ()
        self.initAxisZ    ()
        self.initAxisH    ()
        self.initScanX    ()
        self.initScanY    ()
        self.initSlowX    ()
        self.initSlowY    ()
        self.initDropX    ()
        self.initDropY    ()
        self.initDetector ()

    def initManager(self, **args):
        if not self.hasManager:
            from AppCore.AppImpls.ModBusPool import ModBusPool
            self.setManager(ModBusPool.GetInstance())

        else:
            self.setManager(self.manager)
        if not self.Manager.IsOpen:
            self.Manager.Open()
    
    def deleManager(self, **args):
        if self.hasManager:
            self.Manager.Close()
            self.Manager.deleManager()


    
    def Transmit(self, **args) -> int:
        return RECODE_FAILURE

    
    def Received(self, **args) -> int:
        return RECODE_FAILURE

    
    def Send(self, **args) -> int:
        return RECODE_FAILURE

    
    def Read(self, **args) -> int:
        return RECODE_FAILURE

    
    def WaitDone(self, **args) -> int:
        return RECODE_FAILURE


    
    def initAxisX(self):
        if not self.hasAxisX:
            from AppCore.Impls import AxisImpl
            from AppSettings.AppSetting import AppConfig
            context: AxisContext = AxisContext()
            context.asRegisterContext(AppConfig.Manipulator.AxisX)
            self.AxisX = AxisImpl(context, self.manager, self)

    
    def initAxisY(self):
        if not self.hasAxisY:
            from AppCore.Impls import AxisImpl
            from AppSettings.AppSetting import AppConfig
            context: AxisContext = AxisContext()
            context.asRegisterContext(AppConfig.Manipulator.AxisY)
            self.AxisY = AxisImpl(context, self.manager, self)

    
    def initAxisH(self):
        if not self.hasAxisH:
            from AppCore.Impls import HookImpl
            from AppSettings.AppSetting import AppConfig
            context: HookContext = HookContext()
            context.asRegisterContext(AppConfig.Manipulator.AxisH)
            self.AxisH = HookImpl(context, self.manager, self)

    
    def initScanX(self):
        if not self.hasScanX:
            from AppCore.Impls import AxisImpl
            from AppSettings.AppSetting import AppConfig
            context: AxisContext = AxisContext()
            context.asRegisterContext(AppConfig.Manipulator.ScanX)
            self.ScanX = AxisImpl(context, self.manager, self)

    
    def initScanY(self):
        if not self.hasScanY:
            from AppCore.Impls import AxisImpl
            from AppSettings.AppSetting import AppConfig
            context: AxisContext = AxisContext()
            context.asRegisterContext(AppConfig.Manipulator.ScanY)
            self.ScanY = AxisImpl(context, self.manager, self)

    
    def initSlowX(self):
        if not self.hasSlowX:
            from AppCore.Impls import AxisImpl
            from AppSettings.AppSetting import AppConfig
            context: AxisContext = AxisContext()
            context.asRegisterContext(AppConfig.Manipulator.SlowX)
            self.SlowX = AxisImpl(context, self.manager, self)

    
    def initSlowY(self):
        if not self.hasSlowY:
            from AppCore.Impls import AxisImpl
            from AppSettings.AppSetting import AppConfig
            context: AxisContext = AxisContext()
            context.asRegisterContext(AppConfig.Manipulator.SlowY)
            self.SlowY = AxisImpl(context, self.manager, self)

    
    def initDropX(self):
        if not self.hasDropX:
            from AppCore.Impls import AxisImpl
            from AppSettings.AppSetting import AppConfig
            context: AxisContext = AxisContext()
            context.asRegisterContext(AppConfig.Manipulator.DropX)
            self.DropX = AxisImpl(context, self.manager, self)
    
    def initDropY(self):
        if not self.hasDropY:
            from AppCore.Impls import AxisImpl
            from AppSettings.AppSetting import AppConfig
            context: AxisContext = AxisContext()
            context.asRegisterContext(AppConfig.Manipulator.DropY)
            self.DropY = AxisImpl(context, self.manager, self)

    
    def initDetector(self):
        if not self.hasDetector:
            from AppCore.Impls import DetectorImpl
            from AppSettings.AppSetting import AppConfig
            context: DetectionContext = DetectionContext()
            context.asRegisterContext(AppConfig.Manipulator.Detection)
            self.Detector = DetectorImpl(context, self.manager, self)

    def initAxisZ(self):
        if not self.hasAxisZ:
            from AppCore.Impls import AxisImpl
            from AppSettings.AppSetting import AppConfig
            context: AxisContext = AxisContext()
            context.asRegisterContext(AppConfig.Manipulator.AxisZ)
            self.AxisZ = AxisImpl(context, self.manager, self)

