#!D:/IDE/anaconda3/envs/Microscope_Scan python
# -*- coding: UTF-8 -*-
"""
@FileName：AppTaskPool.py
@Description：
@Author：XSH
@Time：2025/11/19 10:04
@Department：研发部
@Copyright：©2025 乐辰生物
"""
from PyQt5 import Qt
from AppUtils.utils import *
from AppInterfaces.utils import *
from AppInterfaces.TaskPoolInterface import TaskPoolInterface

class AppTaskPool(Qt.QObject, TaskPoolInterface, SingletonInterface):
    Locker      : Qt.QMutex     = Qt.QMutex     (       )
    AppendItem  : Qt.pyqtSignal = Qt.pyqtSignal (object )
    @classmethod
    def GetInstance(cls, *args, **kwargs) -> 'AppTaskPool':
        return super(AppTaskPool, cls).GetInstance(*args, **kwargs)

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


    def __del__(self):
        self.deleValue()
        return super(AppTaskPool, self).__del__()

    def initValue(self):
        super(AppTaskPool, self).initValue()
        self.initWorkerSize ()
        self.initExecutor   ()
        self.initFutures    ()

    def deleValue(self):
        super(AppTaskPool, self).deleValue()
        self.shutdown()


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

    def SlotAppendItem(self, task: (tuple, list)):
        if not isinstance(task, (tuple, list)):
            return
        if len(task) <= 0:
            return
        func = task[0]
        if not func:
            return
        args = task[1] if len(task) > 1 else ()
        kwargs = task[2] if len(task) > 2 else {}
        self.submit(func, *args, **kwargs)



