# Bryan Schumaker (12/12/2010)

import libsaria
import sources
import body

queue = libsaria.sources.queue
queue_page = sources.Source()
QUEUE_PAGE = body.add_page(queue_page, "Queue")
QUEUE_PAGE.goto = queue_page.goto

def set_label_text():
	QUEUE_PAGE.label.set_text("Queue (%s)" % queue.num_visible())

def set_visible():
	if queue.num_visible() == 0:
		body.hide_page(QUEUE_PAGE)
	else:
		body.show_page(QUEUE_PAGE)

def fill_queue():
	songs = libsaria.sources.list_queue(*sources.column.attrs)
	queue_page.insert(songs)
	set_label_text()
	set_visible()
fill_queue()

def refresh():
	queue_page.clear()
	fill_queue()

def refilter():
	queue_page.refilter()
	set_label_text()

def clear():
	queue_page.clear()
	queue.reset()
	refresh()
QUEUE_PAGE.clear = clear

def add_to_queue(menu):
	ids = menu.source.get_selected_ids()
	queue.add_ids(ids)
	refresh()

def rm_from_queue(menu):
	ids = menu.source.get_selected_ids()
	queue.rm_ids(ids)
	refresh()

menu_items = [
	("Remove from Queue", rm_from_queue),
]
def show_menu(event):
	m = sources.Menu(queue_page, menu_items)
	m.show(event)
queue_page.set_right_click(show_menu)
