# Bryan Schumaker (4 / 21 / 2011)

import gtk
from ocarina.body import button
import libsaria
from libsaria import audio
import nowplaying

detailed = gtk.Notebook()
hide     = detailed.hide
show     = detailed.show

action = gtk.HBox()

cur_pos = gtk.Label()
cur_pos.show()
action.pack_start(cur_pos, False, False)

def slider_update(slider):
	slider.set_value(audio.get_progress() * 100)

def slider_scrolled(slider, type, value):
	audio.seek_prcnt(value / 100.0)

def slider_clicked(slider, event):
	prcnt = event.x / float(slider.get_allocation().width)
	audio.seek_prcnt(prcnt)
	slider_update(slider)

def slider_tooltip(slider, event):
	prcnt = event.x / float(slider.get_allocation().width)
	slider.set_tooltip_text(audio.get_time_at(prcnt))

slider = gtk.HScale( gtk.Adjustment(0.0, 0.0, 100.0, 0.1, 1.0, 1.0) )
slider.set_size_request(150, 20)
slider.set_draw_value(False)
slider.set_range(0, 101)
slider.set_update_policy(gtk.UPDATE_DISCONTINUOUS)
slider.connect("change-value", slider_scrolled)
slider.connect("button-press-event", slider_clicked)
slider.connect("motion-notify-event", slider_tooltip)
slider.show()
action.pack_start(slider, True, True)

def add_button(name, button_func, show = True):
	b = button_func(show)
	globals()[name] = b
	action.pack_start(b, False, False)

add_button(   "PLAY", button.play_button)
add_button(  "PAUSE", button.pause_button, False)
add_button(   "STOP", button.stop_button)
add_button(   "NEXT", button.next_button)
add_button(   "DOWN", button.down_button)
action.show()

detailed.set_action_widget(action, gtk.PACK_END)
detailed.append_page(nowplaying.page, nowplaying.label)

def set_art(path):
	nowplaying.set_art(path)

def update_pos(time):
	slider_update(slider)
	cur_pos.set_text(time)

def on_play():
	PLAY.hide()
	PAUSE.show()

def on_pause():
	PLAY.show()
	PAUSE.hide()

load_attrs = ("title", "artist", "album", "year", "lenstr", "count", "like")
def on_load():
	attrs = libsaria.sources.get_attrs(*load_attrs)
	nowplaying.on_load(*attrs)
