# Bryan Schumaker (11/26/2010)

import libsaria
import sources
import body

playlist = libsaria.sources.playlist
playlist_page = sources.Source()
PLAYLIST_PAGE = body.add_page(playlist_page, "Playlist")
PLAYLIST_PAGE.goto = playlist_page.goto

def set_label_text():
	PLAYLIST_PAGE.label.set_text("Playlist (%s)" % playlist.num_visible())

def fill_playlist():
	songs = playlist.walk_playlist("id", "title", "lenstr", "artist", "album", "year")
	playlist_page.insert(songs)
	set_label_text()
fill_playlist()

def refresh():
	playlist_page.clear()
	fill_playlist()

def do_refilter(list, iter):
	return playlist.is_visible(list[iter][0])
playlist_page.set_filter_func(do_refilter)

def filter(text):
	playlist.filter(text)
	playlist_page.refilter()
	playlist_page.goto()
	set_label_text()
PLAYLIST_PAGE.filter = filter

def clear():
	playlist_page.clear()
	playlist.reset()
	playlist.save()
	refresh()
PLAYLIST_PAGE.clear = clear

def add_to_playlist(menu):
	ids = menu.source.get_selected_ids()
	playlist.add_ids(ids)
	refresh()

def rm_from_playlist(menu):
	ids = menu.source.get_selected_ids()
	playlist.rm_ids(ids)
	refresh()

import queue
menu_items = [
	("Add to Queue", queue.add_to_queue),
	("Remove from Playlist", rm_from_playlist),
]
def show_menu(event):
	m = sources.Menu(playlist_page, menu_items)
	m.show(event)
playlist_page.set_right_click(show_menu)
