# Bryan Schumaker (12/7/2010)

import datetime
import libsaria
path  = libsaria.path
tagpy = None

timedelta = datetime.timedelta

source   = None
cur_file = None
file_ref = None
tags     = None
audio    = None

def init():
	global source
	global tagpy

	import tagpy

	source = libsaria.sources.Source()
	source.get_attrs  = get_attrs
	source.set_attr   = set_attr
	source.get_cur_id = get_cur_file
	source.save       = save

	load_file = source.load_file
	print load_file

def load_file(file):
	global file_ref
	global tags
	global audio
	global cur_file
	file_ref = tagpy.FileRef(file)
	tags     = file_ref.tag()
	audio    = file_ref.audioProperties()
	cur_file = file
	source.load_file(file)

def get_cur_file():
	return cur_file

def get_attrs(file, *attrs):
	print "Getting attributes for: %s" % file
	#print "  Requested attributes: %s" % attrs

	res     = []
	id      = libsaria.path.file_id(file)
	seconds = audio.length
	for attr in attrs:
		if attr == "id":
			res += [id]
		elif attr == "filepath":
			res += [file]
		elif attr == "art":
			from libsaria import lastfm
			res += [id]
		elif attr == "artist":
			res += [tags.artist]
		elif attr == "album":
			res += [tags.album]
		elif attr == "title":
			res += [tags.title]
		elif attr == "year":
			res += [tags.year]
		elif attr == "seconds":
			res += [seconds]
		elif attr == "lenstr":
			lenstr = "%s" % timedelta(seconds=seconds)
			if lenstr[0] == "0" and lenstr[1] == ":":
				lenstr = lenstr[2:]
			res += [lenstr]
		else:
			return "???"
	if len(res) == 1:
		return res[0]
	return res

def set_attr(*args):
	pass

def save():
	pass

init()
