#!D:/IDE/anaconda3/envs python
# -*- coding: UTF-8 -*-
"""
 @Author: Administrator
 @FileName：AlgorithmModelInterface.py\n
 @DateTime: 2025/7/21 14:28\n
 @Author  ：xsh
"""
from PyQt5 import Qt
from abc import abstractmethod
import      argparse
import      os
import      shutil
import      cv2
import      time
import numpy            as np
import onnxruntime      as ort
from PyQt5              import Qt
from abc                import abstractmethod
from AppUtils import AppUtils
from tqdm               import tqdm
from PIL                import Image
from scipy.spatial      import KDTree
from tqdm               import tqdm
class AlgorithmModelInterface:
    Name            : str
    Information     : str
    m_Control       : Qt.QObject
    m_ParentControl : Qt.QObject
    classes         : list[str]  = [str(i) for i in range(1, 25)]   #
    class_cont      : float                                         #
    iou_cont        : float                                         #
    conf_threshold  : float                                         #
    iou_threshold   : float                                         #
    model_height    : int                                           #
    model_width     : int                                           #
    dtype           : type                                          #
    session         : object                                        #
    level           : str                                           #
    NextModel       : 'AlgorithmModelInterface'                     #
    LastModel       : 'AlgorithmModelInterface'                     #

    @abstractmethod
    def loadModel(self):
        pass

    @abstractmethod
    def unloadModel(self):
        pass

    @abstractmethod
    def process(self, level, image, *args, **kwargs):
        pass



    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)

    def initialize(self):
        self.initData()
        self.initValue()
        self.initProperty()
        self.initConnect()

    def initData(self):
        pass

    def initValue(self):
        pass

    def initProperty(self):
        pass

    def initConnect(self):
        pass

    @property
    def hasNextModel(self) -> bool:
        return hasattr(self, 'NextModel') and isinstance(self.NextModel, AlgorithmModelInterface)

    @property
    def hasLastModel(self) -> bool:
        return hasattr(self, 'LastModel') and isinstance(self.LastModel, AlgorithmModelInterface)


    @property
    def next_model(self) -> 'AlgorithmModelInterface':
        return None if not self.hasNextModel else self.NextModel

    @property
    def last_model(self) -> 'AlgorithmModelInterface':
        return None if not self.hasLastModel else self.LastModel


    def isAberration(self) -> bool:
        return self.has_level and self.level == 'Aberration'

    def isMicronucleus(self) -> bool:
        return self.has_level and self.level == 'Micronucleus'

    def isKaryotyping(self) -> bool:
        return self.has_level and self.level == 'Karyotyping'


    def isValid(self) -> bool:
        return self.has_level and self.has_session and self.has_dtype and self.has_conf_threshold and self.has_iou_threshold

    @property
    def isAvailable(self) -> bool:
        return self.has_level and self.has_session and self.has_dtype

    @property
    def has_conf_threshold(self) -> bool:
        return hasattr(self, 'conf_threshold') and isinstance(self.conf_threshold, float)

    @property
    def has_iou_threshold(self) -> bool:
        return hasattr(self, 'iou_threshold') and isinstance(self.iou_threshold, float)

    @property
    def has_model_height(self) -> bool:
        return hasattr(self, 'model_height') and isinstance(self.model_height, int)

    @property
    def has_model_height(self) -> bool:
        return hasattr(self, 'model_height') and isinstance(self.model_height, int)

    @property
    def has_dtype(self) -> bool:
        return hasattr(self, 'dtype') and isinstance(self.dtype, type)

    @property
    def has_session(self) -> bool:
        return hasattr(self, 'session') and isinstance(self.session, ort.InferenceSession)

    @property
    def has_level(self) -> bool:
        return hasattr(self, 'level') and isinstance(self.level, str)

    @property
    def has_NextModel(self) -> bool:
        return hasattr(self, 'NextModel') and isinstance(self.NextModel, AlgorithmModelInterface)

    @property
    def has_LastModel(self) -> bool:
        return hasattr(self, 'LastModel') and isinstance(self.LastModel, AlgorithmModelInterface)

