"""
Profile Stats Screen
Display detailed statistics for the active user profile
"""

from .base_screen import BaseScreen


class ProfileStatsScreen(BaseScreen):
    """Profile statistics display screen implementation"""
    
    def render(self) -> None:
        """Render detailed profile statistics"""
        if not self.data_manager.active_profile:
            self.screen_manager.set_screen("main_menu")
            return
        
        stats_data = self.data_manager.get_profile_stats(self.data_manager.active_profile)
        if not stats_data:
            self.screen_manager.set_screen("main_menu")
            return
        
        profile = stats_data['profile']
        
        # Header
        self.ui_renderer.draw_header(f"Stats: {profile.name}")
        
        # Stats panel
        self.ui_renderer.draw_panel(50, 90, 540, 260, 'dark_gray', 'gray')
        
        # Render statistics content
        self._render_local_statistics(stats_data, profile)
        self._render_server_status(stats_data)
        self._render_settings_summary(profile)
        
        # Back button
        back_selected = (self.selected_index == 0)
        self.ui_renderer.draw_button("← Back", 270, 370, 100, 30, back_selected)
        
        self.ui_renderer.draw_footer_help(self.get_help_text())
    
    def _render_local_statistics(self, stats_data, profile) -> None:
        """Render local profile statistics"""
        y = 110
        self.ui_renderer.draw_text("Local Statistics:", 60, y, 'light_blue', 'medium')
        y += 25
        
        stats = [
            f"Games Played: {profile.games_played}",
            f"Best Score: {profile.best_score}",
            f"Total Score: {profile.total_score}",
            f"Achievements: {len(profile.achievements)}",
            f"Created: {stats_data['created_formatted']}",
            f"Last Played: {stats_data['last_played_formatted']}"
        ]
        
        for stat in stats:
            self.ui_renderer.draw_text(stat, 70, y, 'light_gray', 'small')
            y += 20
    
    def _render_server_status(self, stats_data) -> None:
        """Render server connection and sync status"""
        y = 250
        
        if stats_data['api_enabled']:
            self.ui_renderer.draw_text("Server Status:", 60, y, 'light_green', 'medium')
            y += 20
            integration_info = stats_data['integration_info']
            device_id = integration_info.get('device_id', 'Unknown')
            self.ui_renderer.draw_text(f"Device ID: {device_id}", 70, y, 'light_gray', 'small')
            y += 20
            self.ui_renderer.draw_text("✓ Profile synced with server", 70, y, 'light_green', 'small')
        else:
            self.ui_renderer.draw_text("Server Status:", 60, y, 'red', 'medium')
            y += 20
            self.ui_renderer.draw_text("✗ Server offline", 70, y, 'red', 'small')
    
    def _render_settings_summary(self, profile) -> None:
        """Render current profile settings summary"""
        settings_x = 350
        y = 110
        self.ui_renderer.draw_text("Current Settings:", settings_x, y, 'light_blue', 'medium')
        y += 25
        
        settings_display = [
            f"Difficulty: {profile.settings.get('difficulty', 'normal').title()}",
            f"Sound Volume: {profile.settings.get('sound_volume', 50)}%",
            f"Music Volume: {profile.settings.get('music_volume', 50)}%",
            f"Screen Shake: {'On' if profile.settings.get('screen_shake', True) else 'Off'}"
        ]
        
        for setting in settings_display:
            self.ui_renderer.draw_text(setting, settings_x + 10, y, 'light_gray', 'small')
            y += 20
    
    def handle_input(self, action: str) -> bool:
        """Handle profile stats input"""
        if action == 'confirm' or action == 'back':
            self.handle_back()
            return True
        
        return False
    
    def get_help_text(self) -> str:
        """Get help text for profile stats"""
        return "Enter: Back • Escape: Back to Main Menu"
