#!D:/IDE/anaconda3/envs/Microscope_Scan python
# -*- coding: UTF-8 -*-
"""
@FileName：MicroscopeImpl.py
@Description：
@Author：XSH
@Time：2025/12/11 18:01
@Department：研发部
@Copyright：©2025 乐辰生物
"""
from PyQt5                                          import Qt
from AppInterfaces.ImplInterfaces                   import LMicroscopeInterface, ZMicroscopeInterface, MicroscopeInterface
from AppContexts.Contexts                           import SerialContext
from AppInterfaces.utils                            import *
from AppUtils.utils                                 import *
from AppCore.Levels                                 import *
from AppCore.Requests                               import *
from AppSettings.Settings                           import (RECODE_FAILURE, RECODE_SUCCEED)


class MicroscopeImpl(Qt.QObject, MicroscopeInterface):

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


    def initValue(self):
        super(MicroscopeImpl, self).initValue()
        self.initManager()

    def initManager(self, **args):
        if not self.hasManager:
            from AppCore.AppImpls.LMicroscopeImpl import LMicroscopeImpl
            from AppCore.AppImpls.ZMicroscopeImpl import ZMicroscopeImpl
            from AppSettings.SystemSetting import SystemSetting, SystemConfig
            if SystemConfig.isLeiCaMicroscope():
                self.Manager = LMicroscopeImpl(self)
            else:
                self.Manager = ZMicroscopeImpl(self)


    def deleManager(self):
        if self.hasManager:
            try:
                self.Manager.Close()
                del self.Manager
            except Exception as e:
                AppUtils.print_Exception(e)
        self.Manager = None
        from AppSettings.SystemSetting import SystemSetting, SystemConfig
        if self.hasParameter:
            if SystemConfig.isLeiCaMicroscope():
                self.Parameter.IsOpen = False
            else:
                self.Parameter.IsSucceed = False

    def initContext(self):
        from AppSettings.SystemSetting import SystemSetting, SystemConfig
        from AppSettings.AppSetting import AppSetting, AppConfig
        from AppContexts.MicroscopeContext import MicroscopeContext
        if not self.hasParameter:
            if SystemConfig.isLeiCaMicroscope():
                self.Parameter = SerialContext()
                if AppConfig.hasLMicroscopeConfig:
                    self.Parameter.deserialize(AppConfig.LeiCaMicroscope.serialize())
                else:
                    self.Parameter.initMicroscope()
            else:
                self.Parameter = MicroscopeContext()
                if AppConfig.hasMicroscopeConfig:
                    self.Parameter.deserialize(AppConfig.Microscope.serialize())



