#!D:/IDE/anaconda3/envs python
# -*- coding: UTF-8 -*-
"""
 @Author: Administrator
 @FileName：HigObjectiveCellModel.py\n
 @DateTime: 2025/10/24 12:24\n
 @Author  ：xsh
"""
import numpy as np
import cv2
from PyQt5 import Qt
from datetime import datetime
from sqlalchemy.orm import (Mapped, mapped_column)
from sqlalchemy.types import (Text, DateTime)
from AppCore                                                .Levels import *
from AppSettings                                            .utils import *
from AppCore.AppDBModels.SlideModels.SlideModelInterface    import SlideModelInterface
from AppCore.AppDBModels.ConfigModels.ImageAnalysisModel    import ImageAnalysisModel
class HigObjectiveCellModel:
    CaseID              : int                           # 核型序号
    Classifier          : str                           # 模型名称
    IntegrationImages   : list[np.ndarray]              # 融合图像
    ExposureTimes       : list[int]                     # 曝光时间
    Image               : np.ndarray                    # 原始图像
    ImageParameter      : ImageAnalysisModel            # 图像参数
    Cell                : SlideModelInterface           # 数据模型
    Cases               : list[SlideModelInterface]     # 核型列表
    def __init__(self):
        super(HigObjectiveCellModel, self).__init__()
        self.CaseID               = None
        self.Classifier           = None
        self.IntegrationImages    = None
        self.ExposureTimes        = None
        self.Image                = None
        self.ImageParameter       = None
        self.Cell                 = None
        self.Cases                = None

    def __del__(self):
        self.CaseID               = None
        self.Classifier           = None
        self.IntegrationImages    = None
        self.ExposureTimes        = None
        self.Image                = None
        self.ImageParameter       = None
        self.Cell                 = None
        self.Cases                = None

    def deleteLater(self):
        self.CaseID               = None
        self.Classifier           = None
        try:
            del self.IntegrationImages
            del self.Image
        except Exception as e:
            print(e)
        self.IntegrationImages    = None
        self.ExposureTimes        = None
        self.Image                = None
        self.ImageParameter       = None
        self.Cell                 = None
        self.Cases                = None

    @classmethod
    def instance(cls, classifier: str, model: SlideModelInterface, image: np.ndarray, cases: list[SlideModelInterface], exposureTimes: list, **args):
        item: cls = cls.__new__(cls)
        item.__init__()
        Classifier          : str                          = classifier         if isinstance(classifier    , str                   ) else None
        ExposureTimes       : list[int]                    = exposureTimes      if isinstance(exposureTimes , (list, tuple)         ) else None
        Image               : np.ndarray                   = image.copy()       if isinstance(image         , np.ndarray            ) else None
        Cell                : SlideModelInterface          = model              if isinstance(model         , SlideModelInterface   ) else None
        Cases               : list[SlideModelInterface]    = cases              if isinstance(cases         , (tuple, list)         ) else None
        IntegrationImages   : list[np.ndarray]             = args.get('IntegrationImages'   , None)
        ImageParameter      : ImageAnalysisModel           = args.get('imageParameter'      , None)
        item.Classifier           = Classifier          if isinstance(Classifier          , str                       ) else None
        item.IntegrationImages    = IntegrationImages   if isinstance(IntegrationImages   , list                      ) else None
        item.ExposureTimes        = ExposureTimes       if isinstance(ExposureTimes       , list                      ) else None
        item.Image                = Image               if isinstance(Image               , np.ndarray                ) else None
        item.ImageParameter       = ImageParameter      if isinstance(ImageParameter      , ImageAnalysisModel        ) else None
        item.Cell                 = Cell                if isinstance(Cell                , SlideModelInterface       ) else None
        item.Cases                = Cases               if isinstance(Cases               , list                      ) else None
        item.CaseID               = len(item.Cases) + 1 if item.hasCases else None
        return item

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


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


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

    @property
    def isAvailable(self) -> bool:
        return self.hasCell and self.hasClassifier and self.hasCell and self.hasIntegrationImages and self.hasExposureTimes










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

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

    @property
    def hasIntegrationImages    (self) -> bool:
        return hasattr(self, 'IntegrationImages') and isinstance(self.IntegrationImages    , (tuple, list)            )

    @property
    def hasExposureTimes        (self) -> bool:
        return hasattr(self, 'ExposureTimes'    ) and isinstance(self.ExposureTimes        , (tuple, list)            )

    @property
    def hasImage                (self) -> bool:
        return hasattr(self, 'Image'            ) and isinstance(self.Image                , np.ndarray               )

    @property
    def hasImageParameter       (self) -> bool:
        return hasattr(self, 'ImageParameter'   ) and isinstance(self.ImageParameter       , ImageAnalysisModel       )

    @property
    def hasCell                 (self) -> bool:
        return hasattr(self, 'Cell'             ) and isinstance(self.Cell                 , SlideModelInterface      )

    @property
    def hasCases                (self) -> bool:
        return hasattr(self, 'Cases'            ) and isinstance(self.Cases                , list                     )



