#!D:/IDE/anaconda3/envs/Microscope_Scan python
# -*- coding: UTF-8 -*-
"""
@FileName：MicroscopeReader.py
@Description：
@Author：XSH
@Time：2025/11/15 9:03
@Department：研发部
@Copyright：©2025 乐辰生物
"""
from PyQt5 import Qt
from AppInterfaces.utils            import *
from AppInterfaces.ImplInterfaces   import MicroscopeInterface
from AppInterfaces.ReaderInterfaces import MicroscopeReaderInterface
from AppContexts.Contexts           import MicroscopeContext, ZeissContext, LeiCaContext
from AppSettings.Settings           import *

class MicroscopeReader(Qt.QObject, MicroscopeReaderInterface, SingletonInterface):
    Locker: Qt.QMutex = Qt.QMutex()

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

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


    def initValue(self):
        super(MicroscopeReader, self).initValue()
        self.Manager = None

    
    def Open(self, timeOut: int = 1000, **args) -> int:
        isWrong: int = RECODE_FAILURE
        if not self.isValid:
            self.initManager(**args)
        if not self.isValid:
            return isWrong
        if not self.IsOpen:
            self.manager.ReOpen(timeOut, **args)
        return RECODE_SUCCEED if self.IsOpen else RECODE_FAILURE


    
    def Close(self, timeOut: int = 1000, **args) -> int:
        isWrong: int = RECODE_SUCCEED
        if not self.isValid or not self.IsOpen:
            return isWrong
        return self.manager.Close(timeOut, **args)

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

            self.Manager.Parameter = self.Parameter
            self.Manager.Open(**args)


    
    def deleManager(self):
        if self.hasManager:
            try:
                del self.Manager
            except Exception as e:
                pass
            self.Manager = None

    
    def initContext(self):
        if not self.hasParameter:
            from AppSettings.AppSetting import AppConfig
            self.Parameter = ZeissContext()
            self.Parameter.deserialize(AppConfig.ZeissMicroscope.serialize())


