#!D:/IDE/anaconda3/envs/Microscope_Scan python
# -*- coding: UTF-8 -*-
"""
@FileName：CameraReader.py
@Description：
@Author：XSH
@Time：2025/11/12 14:20
@Department：研发部
@Copyright：©2025 乐辰生物
"""
import numpy as np
import cv2
from PyQt5                                          import Qt
from AppContexts.Contexts                           import CameraContext
from AppInterfaces.ImplInterfaces                   import CameraImplInterface
from AppInterfaces.utils                            import *
from AppUtils.AppUtils                              import AppUtils
from AppCore.Requests                               import CameraRequest
from AppCore.Levels                                 import ObjectiveLevel
from AppCore.AppDBModels.CameraModels.CameraModel   import CameraModel, CameraInterface
from AppSettings.Settings                           import (RECODE_FAILURE, RECODE_SUCCEED, SIGNAL_CONNECTION_TYPE)
from AppInterfaces.ReaderInterfaces                 import CameraReaderInterface


class CameraReader(Qt.QObject, CameraReaderInterface, SingletonInterface):
    Locker: Qt.QMutex = Qt.QMutex()

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

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

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






    
    def Open(self, level: int = 7, timeOut: int = 1000, **args) -> int:
        isWrong: int = RECODE_FAILURE
        if not self.hasManager:
            self.initManager()
        if self.hasManager:
            if self.IsOpen:
                self.Manager.setModel(level)
                isWrong = RECODE_SUCCEED if self.Manager.IsOpen else RECODE_FAILURE
            else:
                self.Manager.initContext()
                isWrong = self.Manager.ReOpen(level, timeOut, **args)
        if self.IsOpen:
            isWrong = RECODE_SUCCEED

        return isWrong

    
    def Close(self, timeOut: int = 1000, **args) -> int:
        if not self.hasManager:
            self.initManager()
        if not self.hasManager:
            return RECODE_FAILURE
        return self.Manager.Close(timeOut, **args)

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


    
    def deleManager(self, **args):
        if self.hasManager:
            self.Close(**args)
            self.deleManager(**args)
            del self.Manager
        self.Manager = None
    

    