"""
System Tray Module.
===================

This module handles the application's persistent background presence.
It creates an icon in the Windows System Tray (Notification Area), allowing
interaction even when the main overlay is hidden.

Classes:
    SystemTray: QSystemTrayIcon implementation.
"""

from PySide6.QtWidgets import QSystemTrayIcon, QMenu
from PySide6.QtGui import QIcon
from PySide6.QtCore import Signal

class SystemTray(QSystemTrayIcon):
    """
    Manages the System Tray icon and menu.
    
    Signals:
        quit_requested: Emitted when the user selects 'Quit' from the menu.
    """
    quit_requested = Signal()
    settings_requested = Signal()
    transcribe_file_requested = Signal()

    def __init__(self, parent=None):
        super().__init__(parent)
        
        # Load Icon
        # Note: In a production build, this should be verified to exist.
        # If 'assets/icon.ico' is missing, Qt may show an empty space or default icon.
        self.setIcon(QIcon("assets/icon.ico")) 
        
        self.menu = QMenu(parent)
        self.menu.setStyleSheet("""
            QMenu {
                background-color: #1a1a20;
                color: #ffffff;
                border: 1px solid #40ffffff;
                border-radius: 8px;
                padding: 4px;
            }
            QMenu::item {
                padding: 8px 32px 8px 16px;
                border-radius: 4px;
                background-color: transparent;
                margin: 2px;
            }
            QMenu::item:selected {
                background-color: #00f2ff;
                color: #000000;
            }
            QMenu::separator {
                height: 1px;
                background: #40ffffff;
                margin: 4px 10px;
            }
        """)
        
        # Add Actions
        self.action_settings = self.menu.addAction("Settings")
        self.action_settings.triggered.connect(self.settings_requested.emit)
        
        self.action_transcribe = self.menu.addAction("Transcribe Audio File...")
        self.action_transcribe.triggered.connect(self.transcribe_file_requested.emit)
        
        self.menu.addSeparator()
        
        self.quit_action = self.menu.addAction("Quit Whisper Voice")
        self.quit_action.triggered.connect(self.quit_requested.emit)
        
        self.setContextMenu(self.menu)
        self.setToolTip("Whisper Voice (Ready) - Press F8 to Record")
        
        # Display the icon
        self.show()
