# Bryan Schumaker (8/8/2010)

import libsaria
exists  = libsaria.path.exists
expand  = libsaria.path.expand
from libsaria import threads

import library
import playlist
import queue

controls = libsaria.controls

cur_source = None
class Source:
	def __init__(self):
		self.get_attrs  = None
		self.set_attr   = None
		self.get_cur_id = None
		self.save       = None

	def set_current(self):
		global cur_source
		cur_source = self

	def load_file(self, path):
		global cur_source
		# I should probably get a lock here...
		try:
			self.set_current()
			controls.load(path)
			return True
		except:
			return  False

def get_attrs(*attrs):
	# I should probably get a lock here...
	if cur_source:
		id = cur_source.get_cur_id()
		return cur_source.get_attrs(id, *attrs)
	return -1

def set_attr(attr, value):
	if cur_source:
		cur_source.set_attr(cur_source.get_cur_id(), attr, value)
		cur_source.save()

def init_src(init_func, callback):
	thr = threads.BG_Thread(init_func, callback)
	thr.start()

def new_source(path, bg=True):
	global library
	path = expand(path)
	if not exists(path):
		return 0
	library.scan(path)
	libsaria.callbacks.new_source()

def play_id(id):
	library.play_id(id)
	queue.rm_id(id)
