# Bryan Schumaker (5 / 14 / 2011)

import random
import playlist
import libsaria

recent = []

def track_recent(artist, title):
	recent.append((artist, title))
	if len(recent) > 50:
		recent.pop(0)

def incr_index(cur_index, max_index):
	cur_index += 1
	if cur_index == max_index:
		cur_index = 0
	return cur_index

def seq_next(cur_id):
	is_visible = libsaria.sources.is_visible

	max   = playlist.size()
	if max == 0: # List is empty
		return

	index = playlist.index(cur_id)
	index = incr_index(index, max)
	while not is_visible(playlist.get(index)):
		index = incr_index(index, max)
	return index

def check_candidate(id):
	(artist, title, like) = libsaria.sources.get_attrs_id(id, "artist", "title", "like")
	if (artist, title) in recent:
		return False
	if like == False:
		return False
	return True

def rand_next(list):
	max = len(list) - 1
	if max == -1: # List is empty
		return
	for i in xrange(15):
		index = random.randint(0, max)
		id    = list[index]
		if check_candidate(id) == True:
			break
	return playlist.index(id)
