# Bryan Schumaker (12/12/2010)

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

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

queue_page = source.Source()

def init():
	body.add_page("Queue", queue_page)
	sources.init_src(queue.init_bg, filler)

def filler():
	queue_page.init(filter, is_visible, right_click, play_id, reset)
	menu.add_queue_menu_item("Remove from queue", rm_selected_from_queue)
	queue_page.fill(queue.walk, True)
	ocarina.libsaria.event.invite("POSTNEXT", refresh)

def filter(text):
	queue.filter(text)
	queue_page.refilter()
	queue_page.goto()

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

def reset():
	queue_page.clear()
	queue.reset()

def refresh(*args):
	queue_page.clear()
	queue_page.fill(queue.walk)

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

def rm_selected_from_queue(menu):
	queue_page.for_each_selected_id(queue.rm_id)
	refresh()

def play_id(id):
	queue.play_id(id)
	refresh()
