var Clay = Clay || {};
// GameMaker related data is stored here... (a lot of callback stuff)
Clay['gamemaker'] = Clay['gamemaker'] || {};

/**
 * 
 * LEADERBOARD FUNCTIONS
 *  
 */

/**
 * Shows a Clay.io Leaderboard 
 * @param {String} leaderboardID
 * @param {String} timeFrame
 * @param {String} type
 * @param {String} sort
 * @param {String} scoresFrom
 * @param {Int/Decimal} numScores
 */
function clay_leaderboard_show( leaderboardID, timeFrame, type, sort, scoresFrom, numScores )
{
	var recent = 0;
	if( !numScores )
		numScores = 10;
	switch( timeFrame )
	{
		case 'all':
			recent = 0;
			break;
		case 'month':
			recent = 3600 * 24 * 30;
			break;
		case 'week':
			recent = 3600 * 24 * 7;
			break;
		case 'day':
			recent = 3600 * 24;
			break;
		default:
			recent = 0;
	}
	var options = {
		"cumulative": ( type === 1 ),
		"self": ( scoresFrom === 'self' ) ? true : false,
		"friends": ( scoresFrom === 'friends' ) ? true : false,
		"recent": recent,
		"sort": ( sort === 'asc' ) ? 'asc' : 'desc',
		"limit": numScores
	}
	var leaderboard = new Clay['Leaderboard']( { id: leaderboardID } );
		leaderboard['show']( options );
}

/**
 * Fetches a Clay.io Leaderboard 
 * @param {String} leaderboardID
 * @param {String} timeFrame
 * @param {String} type
 * @param {String} sort
 * @param {String} scoresFrom
 * @param {Int/Decimal} numScores
 */
Clay['gamemaker']['leaderboardFetched'] = {};
function clay_leaderboard_fetch( leaderboardID, timeFrame, type, sort, scoresFrom, numScores )
{
	var recent = 0;
	if( !numScores )
		numScores = 10;
	switch( timeFrame )
	{
		case 'all':
			recent = 0;
			break;
		case 'month':
			recent = 3600 * 24 * 30;
			break;
		case 'week':
			recent = 3600 * 24 * 7;
			break;
		case 'day':
			recent = 3600 * 24;
			break;
		default:
			recent = 0;
	}
	var options = {
		"cumulative": ( type === 1 ),
		"self": ( scoresFrom === 'self' ) ? true : false,
		"friends": ( scoresFrom === 'friends' ) ? true : false,
		"recent": recent,
		"sort": ( sort === 'asc' ) ? 'asc' : 'desc',
		"limit": numScores,
		"getRank": true
	}
	var leaderboard = new Clay['Leaderboard']( { id: leaderboardID } );
	
	// Delete any previously stored data
	delete Clay['gamemaker']['leaderboardFetched'][ leaderboardID ];
	
	leaderboard['fetch']( options, function( results ) {
		Clay['gamemaker']['leaderboardFetched'][ leaderboardID ] = results;
	} );
}
function clay_leaderboard_fetched_ready( leaderboardID )
{
	return clay_check_ready( Clay['gamemaker']['leaderboardFetched'][ leaderboardID ] );
}
function clay_leaderboard_fetched_name_at( leaderboardID, index )
{
	if( clay_leaderboard_fetched_ready( leaderboardID ) )
	{	
		return Clay['gamemaker']['leaderboardFetched'][ leaderboardID ]['data'][ index ]['name'];
	}
}
function clay_leaderboard_fetched_score_at( leaderboardID, index )
{
	if( clay_leaderboard_fetched_ready( leaderboardID ) )
	{	
		return Clay['gamemaker']['leaderboardFetched'][ leaderboardID ]['data'][ index ]['score'];
	}
}
function clay_leaderboard_fetched_count( leaderboardID )
{
	if( clay_leaderboard_fetched_ready( leaderboardID ) )
	{	
		return Clay['gamemaker']['leaderboardFetched'][ leaderboardID ]['data']['length'];
	}
}

/**
 * Posts a score to Clay.io
 * @param {String} leaderboardID
 * @param {String} playerName
 * @param {String} playerPoints
 * @param {String} hideNotification 
 */
Clay['gamemaker']['leaderboardPosted'] = {};
function clay_leaderboard_post( leaderboardID, playerName, playerPoints, hideNotification )
{
	var scoreObj = {
		"name": playerName
	};
	
	if( playerPoints )
		scoreObj['score'] = playerPoints;
	else
		scoreObj['score'] = 0;
		
	if( hideNotification )
		scoreObj['hideUI'] = true;
		
	var leaderboard = new Clay['Leaderboard']( { id: leaderboardID } );
	// Delete any previously stored data
	Clay['gamemaker']['leaderboardPosted'][ leaderboardID ] = false;
	leaderboard['post']( scoreObj, function() {
		Clay['gamemaker']['leaderboardPosted'][ leaderboardID ] = {};
	} );
}

function clay_leaderboard_posted_ready( leaderboardID )
{	
	return clay_check_ready( Clay['gamemaker']['leaderboardPosted'][ leaderboardID ] );
}