#!D:/IDE/anaconda3/envs python
# -*- coding: UTF-8 -*-
"""
 @Author: Administrator
 @FileName：ScanningControllerPool.py\n
 @DateTime: 2025/10/27 10:55\n
 @Author  ：xsh
"""
from AppInterfaces.ThreadPoolInterface                      import ThreadPoolInterface, PoolUtil    #
import threading
from AppInterfaces.utils                                    import *
from AppCore.DBModels                                       import *
from AppCore.Levels                                         import *
from AppCore.Requests                                       import *
from AppCore.Events                                         import *
from AppUtils.utils                                         import *
from AppSettings.utils                                      import *
class ScanningControllerPool(Qt.QThread, SingletonInterface):
    _IOLocker   : Qt.QReadWriteLock = Qt.QReadWriteLock()
    Locker      : Qt.QMutex = Qt.QMutex()
    _args       : tuple
    _kwargs     : dict
    _callback   : object
    _result     : object
    _running    : bool
    @property
    def has_args(self) -> bool:
        return hasattr(self, '_args') and isinstance(self._args, (tuple, list))
    @property
    def has_kwargs(self) -> bool:
        return hasattr(self, '_kwargs') and isinstance(self._kwargs, dict)

    @property
    def has_callback(self) -> bool:
        return hasattr(self, '_callback') and self._callback is not None

    @property
    def has_result(self) -> bool:
        return hasattr(self, '_result') and self._result is not None

    @property
    def has_running(self) -> bool:
        return hasattr(self, '_running') and isinstance(self._running, (bool, int))
    @property
    def result(self):
        return self._result if self.has_result else None

    @result.setter
    def result(self, value):
        self._result = value
    @property
    def running(self) -> bool:
        return self._running if self.has_running else False

    @running.setter
    def running(self, value: (bool, int)):
        self._running = True if isinstance(value, (bool, int)) and value else False

    @property
    def callback(self):
        return self._callback if self.has_callback else None

    @callback.setter
    def callback(self, value):
        self._callback = value

    @property
    def args(self):
        return self._args if self.has_args else None
    @args.setter
    def args(self, value):
        self._args = value

    @property
    def kwargs(self):
        return self._kwargs if self.has_kwargs else None

    @kwargs.setter
    def kwargs(self, value):
        self._kwargs = value

    def Start(self, target = None,args = None,kwargs = None, result = None ):
        if self.isRunning():
            return
        self._running = False
        self.initValue(target, args, kwargs, result)
        self.start(Qt.QThread.HighPriority)
        return self.waitForRunning()




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

    def __init__(self, target = None,args = None,kwargs = None, result = None , parent: Qt.QObject = None):
        super(ScanningControllerPool, self).__init__(parent=parent)
        self.initValue(target, args, kwargs, result)


    def initValue(self, target = None,args = None,kwargs = None, result = None ):
        try:
            self._callback = target
            self._args     = args
            self._kwargs   = kwargs
            self._result   = result
        except Exception as e:
            self._callback = None
            self._args     = None
            self._kwargs   = None
            self._result   = None
            AppUtils.print_Exception(e)
    def run(self) -> None:
        if self.has_callback:
            self._running = True
            try:
                if self.has_args:
                    if self.has_kwargs:
                        self._result = self._callback(*self.args, **self._kwargs)
                    else:
                        self._result = self._callback(*self.args)
                else:
                    if self.has_kwargs:
                        self._result = self._callback(**self._kwargs)
                    else:
                        self._result = self._callback()
            except Exception as e:
                AppUtils.print_Exception(e)
            self._running = False
        return

    def waitForFinishing(self) -> bool:
        while self.running:
            AppTimeLoop.processGlobalEvents(1000)
        return self.isRunning()


    def waitForRunning(self) -> bool:
        while not self.running:
            AppTimeLoop.processGlobalEvents(1000)
        return self.isRunning()








