# Bryan Schumaker (5 / 11 / 2011)
# TODO: Split this file into two files, similiar to how audio is broken up

import threading
from libsaria import storage
plist_lock   = threading.Lock()
lock_plist   = plist_lock.acquire
unlock_plist = plist_lock.release

# Playlist
song_list = []
song_set  = set()

def load():
	global song_list
	plist_list = storage.load_obj("playlist", list)
	# TODO: Can I do this check during load_obj???
	if plist_list.__class__ != list:
		plist_list = []
	lock_plist()
	song_list = plist_list
	unlock_plist()

# NOTE: You should already be holding the lock when you call this
def save():
	storage.save_obj("playlist", song_list)

def reset():
	global song_list
	lock_plist()
	song_list = []
	save()
	unlock_plist()

def add_ids(id_list):
	lock_plist()
	song_list.extend(id_list)
	save()
	unlock_plist()

def rm_ids(id_list):
	lock_plist()
	for id in id_list:
		if id in song_list:
			song_list.remove(id)
	save()
	unlock_plist()

def as_set():
	lock_plist()
	song_set = set(song_list)
	unlock_plist()
	return song_set

def size():
	lock_plist()
	sz = len(song_list)
	unlock_plist()
	return sz

def index(item):
	lock_plist()
	try:
		idx = song_list.index(item)
	except:
		idx = -1
	unlock_plist()
	return idx

def get(index):
	lock_plist()
	try:
		item = song_list[index]
	except:
		item = None
	unlock_plist()
	return item
