import tkinter as tk
from tkinter import messagebox, Listbox
from models.MusicPlayer import MusicPlayerModel

class MusicPlayerApp:
    def __init__(self, root):
        self.root = root
        self.root.title("Reproductor de Música")
        self.root.geometry("400x300")
        
        self.player = MusicPlayerModel()
        self.songs, self.message = self.player.load_songs()
        
        # Lista de canciones
        self.song_listbox = Listbox(self.root, bg="white", fg="black", font=("Arial", 12))
        self.song_listbox.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
        self.refresh_song_list()
        
        # Botones
        self.button_frame = tk.Frame(self.root)
        self.button_frame.pack(pady=10)
        
        self.play_button = tk.Button(self.button_frame, text="Reproducir", command=self.play_song, width=12)
        self.play_button.grid(row=0, column=0, padx=5)
        
        self.stop_button = tk.Button(self.button_frame, text="Detener", command=self.stop_song, width=12)
        self.stop_button.grid(row=0, column=1, padx=5)
        
        self.refresh_button = tk.Button(self.button_frame, text="Actualizar", command=self.refresh_song_list, width=12)
        self.refresh_button.grid(row=0, column=2, padx=5)

    def refresh_song_list(self):
        """Carga las canciones en el Listbox desde la carpeta resources."""
        self.songs, self.message = self.player.load_songs()
        self.song_listbox.delete(0, tk.END)
        if self.songs:
            for song in self.songs:
                self.song_listbox.insert(tk.END, song)
        else:
            messagebox.showinfo("Información", self.message)

    def play_song(self):
        """Reproduce la canción seleccionada."""
        selected = self.song_listbox.curselection()
        if not selected:
            messagebox.showwarning("Advertencia", "Selecciona una canción para reproducir.")
            return
        song_name = self.song_listbox.get(selected)
        message = self.player.play_song(song_name)
        messagebox.showinfo("Reproduciendo", message)
    
    def stop_song(self):
        """Detiene la reproducción."""
        message = self.player.stop_song()
        messagebox.showinfo("Detenido", message)

if __name__ == "__main__":
    root = tk.Tk()
    app = MusicPlayerApp(root)
    root.mainloop()
