# Bryan Schumaker (8/26/2010)

import ocarina
import image
LS     = ocarina.libsaria
gtk    = ocarina.gtk
prefs  = LS.prefs
invite = LS.event.invite

class Button(gtk.Button):
	def __init__(self, stock, tooltip, func=None, show=True):
		gtk.Button.__init__(self)
		self.func = func
		img = gtk.image_new_from_stock(stock, gtk.ICON_SIZE_MENU)
		img.show()
		self.add(img)
		self.set_relief(gtk.RELIEF_NONE)
		self.click_id = self.connect("clicked", self.clicked)
		if show == True:
			Button.show(self)
		self.set_alignment(0,0)
		self.set_tooltip_text(tooltip)

	def clicked(self, button):
		self.func()

	def show(self, *args):
		gtk.Button.show(self)

	def hide(self, *args):
		gtk.Button.hide(self)

class PlayButton(Button):
	def __init__(self):
		Button.__init__(self, gtk.STOCK_MEDIA_PLAY, "Play")
		invite("POSTPLAY", self.hide)
		invite("POSTPAUSE", self.show)
		invite("POSTSTOP", self.show)
	def clicked(self, button):
		LS.controls.play()
	def hide(self, playing):
		if playing == True:
			Button.hide(self)
	def show(self, paused):
		if paused == True:
			Button.show(self)

class PauseButton(Button):
	def __init__(self):
		Button.__init__(self, gtk.STOCK_MEDIA_PAUSE, "Pause", show=False)
		invite("POSTPLAY", self.show)
		invite("POSTPAUSE", self.hide)
		invite("POSTSTOP", self.hide)
	def clicked(self, button):
		LS.controls.pause()
	def hide(self, paused):
		if paused == True:
			Button.hide(self)
	def show(self, playing):
		if playing == True:
			Button.show(self)

class StopButton(Button):
	def __init__(self):
		Button.__init__(self, gtk.STOCK_MEDIA_STOP, "Stop")
	def clicked(self, button):
		LS.controls.stop()

class NextButton(Button):
	def __init__(self):
		Button.__init__(self, gtk.STOCK_MEDIA_NEXT, "Next")
	def clicked(self, button):
		LS.controls.next()

class ForwardButton(Button):
	def __init__(self):
		Button.__init__(self, gtk.STOCK_MEDIA_FORWARD, "Skip Forward")
	def clicked(self, button):
		LS.controls.seek_forward()

class RewindButton(Button):
	def __init__(self):
		Button.__init__(self, gtk.STOCK_MEDIA_REWIND, "Skip Backward")
	def clicked(self, button):
		LS.controls.seek_backward()

class OpenButton(Button):
	def __init__(self):
		Button.__init__(self, gtk.STOCK_OPEN, "Open Anything")
	def clicked(self, button):
		from ocarina import fsselect
		fsselect.run_chooser2(LS.path.files.universal_open)
		ocarina.playlist.refresh()

class SaveButton(Button):
	def __init__(self):
		Button.__init__(self, gtk.STOCK_SAVE, "Save current playlist")
	def clicked(self, button):
		from ocarina import fsselect
		from libsaria.path import export
		loc = fsselect.save()
		if loc != None:
			export.save_playlist(loc)

class ClearButton(Button):
	def __init__(self):
		Button.__init__(self, gtk.STOCK_CLEAR, "Clear Current Source")
	def clicked(self, button):
		from ocarina import body
		body.cur_page_reset()

class ExportButton(Button):
	def __init__(self):
		Button.__init__(self, gtk.STOCK_CONVERT, "Export Library")
	def clicked(self, button):
		from libsaria.path import export
		export.export()

class GotoButton(Button):
	def __init__(self):
		Button.__init__(self, gtk.STOCK_JUMP_TO, "Go To Current Song")
	def clicked(self, button):
		from ocarina import body
		body.cur_page_goto()

class UpdateButton(Button):
	def __init__(self):
		Button.__init__(self, gtk.STOCK_REFRESH, "Update Library")
	def clicked(self, button):
		LS.sources.library.update_bg()

class UpButton(Button):
	def __init__(self, callback):
		Button.__init__(self, gtk.STOCK_GO_UP, "Show More Information")
		self.callback = callback
	def clicked(self, button):
		self.callback()

class DownButton(Button):
	def __init__(self, callback):
		Button.__init__(self, gtk.STOCK_GO_DOWN, "Show Less Information")
		self.callback = callback
	def clicked(self, button):
		self.callback()

class VolumeButton(gtk.VolumeButton):
	def __init__(self):
		gtk.VolumeButton.__init__(self)
		self.set_relief(gtk.RELIEF_NONE)

		adj = self.get_adjustment()
		adj.set_page_increment(0.05)
		self.set_value(prefs.get_pref("libsaria.audio.volume"))
		self.resize()

		self.connect("value-changed", self.changed)
		invite("POSTSETVOLUME", self.set_slider)
		self.set_tooltip_text("Change Volume")
		self.show()

	def set_slider(self, prcnt):
		self.set_value(prcnt)

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

	def changed(self, widget, value):
		LS.controls.set_volume(value)
		widget.resize()

class ToggleButton(gtk.ToggleButton):
	def __init__(self, image, tooltip):
		gtk.ToggleButton.__init__(self)
		img = gtk.image_new_from_file(image)
		self.add(img)
		self.set_relief(gtk.RELIEF_NONE)
		self.set_tooltip_text(tooltip)
		self.t_id = self.connect("toggled", self.toggle)
		self.show_all()

	def toggle(self, *args):
		pass

class LikeButton(ToggleButton):
	def __init__(self):
		ToggleButton.__init__(self, "images/thumbs_up.png", "I like this")
		invite("POSTLOAD", self.set_status)
		invite("POSTSETLIKE", self.set_status)

	def set_status(self, *args):
		self.disconnect(self.t_id)
		self.set_active(LS.controls.get_like() == True)
		self.t_id = self.connect("toggled", self.toggle)

	def toggle(self, button):
		status = self.get_active()
		if status == False:
			status = None
		LS.controls.set_like(status)

class DislikeButton(ToggleButton):
	def __init__(self):
		ToggleButton.__init__(self, "images/thumbs_down.png", "I don't like this")
		invite("POSTLOAD", self.set_status)
		invite("POSTSETLIKE", self.set_status)

	def set_status(self, *args):
		self.disconnect(self.t_id)
		self.set_active(LS.controls.get_like() == False)
		self.t_id = self.connect("toggled", self.toggle)

	def toggle(self, button):
		status = not self.get_active()
		if status == True:
			status = None
		LS.controls.set_like(status)

class RandomButton(ToggleButton):
	def __init__(self):
		ToggleButton.__init__(self, "images/random.png", "Random")
		self.set_active(prefs.get_pref("libsaria.random"))

	def toggle(self, button):
		LS.controls.set_rand(self.get_active())
