"""
Base Screen Class
Common interface and functionality for all screens in the Profile Manager
"""

from abc import ABC, abstractmethod
from typing import Dict, Any, Optional, Tuple


class BaseScreen(ABC):
    """Base class for all profile manager screens"""
    
    def __init__(self, data_manager, ui_renderer, screen_manager):
        """
        Initialize base screen
        
        Args:
            data_manager: ProfileDataManager instance
            ui_renderer: UIRenderer instance
            screen_manager: ScreenManager instance
        """
        self.data_manager = data_manager
        self.ui_renderer = ui_renderer
        self.screen_manager = screen_manager
        
        # Screen-specific state
        self.selected_index = 0
        self.screen_state = {}
    
    @abstractmethod
    def render(self) -> None:
        """Render the screen content"""
        pass
    
    @abstractmethod
    def handle_input(self, action: str) -> bool:
        """
        Handle input for this screen
        
        Args:
            action: Input action string ('up', 'down', 'left', 'right', 'confirm', 'back', 'delete')
        
        Returns:
            bool: True if input was handled, False otherwise
        """
        pass
    
    def navigate_up(self) -> None:
        """Navigate selection up"""
        if self.selected_index > 0:
            self.selected_index -= 1
    
    def navigate_down(self, max_index: int) -> None:
        """Navigate selection down"""
        if self.selected_index < max_index:
            self.selected_index += 1
    
    def navigate_left(self) -> None:
        """Navigate left (screen-specific implementation)"""
        pass
    
    def navigate_right(self) -> None:
        """Navigate right (screen-specific implementation)"""
        pass
    
    def handle_confirm(self) -> None:
        """Handle confirm action (screen-specific implementation)"""
        pass
    
    def handle_back(self) -> None:
        """Handle back action (default: return to main menu)"""
        self.screen_manager.set_screen("main_menu")
    
    def handle_delete(self) -> None:
        """Handle delete action (screen-specific implementation)"""
        pass
    
    def show_error(self, message: str) -> None:
        """Show error message through the screen manager"""
        # Delegate to screen manager
        if hasattr(self.screen_manager, 'show_error_dialog'):
            self.screen_manager.show_error_dialog(message)
    
    def reset_state(self) -> None:
        """Reset screen state when entering"""
        self.selected_index = 0
        self.screen_state.clear()
    
    def get_help_text(self) -> str:
        """Get help text for this screen"""
        return "↑↓ Navigate • Enter Confirm • Escape Back"
