#!D:\IDE\Anaconda3\envs\Microscope_Scan python
# -*- coding: UTF-8 -*-
"""
 @Author: Administrator
 @FileName: GlobalInterface.py
 @DateTime: 2025/6/21 22:18
 @SoftWare: PyCharm
 @Author  ：xsh
"""
from PyQt5 import Qt
from AppInterfaces.AppInterface import AppInterface
from AppUtils import AppUtils


class GlobalInterface(AppInterface):
    m_Control       : Qt.QObject = None
    m_ParentControl : Qt.QObject = None

    def isAberration(self, classifier: str) -> bool:
        return isinstance(classifier, str)  and classifier == 'Aberration'

    def isMicronucleus(self, classifier: str) -> bool:
        return isinstance(classifier, str)  and classifier == 'Micronucleus'

    def isKaryotyping(self, classifier: str) -> bool:
        return isinstance(classifier, str)  and classifier == 'Karyotyping'

    @property
    def hasControl       (self) -> bool:
        return hasattr(self, 'm_Control') and isinstance(self.m_Control       , (Qt.QWidget, Qt.QObject))
    @property
    def hasParentControl (self) -> bool:
        return hasattr(self, 'm_ParentControl') and isinstance(self.m_ParentControl , (Qt.QWidget, Qt.QObject))

    def Initialization(self, info: str, control: Qt.QObject, parent: Qt.QObject) -> None:
        self.Name = f"{self.__class__.__name__}"
        self.Information = info
        self.SetupUi(control, parent)
        self.initialize()

    def SetupUi(self, control: Qt.QObject, parent: Qt.QObject):
        self.m_Control = control
        self.m_ParentControl = parent
        if not isinstance(self.m_Control, Qt.QObject):
            return
        if self.m_Control.objectName() != self.Name:
            self.m_Control.setObjectName(self.Name)
        if self.m_Control.parent() != self.m_ParentControl and isinstance(self.m_ParentControl, Qt.QObject):
            self.m_Control.setParent(self.m_ParentControl)
        # Qt.QMetaObject.connectSlotsByName(self.m_Control)


    def InvokeMethod(self, method: str, **args):
        try:
            func = self.get_method(method)
            if func is not None:
                return func(**args)
            return None
        except Exception as e:
            AppUtils.print_Exception(e)
            return None


    def initParameter(self, **args):
        keys: list[str] = list(args.keys())
        for key, value in vars(self).items():
            if key in keys:
                setattr(self, key, args.get(key, None))

    @staticmethod
    def AppendTask(func, *args, **kwargs):
        if func is None:
            return
        from AppInterfaces.AppEventInterfaces.AppTask import AppTask
        try:
            task: AppTask = AppTask(target=func, args=args, kwargs=kwargs)
            Qt.QThreadPool.globalInstance().start(task)
        except Exception as e:
            AppUtils.print_Exception(e)