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


class AppUiProperty:
    WindowIcon          : Qt.QIcon              = None
    Margin_Left         : int                   = None
    Margin_Top          : int                   = None
    Margin_Right        : int                   = None
    Margin_Bottom       : int                   = None
    Layout_Spacing      : int                   = None
    IsPress             : bool                  = None
    IsDoubleClick       : bool                  = None
    IsHover             : bool                  = None



class UiInterface(AppInterface, AppUiProperty):
    m_Control           : Qt.QWidget            = None
    m_PControl          : Qt.QWidget            = None


    def initWidgetStyle(self):
        from AppUtils.UiUtils import UiUtils
        self.setStyleSheet(UiUtils.getTransparentStyle())

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

    @property
    def hasPControl(self) -> bool:
        return hasattr(self, 'm_PControl') and isinstance(self.m_PControl, (Qt.QWidget, Qt.QObject))

    def Initialization(self, info: str, control: Qt.QWidget, parent: Qt.QWidget) -> None:
        self.m_Name = f"{self.__class__.__name__}"
        self.Information = info
        self.SetupUi(control, parent)
        if self.m_Control is not None:
            self.initialize()

    def SetupUi(self, control: Qt.QWidget, parent: Qt.QWidget):
        self.m_Control       = control
        self.m_PControl      = parent
        if not self.hasControl:
            return
        if isinstance(self.m_Name, str) and self.m_Control.objectName() != self.m_Name:
            self.m_Control.setObjectName(self.m_Name)
        self.m_Control.setWindowTitle(self.m_Name if not isinstance(self.Information, str) else self.Information)

        if self.m_Control.parent() != self.m_PControl and isinstance(self.m_PControl, Qt.QWidget):
            self.m_Control.setParent(self.m_PControl)
        # Qt.QMetaObject.connectSlotsByName(self.m_Control)

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

    def deinitialize   (self):
        self.deleConnect        ()
        self.deleProperty       ()
        self.deleValue          ()
        self.deleData           ()

    def initData            (self):
        super(UiInterface, self).initData()
        self.WindowIcon         = Qt.QIcon              ()
        self.Margin_Left         = 2
        self.Margin_Top          = 2
        self.Margin_Right        = 2
        self.Margin_Bottom       = 2
        self.Layout_Spacing      = 2
        self.IsPress             = False
        self.IsDoubleClick       = False
        self.IsHover             = False

    def deleData            (self):
        super(UiInterface, self).deleData()
        self.WindowIcon = None
        pass

    def initConnect    (self):
        super(UiInterface, self).initConnect()

    def deleConnect    (self):
        super(UiInterface, self).deleConnect()

    def initValue      (self):
        super(UiInterface, self).initValue()

    def deleValue      (self):
        super(UiInterface, self).deleValue()


    def initProperty   (self):
        super(UiInterface, self).initProperty()

    def deleProperty   (self):
        super(UiInterface, self).deleProperty()

    def initLayouts         (self):
        pass
    def initFrame           (self):
        pass

    def flushFrame          (self):
        pass

    def updateFrame         (self):
        pass

    @property
    def hasWindowLayouts(self) -> bool:
        return self.hasControl and self.m_Control.layout() is not None

    @property
    def has_WindowIcon(self):
        return hasattr(self, 'WindowIcon') and isinstance(self.WindowIcon, Qt.QIcon) and not self.WindowIcon.isNull()

    def initWindowIcon      (self):
        self.loadWindowIcon()
        if self.has_WindowIcon:
            if self.hasControl:
                self.m_Control.setWindowIcon(self.WindowIcon)
                if self.hasInformation:
                    self.m_Control.setWindowTitle(self.Information)

    def loadWindowIcon(self):
        if not self.has_WindowIcon:
            import sys
            info: Qt.QFileInfo = Qt.QFileInfo(f'{Qt.QFileInfo(sys.argv[0]).absolutePath()}/Resource/Icon/Change.png')
            self.WindowIcon = Qt.QIcon(Qt.QPixmap(info.absoluteFilePath()))