import os
import pygame
from tkinter import *
from tkinter import filedialog

pygame.init()
pygame.mixer.init()

window = Tk()
window.title("🎵 PyJukebox")
window.geometry("620x720")
window.configure(bg="black")

letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K']
numbers = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']

selected_letter = ""
selected_number = ""

tracklist = []

def load_songs():
    global tracklist
    folder_selected = filedialog.askdirectory()
    tracklist = []
    for file in os.listdir(folder_selected):
        if file.endswith(".mp3"):
            tracklist.append(os.path.join(folder_selected, file))
    tracklist.sort()
    listbox.delete(0, END)
    for song in tracklist:
        listbox.insert(END, os.path.basename(song))

def play_song_by_index(index):
    if 0 <= index < len(tracklist):
        song = tracklist[index]
        pygame.mixer.music.load(song)
        pygame.mixer.music.play()
        now_playing.config(text=os.path.basename(song))
        listbox.selection_clear(0, END)
        listbox.select_set(index)
        listbox.activate(index)

def stop_song():
    pygame.mixer.music.stop()
    now_playing.config(text="")

def on_listbox_select(event):
    selected = listbox.curselection()
    if selected:
        play_song_by_index(selected[0])

def letter_clicked(ltr):
    global selected_letter
    selected_letter = ltr
    update_selection_display()
    try_play()

def number_clicked(num):
    global selected_number
    selected_number = num
    update_selection_display()
    try_play()

def update_selection_display():
    label_selection.config(text=f"Επιλογή: {selected_letter}{selected_number}")

def try_play():
    if selected_letter and selected_number:
        l_index = letters.index(selected_letter)
        # Μετατρέπουμε το "0" σε 9 (ώστε να είναι το 10ο τραγούδι)
        if selected_number == '0':
            n_index = 9
        else:
            n_index = int(selected_number) - 1
        index = l_index * 10 + n_index
        play_song_by_index(index)

        # Reset after play
        global selected_letter, selected_number
        selected_letter = ""
        selected_number = ""
        update_selection_display()

# GUI Εμφάνιση

now_playing = Label(window, text="Τίποτα δεν παίζει ακόμα", fg="lime", bg="black", font=("Courier", 14))
now_playing.pack(pady=10)

listbox = Listbox(window, bg="black", fg="white", width=70)
listbox.pack(pady=10)
listbox.bind('<<ListboxSelect>>', on_listbox_select)

Button(window, text="🎵 Φόρτωση Τραγουδιών", command=load_songs, bg="blue", fg="white").pack(pady=10)

control_frame = Frame(window, bg="black")
control_frame.pack(pady=10)

Button(control_frame, text="🔊 Start", width=12, command=lambda: play_song_by_index(0)).pack(side=LEFT, padx=10)
Button(control_frame, text="⏹️ Stop", width=12, command=stop_song).pack(side=LEFT, padx=10)

label_selection = Label(window, text="Επιλογή: ", fg="yellow", bg="black", font=("Courier", 16))
label_selection.pack(pady=15)

# Πληκτρολόγιο Jukebox
keyboard_frame = Frame(window, bg="black")
keyboard_frame.pack(pady=10)

# Κουμπιά Γραμμάτων
letter_frame = Frame(keyboard_frame, bg="black")
letter_frame.grid(row=0, column=0, pady=5)
for i, letter in enumerate(letters):
    Button(letter_frame, text=letter, width=4, height=2, bg="darkred", fg="white",
           command=lambda l=letter: letter_clicked(l)).grid(row=0, column=i, padx=3)

# Κουμπιά Αριθμών
number_frame = Frame(keyboard_frame, bg="black")
number_frame.grid(row=1, column=0, pady=5)
for i, number in enumerate(numbers):
    Button(number_frame, text=number, width=4, height=2, bg="darkgreen", fg="white",
           command=lambda n=number: number_clicked(n)).grid(row=0, column=i, padx=3)

window.mainloop()