import psutil
import threading
import tkinter as tk

class SystemMonitor:
    def __init__(self, parent, stop_event):
        """Muestra el uso del CPU, RAM y red en tiempo real."""
        self.parent = parent
        self.stop_event = stop_event

        # Etiquetas para mostrar métricas
        self.cpu_label = tk.Label(parent, text="CPU: 0%", bg="lightgreen")
        self.ram_label = tk.Label(parent, text="RAM: 0%", bg="lightcoral")
        self.cpu_label.pack(side="left", expand=True)
        self.ram_label.pack(side="left", expand=True)

        # Hilos para actualizar los datos
        threading.Thread(target=self.update_cpu, daemon=True).start()
        threading.Thread(target=self.update_ram, daemon=True).start()

    def update_cpu(self):
        """Actualiza la etiqueta del uso de CPU."""
        while not self.stop_event.is_set():
            cpu_usage = psutil.cpu_percent()
            self.cpu_label.config(text=f"CPU: {cpu_usage}%")
            self.stop_event.wait(1)

    def update_ram(self):
        """Actualiza la etiqueta del uso de RAM."""
        while not self.stop_event.is_set():
            ram_usage = psutil.virtual_memory().percent
            self.ram_label.config(text=f"RAM: {ram_usage}%")
            self.stop_event.wait(1)
