"""
Edit Profile Screen
Interface for editing profile settings and configurations
"""

from .base_screen import BaseScreen
from profile_data import SettingsManager


class EditProfileScreen(BaseScreen):
    """Profile editing screen implementation"""
    
    def __init__(self, data_manager, ui_renderer, screen_manager):
        super().__init__(data_manager, ui_renderer, screen_manager)
        
        # Settings configuration
        self.settings = [
            ("Difficulty", "difficulty"),
            ("Sound Vol", "sound_volume"),
            ("Music Vol", "music_volume"),
            ("Screen Shake", "screen_shake"),
        ]
        self.max_index = len(self.settings) + 1  # +1 for save/back buttons
    
    def render(self) -> None:
        """Render profile editing screen"""
        active_profile = self.data_manager.get_active_profile()
        if not active_profile:
            self.screen_manager.set_screen("main_menu")
            return
        
        # Header with profile info
        subtitle = f"Games: {active_profile.games_played} • Score: {active_profile.best_score}"
        self.ui_renderer.draw_header(f"Edit: {active_profile.name}", subtitle)
        
        # Settings panel
        self.ui_renderer.draw_panel(60, 90, 520, 280, 'dark_gray', 'gray')
        self.ui_renderer.draw_text("Settings", 320, 105, 'white', 'large', center=True)
        
        # Settings items
        for i, (label, setting_key) in enumerate(self.settings):
            item_y = 130 + i * 35
            selected = (i == self.selected_index)
            
            if selected:
                self.ui_renderer.draw_panel(80, item_y, 480, 25, 'blue', 'light_blue')
                text_color = 'white'
                value_color = 'light_green'
                # Navigation arrows
                self.ui_renderer.draw_text("◄", 70, item_y + 5, 'white', 'small')
                self.ui_renderer.draw_text("►", 570, item_y + 5, 'white', 'small')
            else:
                text_color = 'light_gray'
                value_color = 'yellow'
            
            # Setting label and value
            self.ui_renderer.draw_text(label, 90, item_y + 5, text_color, 'medium')
            
            value = active_profile.settings[setting_key]
            display_value = SettingsManager.get_setting_display_value(setting_key, value)
            self.ui_renderer.draw_text(display_value, 500, item_y + 5, value_color, 'medium')
        
        # Action buttons
        save_selected = (self.selected_index == len(self.settings))
        back_selected = (self.selected_index == len(self.settings) + 1)
        
        self.ui_renderer.draw_button("Save", 200, 310, 80, 30, save_selected)
        self.ui_renderer.draw_button("← Back", 320, 310, 80, 30, back_selected)
        
        self.ui_renderer.draw_footer_help(self.get_help_text())
    
    def handle_input(self, action: str) -> bool:
        """Handle edit profile input"""
        max_index = len(self.settings) + 1  # settings + save + back
        
        if action == 'up':
            self.navigate_up()
            return True
        elif action == 'down':
            self.navigate_down(max_index)
            return True
        elif action == 'left':
            self.adjust_setting(-1)
            return True
        elif action == 'right':
            self.adjust_setting(1)
            return True
        elif action == 'confirm':
            self.handle_confirm()
            return True
        elif action == 'back':
            self.handle_back()
            return True
        
        return False
    
    def adjust_setting(self, direction: int) -> None:
        """Adjust setting value left/right"""
        if not self.data_manager.active_profile or self.selected_index >= len(self.settings):
            return
        
        profile = self.data_manager.get_active_profile()
        if not profile:
            return
        
        setting_label, setting_name = self.settings[self.selected_index]
        current_value = profile.settings[setting_name]
        
        # Adjust based on setting type
        if setting_name == "difficulty":
            new_value = SettingsManager.adjust_difficulty(current_value, direction)
        elif setting_name in ["sound_volume", "music_volume"]:
            new_value = SettingsManager.adjust_volume(current_value, direction)
        elif setting_name == "screen_shake":
            new_value = SettingsManager.toggle_boolean(current_value)
        else:
            return
        
        # Update the setting
        success, message = self.data_manager.update_profile_settings(
            self.data_manager.active_profile, setting_name, new_value
        )
        
        if not success:
            self.show_error(message)
    
    def handle_confirm(self) -> None:
        """Handle profile editing confirmation"""
        if self.selected_index == len(self.settings):  # Save
            self.screen_manager.set_screen("main_menu")
        elif self.selected_index == len(self.settings) + 1:  # Back
            self.screen_manager.set_screen("main_menu")
    
    def get_help_text(self) -> str:
        """Get help text for edit profile"""
        return "Left/Right: Adjust • Enter: Save/Back • Escape: Cancel"
