#!D:/IDE/anaconda3/envs/Microscope_Scan python
# -*- coding: UTF-8 -*-
"""
@FileName：MotionReader.py
@Description：
@Author：XSH
@Time：2025/11/15 16:14
@Department：研发部
@Copyright：©2025 乐辰生物
"""
from PyQt5                          import Qt
from AppSettings.Settings           import *
from AppCore.Levels                 import *
from AppInterfaces.utils            import *
from AppInterfaces.ImplInterfaces   import *
from AppUtils.utils                 import *
from AppInterfaces.ReaderInterfaces import ManipulatorReaderInterface, MicroscopeReaderInterface, MotionReaderInterface

class MotionReader(Qt.QObject, MotionReaderInterface, SingletonInterface):
    Locker: Qt.QMutex = Qt.QMutex()

    @classmethod
    def GetInstance(cls, *args, **kwargs) -> 'MotionReader':
        return super(MotionReader, cls).GetInstance(*args, **kwargs)

    def __init__(self, parent: Qt.QObject = None):
        super(MotionReader, self).__init__(parent=parent)
        self.Initialization('MotionReader', self, parent)

    def initValue(self):
        super(MotionReader, self).initValue()
        self.initManager()
    
    def initManipulator(self):
        if not self.hasManipulator:
            from AppCore.AppReaders.ManipulatorReader import ManipulatorReader
            self.Manipulator = ManipulatorReader(self)

    
    def initMicroscope(self):
        if not self.hasMicroscope:
            from AppCore.AppReaders.MicroscopeReader import MicroscopeReader
            self.Microscope = MicroscopeReader.GetInstance()

    @property
    def isAvailable(self) -> bool:
        return self.isValid and self.Microscope.isAvailable and self.Manipulator.isAvailable

    @property
    def isValid(self) -> bool:
        return self.hasManipulator and self.hasMicroscope and self.hasManager

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

    
    def Open(self, **args) -> int:

        if not self.isValid:
            self.initManager(**args)
        if self.hasManager:
            self.Manager.Open()

        if not self.isValid:
            return RECODE_FAILURE
        isWrong: int = self.Manipulator.Open(**args)
        if RECODE_SUCCEED != isWrong:
            return isWrong
        isWrong = self.Microscope.Open(**args)
        return isWrong

    
    def Close(self, **args) -> int:
        if not self.isValid:
            return RECODE_SUCCEED
        if self.hasManager:
            self.Manager.Close()
        isWrong: int = self.Manipulator.Close(**args)
        if RECODE_SUCCEED != isWrong:
            return isWrong
        isWrong = self.Microscope.Close(**args)
        return isWrong

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

        self.initManipulator()
        self.initMicroscope()
        if self.hasManipulator:
            if self.Manipulator.manager != self.Manager:
                self.Manipulator.setManager(self.Manager)

    
    def deleManager(self, **args):
        self.Manager = None
        if self.hasManipulator:
            try:
                self.Manipulator.setManager(None)
                del self.Manipulator
            except Exception as e:
                AppUtils.print_Exception(e)
        if self.hasMicroscope:
            try:
                if self.Microscope.IsOpen:
                    self.Microscope.Close()
                self.Microscope.deleManager()
                del self.Microscope
            except Exception as e:
                AppUtils.print_Exception(e)
        self.Microscope     = None
        self.Manipulator    = None