# Bryan Schumaker (2 / 20 / 2011)

import gtk
from libsaria.path import files
from libsaria.path import export
from libsaria import controls
from libsaria import sources
from ocarina import fsselect

SIZE = gtk.ICON_SIZE_MENU

def on_click(button, func):
	func()

def on_toggle(button, func):
	func(button.get_active())

def stock_image(stock_item):
	img = gtk.image_new_from_stock(stock_item, SIZE)
	return img

def file_image(file):
	img = gtk.image_new_from_file(file)
	return img

def make_text(text):
	lbl = gtk.Label(text)
	lbl.show()
	return lbl

def default_button_attrs(button, img, tooltip, show):
	button.set_relief(gtk.RELIEF_NONE)
	if img != None:
		img.show()
		button.add(img)
	if tooltip != None:
		button.set_tooltip_text(tooltip)
	if show == True:
		button.show()

def make_button(stock_item, func, tooltip, show):
	b = gtk.Button()
	b.connect("clicked", on_click, func)
	default_button_attrs(b, stock_image(stock_item), tooltip, show)
	return b

def toggle_connect(button, func):
	button.t_id = button.connect("toggled", on_toggle, func)
	button.t_func = func

def toggle_call_unconnected(button, func, *args):
	button.disconnect(button.t_id)
	func(*args)
	toggle_connect(button, button.t_func)

def make_toggle(img_file, func, tooltip, is_active, show):
	b = gtk.ToggleButton()
	b.set_active(is_active)
	toggle_connect(b, func)
	default_button_attrs(b, file_image(img_file), tooltip, show)
	return b


def rewind_button(show):
	return make_button(gtk.STOCK_MEDIA_REWIND, controls.seek_backward, "Rewind", show)

def forward_button(show):
	return make_button(gtk.STOCK_MEDIA_FORWARD, controls.seek_forward, "Fast Forward", show)

def play_button(show):
	return make_button(gtk.STOCK_MEDIA_PLAY, controls.play, "Play", show)

def pause_button(show):
	return make_button(gtk.STOCK_MEDIA_PAUSE, controls.pause, "Pause", show)

def stop_button(show):
	return make_button(gtk.STOCK_MEDIA_STOP, controls.stop, "Stop", show)

def next_button(show):
	return make_button(gtk.STOCK_MEDIA_NEXT, controls.next, "Next", show)

def export_button(show):
	return make_button(gtk.STOCK_CONVERT, export.export, "Export Library", show)

def update_button(show):
	return make_button(gtk.STOCK_REFRESH, sources.library.update_bg, "Update Library", show)

def open_button(show):
	def open_func():
		fsselect.run_chooser2(files.universal_open)
	return make_button(gtk.STOCK_OPEN, open_func, "Open Anything", show)

def save_button(show):
	def save_func():
		loc = fsselect.select_save()
		if loc != None:
			export.save_playlist(loc)
	return make_button(gtk.STOCK_SAVE, save_func, "Save Current Playlist", show)

def up_button(show):
	def up_func():
		from ocarina.body import footer
		footer.show_more()
	return make_button(gtk.STOCK_GO_UP, up_func, "Show More Information", show)

def down_button(show):
	def down_func():
		from ocarina.body import footer
		footer.show_less()
	return make_button(gtk.STOCK_GO_DOWN, down_func, "Show Less Information", show)

def goto_button(show):
	def goto_func():
		from ocarina import body
		body.cur_page_goto()
	return make_button(gtk.STOCK_JUMP_TO, goto_func, "Go To Current Song", show)

def clear_button(show):
	def clear_func():
		from ocarina import body
		body.cur_page_clear()
	return make_button(gtk.STOCK_CLEAR, clear_func, "Clear Current Source", show)

def random_button(is_active, show):
	return make_toggle("images/random.png", controls.set_rand, "Random", is_active, show)

def like_button(show):
	def set_like(like):
		if like == False:
			like = None
		controls.set_like(like)
	return make_toggle("images/thumbs_up.png", set_like, "I like this", False, show)

def dislike_button(show):
	def set_dislike(like):
		like = not like
		if like == True:
			like = None
		controls.set_like(like)
	return make_toggle("images/thumbs_down.png", set_dislike, "I don't like this", False, show)

def volume_button(volume, show):
	b = gtk.VolumeButton()
	default_button_attrs(b, None, None, show)
	b.get_adjustment().set_page_increment(0.05)
	b.set_value(volume)

	def resize(button):
		image = b.get_children()[0]
		icon  = image.get_icon_name()[0]
		image.set_from_icon_name(icon, gtk.ICON_SIZE_MENU)

	def changed(button, value):
		controls.set_volume(value)
		resize(button)
	b.connect("value-changed", changed)
	resize(b)
	return b
