# Bryan Schumaker (11/23/2010)

import prefs
import audio
from libsaria import callbacks
libsaria = None
sources  = None
library  = None
playlist = None
queue    = None

expand = None
exists = None

pause_after = False
prefs.init("libsaria.random", False)

def init():
	global libsaria
	import libsaria

def init2():
	global sources
	global library
	global playlist
	global expand
	global exists
	global queue

	expand   = libsaria.path.expand
	exists   = libsaria.path.exists
	sources  = libsaria.sources
	library  = sources.library
	playlist = sources.playlist
	queue    = sources.queue

def pick_next():
	global pause_after
	file = queue.next()
	if file == None:
		file = playlist.next()
	load(file)
	if pause_after == True:
		pause()
	else:
		play()
	pause_after = False

def catch_eos(*args):
	library.inc_count()
	pick_next()

def next():
	pick_next()

def play():
	audio.play()

def pause():
	audio.pause()

def playing():
	return audio.get_state() == audio.gst.STATE_PLAYING

def do_pause_after(*args):
	global pause_after
	pause_after = True

def toggle_play():
	if audio.get_state() == audio.gst.STATE_PAUSED:
		play()
	else:
		pause()

def stop():
	audio.stop()

def seek_forward():
	libsaria.audio.seek(5)

def seek_backward():
	libsaria.audio.seek(-5)

def set_volume(prcnt):
	audio.set_volume(prcnt)
	callbacks.set_volume(prcnt)

def set_rand(rand):
	prefs.set("libsaria.random", rand)

def set_like(like=None):
	sources.set_attr("like", like)
	callbacks.like_song(like)

def get_like():
	return sources.get_attrs("like")

def load(file):
	file = expand(file)
	if not exists(file):
		return False
	audio.load_file(file)
