import time
import pygame
from waiter_data import WaiterDataProvider

class Stadistics:

    def __init__(self, screen, waiters):
        self.screen = screen
        self.last_surface = None
        self.waiters = waiters
        self._last_update = 0
        self._last_names = []
        self._last_loads = []
        

    def draw_waiter_loads(self, pos=(0,0)):
        now = time.time()
        names = []
        loads = []
        if now - self._last_update > 0.5:
            for waiter in self.waiters:
                names.append(waiter.name)
                loads.append(waiter.load)
            if names:
                self._last_names = names
                self._last_loads = loads
            self._last_update = now
        # Usar los últimos datos obtenidos
        if self._last_names and self._last_loads:
            # Dibujar barras simples con pygame
            bar_width = 20
            bar_gap = 12
            max_height = 80
            max_load = max(self._last_loads) if self._last_loads else 1
            x, y = pos
            font = pygame.font.SysFont(None, 18)
            for i, (name, load) in enumerate(zip(self._last_names, self._last_loads)):
                height = int((load / max_load) * max_height) if max_load > 0 else 0
                bar_x = x + i * (bar_width + bar_gap)
                bar_y = y + max_height - height
                pygame.draw.rect(self.screen, (100, 180, 255), (bar_x, bar_y, bar_width, height))
                # Nombre debajo
                text = font.render(name[0], True, (0, 0, 0))
                text_rect = text.get_rect(center=(bar_x + bar_width // 2, y + max_height + 10))
                self.screen.blit(text, text_rect)
                # Valor encima
                value_text = font.render(str(round(load, 1)), True, (0, 0, 0))
                value_rect = value_text.get_rect(center=(bar_x + bar_width // 2, bar_y - 10))
                self.screen.blit(value_text, value_rect)

    def draw_waiter_waiting_times(self, pos=(0, 120)):
        """
        Dibuja un gráfico de barras con el waiting time de cada camarero.
        Se asume que cada objeto waiter tiene un atributo 'waiting_time' (float o int).
        """
        names = []
        waiting_times = []
        for waiter in self.waiters:
            names.append(waiter.name)
            waiting_times.append(getattr(waiter, 'waiting_time', 0))
        if names and waiting_times:
            bar_width = 20
            bar_gap = 12
            max_height = 80
            max_time = max(waiting_times) if waiting_times else 1
            x, y = pos
            font = pygame.font.SysFont(None, 18)
            for i, (name, w_time) in enumerate(zip(names, waiting_times)):
                height = int((w_time / max_time) * max_height) if max_time > 0 else 0
                bar_x = x + i * (bar_width + bar_gap)
                bar_y = y + max_height - height
                pygame.draw.rect(self.screen, (255, 180, 100), (bar_x, bar_y, bar_width, height))
                # Nombre debajo
                text = font.render(name[0], True, (0, 0, 0))
                text_rect = text.get_rect(center=(bar_x + bar_width // 2, y + max_height + 10))
                self.screen.blit(text, text_rect)
                # Valor encima
                value_text = font.render(str(round(w_time, 1)), True, (0, 0, 0))
                value_rect = value_text.get_rect(center=(bar_x + bar_width // 2, bar_y - 10))
                self.screen.blit(value_text, value_rect)

    
    def draw_avg_order_prep_time(self, order_data_provider, pos=(0, 220)):
        """
        Dibuja el tiempo medio de preparación de las órdenes en pantalla.
        order_data_provider debe tener el atributo order_prep_times (lista de floats).
        """
        if len(order_data_provider.order_prep_times) == 0:
            avg_prep_time = 0.0
        else:
            avg_prep_time = sum(order_data_provider.order_prep_times) / len(order_data_provider.order_prep_times)

        x, y = pos
        font = pygame.font.SysFont(None, 22)
        text = font.render(f"Tiempo medio preparación: {avg_prep_time:.1f} s", True, (50, 50, 50))
        self.screen.blit(text, (x, y))