"""
Main Menu Screen
The primary menu interface for profile management
"""

from .base_screen import BaseScreen


class MainMenuScreen(BaseScreen):
    """Main menu screen implementation"""
    
    def __init__(self, data_manager, ui_renderer, screen_manager):
        super().__init__(data_manager, ui_renderer, screen_manager)
        self.menu_items = [
            "Create Profile",
            "Select Profile",
            "Edit Settings", 
            "Leaderboard",
            "Profile Stats",
            "Exit"
        ]
    
    def render(self) -> None:
        """Render main menu screen"""
        # Header
        active_profile = self.data_manager.active_profile
        subtitle = f"Active: {active_profile}" if active_profile else "No active profile"
        self.ui_renderer.draw_header("Profile Manager", subtitle)
        
        # API status
        api_status = "API: Connected" if self.data_manager.api_enabled else "API: Offline"
        api_color = 'light_green' if self.data_manager.api_enabled else 'red'
        self.ui_renderer.draw_text(api_status, 320, 80, api_color, 'tiny', center=True)
        
        # Device ID
        device_text = f"Device: {self.data_manager.device_id}"
        self.ui_renderer.draw_text(device_text, 320, 95, 'light_gray', 'tiny', center=True)
        
        # Draw menu panel
        self.ui_renderer.draw_panel(120, 120, 400, 240, 'dark_gray', 'gray')
        
        # Draw menu buttons
        for i, item in enumerate(self.menu_items):
            button_y = 135 + i * 38
            selected = (i == self.selected_index)
            
            # Gray out unavailable options
            disabled = False
            display_text = item
            
            if (item in ["Edit Settings", "Profile Stats"]) and not active_profile:
                disabled = True
                display_text = f"{item} (No Profile)"
            elif item == "Leaderboard" and not self.data_manager.api_enabled:
                disabled = True
                display_text = f"{item} (Offline)"
            
            self.ui_renderer.draw_button(display_text, 180, button_y, 280, 30, 
                                       selected, disabled)
        
        # Help text
        self.ui_renderer.draw_footer_help(self.get_help_text())
    
    def handle_input(self, action: str) -> bool:
        """Handle main menu input"""
        if action == 'up':
            self.navigate_up()
            return True
        elif action == 'down':
            self.navigate_down(len(self.menu_items) - 1)
            return True
        elif action == 'confirm':
            self.handle_confirm()
            return True
        elif action == 'back':
            # Exit application from main menu
            return False
        
        return False
    
    def handle_confirm(self) -> None:
        """Handle main menu selections"""
        index = self.selected_index
        
        if index == 0:  # Create Profile
            self.screen_manager.set_screen("create_profile")
        elif index == 1:  # Select Profile
            self.screen_manager.set_screen("profile_list")
        elif index == 2:  # Settings
            if self.data_manager.active_profile:
                self.screen_manager.set_screen("edit_profile")
        elif index == 3:  # Leaderboard
            if self.data_manager.api_enabled:
                self.screen_manager.set_screen("leaderboard")
        elif index == 4:  # Profile Stats
            if self.data_manager.active_profile:
                self.screen_manager.set_screen("profile_stats")
        elif index == 5:  # Exit
            return False
    
    def get_help_text(self) -> str:
        """Get help text for main menu"""
        return "↑↓ Navigate • Enter Confirm • Escape Exit"
