"""
Menú para el procesador en tiempo real
"""

import logging
from .base_menu import BaseCommandProcessor

logger = logging.getLogger(__name__)

class RealtimeCommandProcessor(BaseCommandProcessor):
    def __init__(self, realtime_processor):
        super().__init__()
        self.processor = realtime_processor

    def _show_menu(self):
        """Muestra el menú de comandos disponibles"""
        estados = []
        if self.processor.verbose:
            estados.append("VERBOSE")
        if self.processor.debug:
            estados.append("DEBUG")
            
        print(f"\nEstado: {' | '.join(estados) or 'NORMAL'}")
        print(f"Entradas activas: {len(self.processor.entradas)}")
        print("\nComandos disponibles:")
        print("  X - Salir")
        print("  V - Toggle modo Verbose")
        print("  D - Toggle modo Debug")
        print("  S - Mostrar estado")
        print("  M - Mostrar mediciones")
        print("  L - Limpiar pantalla")

    def _process_command(self, cmd):
        """Procesa un comando individual"""
        if cmd == 'X':
            self.running = False
            self.processor.running = False
        elif cmd == 'V':
            self.processor.toggle_verbose()
        elif cmd == 'D':
            self.processor.toggle_debug()
        elif cmd == 'S':
            self._show_status()
        elif cmd == 'M':
            self._show_measurements()
        elif cmd == 'L':
            self._clear_screen()
        else:
            print("Comando no reconocido")

    def _show_status(self):
        """Muestra el estado actual del sistema"""
        print("\n=== Estado del Sistema ===")
        print(f"Último proceso: {self.processor.last_process_time}")
        print(f"Entradas activas: {len(self.processor.entradas)}")
        print("\nEstadísticas por entrada:")
        
        for dmac, entrada in self.processor.entradas.items():
            stats = entrada.get_estadisticas()
            if stats:
                print(f"\nEntrada {dmac}:")
                print(f"  Período: {stats['periodo_inicio']} - {stats['periodo_fin']}")
                print(f"  Mediciones: {self.processor.period_measurements.get(dmac, 0)}")
                if stats['promedio'] is not None:
                    print(f"  Promedio: {stats['promedio']}°C")

    def _show_measurements(self):
        """Muestra las mediciones actuales"""
        self.processor.show_measurement_status()