import tkinter as tk  # Importar Tkinter para la interfaz gráfica
from tkinter import ttk, scrolledtext  # Importar widgets específicos de Tkinter


class MainView:
    def __init__(self, root):
        self.root = root
        self.root.title("Ventana Responsive")
        self.root.geometry("1000x700")  # Tamaño inicial

        # Configuración de la ventana principal
        self.root.columnconfigure(1, weight=1)  # Columna central ajustable
        self.root.rowconfigure(0, weight=1)  # Fila principal ajustable
        self.root.rowconfigure(1, weight=0)  # Fila inferior (barra de estado)

        # Crear frames principales
        self.frame_left = tk.Frame(self.root, bg="lightblue", width=200)
        self.frame_center = tk.Frame(self.root, bg="white", height=400, width=600)
        self.frame_bottom = tk.Frame(self.root, bg="lightgray", height=50)
        self.frame_right = tk.Frame(self.root, bg="red", width=200)

        # Configurar posiciones en el grid
        self.frame_left.grid(row=0, column=0, sticky="ns")
        self.frame_center.grid(row=0, column=1, padx=10, pady=10, sticky="nsew")
        self.frame_right.grid(row=0, column=2, sticky="ns")
        self.frame_bottom.grid(row=1, column=0, columnspan=3, sticky="ew")

        # Configurar tamaños fijos
        self.frame_left.grid_propagate(False)
        self.frame_center.grid_propagate(False)
        self.frame_bottom.grid_propagate(False)
        self.frame_right.grid_propagate(False)

        # Crear el notebook dentro del frame central
        self.create_tabs()

        # Crear barra de estado en el frame inferior
        self.create_status_bar()
        
        self.create_music_player()

    def create_tabs(self):
        """Crea las solapas dentro del notebook."""
        self.notebook = ttk.Notebook(self.frame_center, height=800, width=800)
        self.notebook.pack(padx=10, pady=10)

        # Solapa 1: IP Tracker
        tab1 = ttk.Frame(self.notebook)
        self.notebook.add(tab1, text="IP Tracker")
        self.create_tracker_ui(tab1)

        # Solapa 2: Escaneo de red
        tab2 = ttk.Frame(self.notebook)
        self.notebook.add(tab2, text="Escaneo de Red")
        self.create_network_scan_ui(tab2)
        
        # Solapa 3: Sniffer
        tab3 = ttk.Frame(self.notebook)
        self.notebook.add(tab3, text="Sniffer")
        self.create_sniffer_ui(tab3)

        # Solapas 4-5: Vacías
        for i in range(4, 6):
            tab = ttk.Frame(self.notebook)
            self.notebook.add(tab, text=f"Solapa {i}")
            label = ttk.Label(tab, text=f"Contenido de la Solapa {i}")
            label.pack(pady=10)

    def create_tracker_ui(self, tab):
        """Crea la interfaz de usuario para el rastreador de IP en la solapa 1."""
        label_ip = ttk.Label(tab, text="Introduce la IP para rastrear:")
        label_ip.pack(pady=5)

        self.entry_ip = ttk.Entry(tab, width=40)
        self.entry_ip.pack(pady=5)

        self.text_output = scrolledtext.ScrolledText(tab, wrap=tk.WORD, height=10, width=60)
        self.text_output.pack(fill="both", expand=True, pady=10)

        self.button_track_ip = ttk.Button(tab, text="Rastrear IP")
        self.button_track_ip.pack(pady=5)
        
    def create_network_scan_ui(self, tab):
        """Crea la interfaz de usuario para el escaneo de red en la Solapa 2."""
        label = ttk.Label(tab, text="Escaneo de dispositivos en la red:")
        label.pack(pady=5)

        self.text_network_scan = scrolledtext.ScrolledText(tab, wrap=tk.WORD, height=20, width=80)
        self.text_network_scan.pack(fill="both", expand=True, pady=10)

        # Botón para iniciar el escaneo
        self.button_scan_network = ttk.Button(tab, text="Iniciar Escaneo")
        self.button_scan_network.pack(pady=5)
        
    def create_sniffer_ui(self, tab):
        """Crea la interfaz de usuario para el sniffer en la Solapa 3."""
        label = ttk.Label(tab, text="Sniffer de paquetes:")
        label.pack(pady=5)

        self.text_sniffer_output = scrolledtext.ScrolledText(tab, wrap=tk.WORD, height=20, width=80)
        self.text_sniffer_output.pack(fill="both", expand=True, pady=10)

        self.button_start_sniffer = ttk.Button(tab, text="Iniciar Sniffer")
        self.button_start_sniffer.pack(side="left", padx=5)

        self.button_stop_sniffer = ttk.Button(tab, text="Detener Sniffer", state="disabled")
        self.button_stop_sniffer.pack(side="left", padx=5)

    

    def create_status_bar(self):
        """Crea una barra de estado en la parte inferior."""
        self.label_cpu = tk.Label(self.frame_bottom, text="Uso CPU: 0%", bg="orange", anchor="w", width=20)
        self.label_bytes_recv = tk.Label(self.frame_bottom, text="Descarga: 0 KB/s", bg="blue", anchor="w", width=20)
        self.label_bytes_sent = tk.Label(self.frame_bottom, text="Subida: 0 KB/s", bg="cyan", anchor="w", width=20)
        self.label_uso_memoria = tk.Label(self.frame_bottom, text="Uso RAM: 0%", bg="pink", anchor="w", width=20)
        self.label_fecha_hora = tk.Label(
            self.frame_bottom, text="Hilo fecha-hora", font=("Helvetica", 14), fg="blue", relief="sunken", anchor="w", width=20
        )

        self.label_cpu.pack(side="left", fill="x", expand=True)
        self.label_bytes_recv.pack(side="left", fill="x", expand=True)
        self.label_bytes_sent.pack(side="left", fill="x", expand=True)
        self.label_uso_memoria.pack(side="left", fill="x", expand=True)
        self.label_fecha_hora.pack(side="right", fill="x", expand=True)


    def create_music_player(self):
        """Crea el reproductor de música en el frame derecho."""
        label = tk.Label(self.frame_right, text="Reproductor de Música", bg="red", fg="white", font=("Arial", 12))
        label.pack(pady=10)

        # Lista de canciones
        self.music_listbox = tk.Listbox(self.frame_right, height=15, width=25)
        self.music_listbox.pack(pady=5)

        # Botones de control organizados verticalmente
        self.button_play_music = ttk.Button(self.frame_right, text="Reproducir")
        self.button_play_music.pack(pady=5, fill="x")

        self.button_stop_music = ttk.Button(self.frame_right, text="Detener")
        self.button_stop_music.pack(pady=5, fill="x")

        self.button_refresh_music = ttk.Button(self.frame_right, text="Actualizar")
        self.button_refresh_music.pack(pady=5, fill="x")
        
        
    def populate_music_player(self, songs):
        """Llena la lista de canciones en el reproductor."""
        self.music_listbox.delete(0, tk.END)
        if songs:
            for song in songs:
                self.music_listbox.insert(tk.END, song)