"""
Profile List Screen
Display and select from existing user profiles
"""

from .base_screen import BaseScreen


class ProfileListScreen(BaseScreen):
    """Profile list selection screen implementation"""
    
    def render(self) -> None:
        """Render profile selection screen"""
        self.ui_renderer.draw_header("Select Profile")
        
        profile_names = self.data_manager.get_profile_list()
        
        if not profile_names:
            # No profiles message
            self.ui_renderer.draw_panel(120, 100, 400, 150, 'dark_gray', 'gray')
            self.ui_renderer.draw_text("No profiles found", 320, 150, 'yellow', 'large', center=True)
            self.ui_renderer.draw_text("Create one first", 320, 180, 'light_gray', 'medium', center=True)
            self.ui_renderer.draw_button("← Back", 270, 300, 100, 30, True)
        else:
            # Profile list
            panel_height = min(280, len(profile_names) * 55 + 60)
            self.ui_renderer.draw_panel(30, 80, 580, panel_height, 'black', 'gray')
            
            for i, name in enumerate(profile_names):
                profile = self.data_manager.get_profile(name)
                entry_y = 95 + i * 50
                selected = (i == self.selected_index)
                
                # Profile stats
                stats_text = f"Games: {profile.games_played} • Score: {profile.best_score}"
                indicator = "★" if name == self.data_manager.active_profile else ""
                
                self.ui_renderer.draw_list_item(name, 40, entry_y, 560, 40, 
                                               selected, stats_text, indicator)
            
            # Back button
            back_y = 95 + len(profile_names) * 50 + 10
            back_selected = (self.selected_index == len(profile_names))
            self.ui_renderer.draw_button("← Back", 270, back_y, 100, 30, back_selected)
        
        self.ui_renderer.draw_footer_help(self.get_help_text())
    
    def handle_input(self, action: str) -> bool:
        """Handle profile list input"""
        profile_names = self.data_manager.get_profile_list()
        max_index = len(profile_names)  # Includes back button
        
        if action == 'up':
            self.navigate_up()
            return True
        elif action == 'down':
            self.navigate_down(max_index)
            return True
        elif action == 'confirm':
            self.handle_confirm()
            return True
        elif action == 'back':
            self.handle_back()
            return True
        elif action == 'delete':
            self.handle_delete()
            return True
        
        return False
    
    def handle_confirm(self) -> None:
        """Handle profile list selections"""
        profile_names = self.data_manager.get_profile_list()
        
        if self.selected_index < len(profile_names):
            # Select profile
            profile_name = profile_names[self.selected_index]
            success, message = self.data_manager.set_active_profile(profile_name)
            if success:
                self.screen_manager.set_screen("main_menu")
            else:
                self.show_error(message)
        else:
            # Back option
            self.handle_back()
    
    def handle_delete(self) -> None:
        """Handle profile deletion"""
        profile_names = self.data_manager.get_profile_list()
        
        if self.selected_index < len(profile_names):
            profile_name = profile_names[self.selected_index]
            success, message = self.data_manager.delete_profile(profile_name)
            if not success:
                self.show_error(message)
            else:
                # Adjust selection if needed
                max_index = max(0, len(self.data_manager.get_profile_list()) - 1)
                self.selected_index = min(self.selected_index, max_index)
    
    def get_help_text(self) -> str:
        """Get help text for profile list"""
        return "Enter: Select • Escape: Back • Delete: Remove"
