#!D:/IDE/anaconda3/envs/Microscope_Scan python
# -*- coding: UTF-8 -*-
"""
@FileName：OnlyDrawPool.py
@Description：
@Author：XSH
@Time：2025/11/15 11:53
@Department：研发部
@Copyright：©2025 乐辰生物
"""
import numpy as np
import cv2
from PyQt5                                                  import Qt
from AppInterfaces.utils                                    import *
from AppUtils.utils                                         import *
from AppInterfaces.EventInterfaces                          import *
from AppCore.Levels                                         import *

class OnlyDrawPool(Qt.QThread, AppEventInterface, SingletonInterface):
    AppendItem: Qt.pyqtSignal = Qt.pyqtSignal(object)
    _IOLocker: Qt.QReadWriteLock = Qt.QReadWriteLock()
    Locker: Qt.QMutex = Qt.QMutex()

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

    def __init__(self, parent: Qt.QObject = None):
        super(OnlyDrawPool, self).__init__(parent=parent)
        self.Initialization('DataOnlyDrawPool', self, parent)
        self.Mode = EventLevel.OnlyDrawMode
        # self.Start()

    def initConnect(self):
        super(OnlyDrawPool, self).initConnect()
        TYPE: Qt.Qt.ConnectionType = Qt.Qt.ConnectionType(Qt.Qt.AutoConnection | Qt.Qt.UniqueConnection)
        self.AppendItem.connect(slot=self.SlotAppendItem, type=TYPE)

    def initValue(self):
        super(OnlyDrawPool, self).initValue()
        self.initPools()

    def SendEvent(self, value, **args):
        self.AppendItem.emit(value)
        isAsync: bool = args.get('isAsync', False)
        if not isAsync:
            AppTimeLoop.processGlobalEvents(100)

    def StartWork(self):
        self.initPools  ()
        self.Start      ()

    def StopWork(self):
        pass

    def StartFocusing(self, isShowFocusing: bool = True, **args):
        self.initPools()
        self.Start()

    def StopFocusing(self, isShowFocusing: bool = True, **args):
        pass

    def appendItem(self, item, *args, **kwargs):
        if not self.isRunning():
            self.is_running = True
            self.start(Qt.QThread.NormalPriority)
        if isinstance(item, ImageEvent):
            with self.condition:
                self.DataBuffer.put(item)
                self.condition.notify_all()

    def Start(self):
        if self.isRunning():
            return
        self.initPools()
        self.is_running = True
        self.start(Qt.QThread.NormalPriority)

    def run(self) -> None:
        self.process()

    def WaitDone(self, item, *args, **kwargs):
        if isinstance(item, EventInterface) and item.mode == self.mode:
            item.WaitDone(*args, **kwargs)


    def AppendQImage(self, buffer: Qt.QImage, **args):
        self.SendEvent(ImageEvent.instance(image=buffer, **args))

    def AppendImage(self, buffer: np.ndarray, **args):
        self.SendEvent(ImageEvent.instance(image=buffer, **args))


    def initPools(self):
        pass


    def delePools(self):
        pass
