# Bryan Schumaker (11/26/2010)

import ocarina
from ocarina import body
from ocarina import source
import menu

gtk      = ocarina.gtk
sources  = ocarina.libsaria.sources
playlist = sources.playlist
visible  = playlist.is_visible

plist_page = source.Source()

def init():
	body.add_page("Playlist", plist_page)
	sources.init_src(playlist.init_bg, filler)

def filler():
	plist_page.init(filter, is_visible, right_click, playlist.play_id, reset)
	menu.add_plist_menu_item("Add to queue", add_selected_to_queue)
	menu.add_plist_menu_item("Remove from playlist", rm_selected_from_playlist)
	ocarina.libsaria.event.invite("POSTLOADPLAYLIST", refresh)
	plist_page.fill(playlist.walk, True)

def filter(text):
	playlist.filter(text)
	plist_page.refilter()
	plist_page.goto()

def is_visible(list, iter):
	return visible(list[iter][0])

def reset():
	plist_page.clear()
	playlist.reset()
	playlist.save()

def refresh(*args):
	plist_page.clear()
	plist_page.fill(playlist.walk)

def right_click(button, time):
	menu.make_plist_menu(button, time)

def rm_selected_from_playlist(menu):
	plist_page.for_each_selected_id(playlist.rm_id)
	playlist.save()
	refresh()

def add_selected_to_queue(menu):
	import queue
	plist_page.for_each_selected_id(sources.queue.add_id)
	queue.refresh()
