#!D:/IDE/anaconda3/envs python
# -*- coding: UTF-8 -*-
"""
 @Author: Administrator
 @FileName：ClassifierContext.py\n
 @DateTime: 2025/7/4 13:44\n
 @Author  ：xsh
"""
from AppSettings.Settings import *
import sys
from PyQt5                              import Qt
from AppInterfaces.ModelInterface       import ContextModelInterface
from AppCore.Levels                     import *
from AppUtils.utils                     import *
from AppCore.Requests                   import *

class ClassifierContext(ContextModelInterface):
    Karyotyping                         : str
    Aberration                          : str
    Micronucleus                        : str
    Karyotyping_OrientedBoundingBox     : str
    Aberration_OrientedBoundingBox      : str
    Micronucleus_OrientedBoundingBox    : str
    OrientedBoundingBox                 : str
    Karyotyping_conf_threshold          : float
    Aberration_conf_threshold           : float
    Micronucleus_conf_threshold         : float
    OrientedBoundingBox_conf_threshold  : float
    Karyotyping_iou_threshold           : float
    Aberration_iou_threshold            : float
    Micronucleus_iou_threshold          : float
    OrientedBoundingBox_iou_threshold   : float
    __slots__ = (
        'Karyotyping'                         ,
        'Aberration'                          ,
        'Micronucleus'                        ,
        'Karyotyping_OrientedBoundingBox'     ,
        'Aberration_OrientedBoundingBox'      ,
        'Micronucleus_OrientedBoundingBox'    ,
        'OrientedBoundingBox'                 ,
        'Karyotyping_conf_threshold'          ,
        'Aberration_conf_threshold'           ,
        'Micronucleus_conf_threshold'         ,
        'OrientedBoundingBox_conf_threshold'  ,
        'Karyotyping_iou_threshold'           ,
        'Aberration_iou_threshold'            ,
        'Micronucleus_iou_threshold'          ,
        'OrientedBoundingBox_iou_threshold'   ,
    )
    __types__ = (
        str      ,
        str      ,
        str      ,
        str      ,
        str      ,
        str      ,
        str      ,
        float    ,
        float    ,
        float    ,
        float    ,
        float    ,
        float    ,
        float    ,
        float    ,
    )
    Detection_10X_Path: str
    def __init__(self, **args):
        super(ClassifierContext, self).__init__()
        self.initialization("算法文件路径上下文")
        self.initParameter(**args)

    def initParameter(self, **args):
        if not isinstance(args, dict) or len(args) <= 0:
            return
        for index, _property in enumerate(self.__slots__):
            setattr(self, _property, AppConvert.ObjectConvert(args.get(_property, getattr(self, _property, None)),
                                                              self.__types__[index]))




    def initValue(self):
        super(ClassifierContext, self).initValue()
        self.Karyotyping                            = "Resource/Classifier/Karyotyping_det_10X_hzh.onnx"
        self.Aberration                             = "Resource/Classifier/Aberration_det_10X_hzh.onnx"
        self.Micronucleus                           = "Resource/Classifier/Micronucleus_det_10X_hzh.onnx"
        self.OrientedBoundingBox                    = "Resource/Classifier/obb_960_100X_hzh.onnx"
        self.Karyotyping_OrientedBoundingBox        = "Resource/Classifier/Karyotyping_obb_960_100X_hzh.onnx"
        self.Aberration_OrientedBoundingBox         = "Resource/Classifier/Aberration_obb_960_100X_hzh.onnx"
        self.Micronucleus_OrientedBoundingBox       = "Resource/Classifier/Micronucleus_obb_960_100X_hzh.onnx"
        self.Karyotyping_conf_threshold             = 0.5
        self.Aberration_conf_threshold              = 0.5
        self.Micronucleus_conf_threshold            = 0.5
        self.OrientedBoundingBox_conf_threshold     = 0.5
        self.Karyotyping_iou_threshold              = 0.45
        self.Aberration_iou_threshold               = 0.45
        self.Micronucleus_iou_threshold             = 0.45
        self.OrientedBoundingBox_iou_threshold      = 0.45

    @property
    def absoluteDetection_Karyotyping(self) -> str:
        return "{0}/{1}".format(Qt.QFileInfo(sys.argv[0]).absolutePath(), self.Karyotyping)

    @property
    def absoluteDetection_Aberration(self) -> str:
        return "{0}/{1}".format(Qt.QFileInfo(sys.argv[0]).absolutePath(), self.Aberration)

    @property
    def absoluteDetection_Micronucleus(self) -> str:
        return "{0}/{1}".format(Qt.QFileInfo(sys.argv[0]).absolutePath(), self.Micronucleus)

    @property
    def absoluteDetection_OrientedBoundingBox(self) -> str:
        return "{0}/{1}".format(Qt.QFileInfo(sys.argv[0]).absolutePath(), self.OrientedBoundingBox)

    @property
    def absoluteDetection_Karyotyping_OrientedBoundingBox  (self) -> str:
        return "{0}/{1}".format(Qt.QFileInfo(sys.argv[0]).absolutePath(), self.Karyotyping_OrientedBoundingBox )

    @property
    def absoluteDetection_Aberration_OrientedBoundingBox   (self) -> str:
        return "{0}/{1}".format(Qt.QFileInfo(sys.argv[0]).absolutePath(), self.Aberration_OrientedBoundingBox  )

    @property
    def absoluteDetection_Micronucleus_OrientedBoundingBox (self) -> str:
        return "{0}/{1}".format(Qt.QFileInfo(sys.argv[0]).absolutePath(), self.Micronucleus_OrientedBoundingBox)


    def absoluteFilePath(self, filename: str):
        fileInfo: Qt.QFileInfo = Qt.QFileInfo(filename)
        if fileInfo.isRelative():
            return "{0}/{1}".format(Qt.QFileInfo(sys.argv[0]).absolutePath(), fileInfo.filePath())
        return fileInfo.absoluteFilePath()
        pass
    def absoluteOrientedBoundingBox(self) -> (str, float, float):
        return self.absoluteDetection_OrientedBoundingBox, self.OrientedBoundingBox_conf_threshold, self.OrientedBoundingBox_iou_threshold




    def getAbsoluteOrientedBoundingBox(self, level: str) -> (str, float, float):
        if level == 'Karyotyping':
            return self.absoluteDetection_Karyotyping_OrientedBoundingBox, self.OrientedBoundingBox_conf_threshold, self.OrientedBoundingBox_iou_threshold
        if level == 'Aberration':
            return self.absoluteDetection_Aberration_OrientedBoundingBox, self.OrientedBoundingBox_conf_threshold, self.OrientedBoundingBox_iou_threshold
        if level == 'Micronucleus':
            return self.absoluteDetection_Micronucleus_OrientedBoundingBox, self.OrientedBoundingBox_conf_threshold, self.OrientedBoundingBox_iou_threshold
        return self.absoluteDetection_OrientedBoundingBox, self.OrientedBoundingBox_conf_threshold, self.OrientedBoundingBox_iou_threshold


    def absoluteDetection(self, level: str) -> (str, float, float):
        if level == 'Karyotyping':
            return self.absoluteDetection_Karyotyping, self.Karyotyping_conf_threshold, self.Karyotyping_iou_threshold
        if level == 'Aberration':
            return self.absoluteDetection_Aberration, self.Aberration_conf_threshold, self.Aberration_iou_threshold
        if level == 'Micronucleus':
            return self.absoluteDetection_Micronucleus, self.Micronucleus_conf_threshold, self.Micronucleus_iou_threshold
        return self.absoluteDetection_Karyotyping, self.Karyotyping_conf_threshold, self.Karyotyping_iou_threshold

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

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

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