#!D:/IDE/anaconda3/envs python
# -*- coding: UTF-8 -*-
"""
 @Author: Administrator
 @FileName：ActionMenuBar.py\n
 @DateTime: 2025/7/16 18:09\n
 @Author  ：xsh
"""
from datetime import datetime, date, time
import sys
from PyQt5 import Qt
from AppCore.DBModels import *
from AppCore.Levels import *
from AppInterfaces.globalSlot import globalSlot, AppSlot
from utils import *
from AppInterfaces.UiInterface import UiInterface, AppUiProperty

class ActionMenuBar(Qt.QMenuBar, UiInterface):
    ActionSignal: Qt.pyqtSignal = Qt.pyqtSignal(object)
    Signals     : list[str]
    ContextMenu : Qt.QMenu
    ActionIcon  : Qt.QIcon
    DefaultIcon : str
    IsAvailable : bool
    def __init__(self, signals: list[str] = ['clicked'], parent: Qt.QWidget = None):
        super(ActionMenuBar, self).__init__(parent=parent)
        self.Signals = ['clicked'] if not isinstance(signals, (list, tuple)) else list(signals)
        self.Initialization('ActionMenuBar', self, parent)
        self.initMenuBar()

    def initValue(self):
        super(ActionMenuBar, self).initValue()
        self.IsAvailable = False
        self.initSignals     ()
        self.initDefaultIcon ()
        self.initActionIcon  ()
        self.initContextMenu ()



    def initContextMenu (self):
        if not self.hasContextMenu:
            self.ContextMenu = Qt.QMenu(self)

    def initMenuBar(self):
        if self.IsAvailable:
            self.clearMenuBar()
        self.updateMenuBar()


    def clearMenuBar(self):
        self.IsAvailable = False
        if not self.hasContextMenu:
            return
        actions: list[Qt.QAction] = self.ContextMenu.actions()
        for index, action in enumerate(actions):
            if not action:
                continue
            self.ContextMenu.removeAction(action)
            action.setVisible(False)
            action.deleteLater()



    def updateMenuBar(self):
        self.IsAvailable = False
        self.initContextMenu()
        self.initSignals()
        self.initActionIcon()
        TYPE: Qt.Qt.ConnectionType = Qt.Qt.ConnectionType(Qt.Qt.AutoConnection | Qt.Qt.UniqueConnection)
        action: Qt.QAction
        self.ContextMenu.addSeparator()
        for index, action_name in enumerate(self.Signals):
            action = Qt.QAction(icon=self.ActionIcon, text=f'{action_name}', parent=self)
            action.triggered.connect(slot=self.slot_action_signal, type=TYPE)
            self.ContextMenu.addAction(action)
        self.ContextMenu.addSeparator()
        self.IsAvailable = True

    def initSignals(self):
        if not self.hasSignals:
            self.Signals = ['clicked']


    def draw(self, pos):
        """显示右键菜单"""
        self.initContextMenu()

        self.ContextMenu.exec_(pos)

    def initActionIcon  (self):
        if not self.hasActionIcon :
            self.initDefaultIcon()
            self.ActionIcon  =  Qt.QIcon(Qt.QPixmap(self.DefaultIcon))

    def initDefaultIcon (self):
        if not self.hasDefaultIcon:
            import sys
            info: Qt.QFileInfo = Qt.QFileInfo(sys.argv[0])
            self.DefaultIcon = f'{info.absolutePath()}/Resource/Icon/Change.png'

    @property
    def hasContextMenu (self) -> bool:
        return hasattr(self, 'ContextMenu' ) and isinstance(self.ContextMenu, Qt.QMenu )

    @property
    def hasActionIcon  (self) -> bool:
        return hasattr(self, 'ActionIcon'  ) and isinstance(self.ActionIcon , Qt.QIcon )

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

    @property
    def hasAvailable(self) -> bool:
        return hasattr(self, 'IsAvailable') and isinstance(self.IsAvailable, bool)


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



    def slot_action_signal(self):
        if not self.hasControl:
            return
        action: Qt.QAction = self.m_Control.sender()
        if isinstance(action, Qt.QAction):
            self.ActionSignal.emit(action.text())

