#!D:/IDE/anaconda3/envs/Microscope_Scan python
# -*- coding: UTF-8 -*-
"""
@FileName：ImageEvent.py
@Description：
@Author：XSH
@Time：2025/11/15 14:46
@Department：研发部
@Copyright：©2025 乐辰生物
"""
from PyQt5                                                  import Qt
from abc                                                    import abstractmethod
from AppInterfaces.utils                                    import *
from AppUtils import AppUtils
from AppSettings.Settings import *
from AppUtils.ImageUtils import ImageUtils
import numpy as np
import cv2
from AppCore.AppLevelModels.EventLevel import EventLevel

class ImageEvent(EventInterface):
    Image   : np.ndarray    #
    QImage  : Qt.QImage     #
    Data    : dict          #

    @classmethod
    def instance(cls, image, **args):
        item: cls = cls.__new__(cls)
        item.__init__()
        item.initEvent(image, **args)
        return item

    def __init__(self):
        super(ImageEvent, self).__init__()
        self.initialization('ImageEvent')

    def initValue(self):
        super(ImageEvent, self).initValue()
        self.Mode = EventLevel.OnlyDrawMode
        self.Image    = None
        self.QImage   = None
        self.Data     = None



    def initEvent(self, src=None, **args):
        if isinstance(src, np.ndarray):
            self.Image = src
        elif isinstance(src, Qt.QImage):
            self.QImage = Qt.QImage(src)
        self.Data = args



    def copyEvent(self):
        item = self.__class__.__new__(self.__class__)
        item.__init__()
        return item


    def isValid(self) -> bool:
        return self.hasImage or self.hasQImage


    def WaitDone(self, *args, **kwargs):
        from AppSettings.SystemSetting import SystemConfig
        try:
            if self.isValid() and self.isOnlyDrawMode:
                w: int
                h: int
                w, h = SystemConfig.image.DrawImageSize()
                image: Qt.QImage
                if self.hasQImage:
                    image = self.QImage.scaled(w, h, Qt.Qt.IgnoreAspectRatio, Qt.Qt.SmoothTransformation)
                elif self.hasImage:
                    image = ImageUtils.cv2_to_QImage(self.Image, w, h)
                else:
                    image = None
                if not isinstance(image, Qt.QImage) or image.isNull():
                    return RECODE_SUCCEED
                if HeadEndShowMode.get('DEBUG_SHOW', False):
                    AppSlot.CameraUiSignal.emit('OnlyDrawMode', image)
                    AppTimeLoop.processGlobalEvents(1000)
        except Exception as e:
            AppUtils.print_Exception(e)
        return RECODE_SUCCEED




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

    @property
    def hasQImage (self) -> bool:
        return hasattr(self, 'QImage') and isinstance(self.QImage, Qt.QImage )

    @property
    def hasData   (self) -> bool:
        return hasattr(self, 'Data'  ) and isinstance(self.Data  , dict      )


