<?php

// this uses the Rovi API
// http://developer.rovicorp.com/docs
// username: cms628
// password: CMS.6828
// API limitaations:
//  5 calls/second
//  3,500 calls/day


// load data into hash tables called
// moodlook and genrelook respectively
require_once('moodLookup.php');
require_once('genreLookup.php');


/* for debugging
error_reporting(E_ALL);
ini_set('display_errors', '1');
*/

function curl_get_file_contents($URL)
{
	$c = curl_init();
	curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($c, CURLOPT_URL, $URL);
	$contents = curl_exec($c);
	curl_close($c);

	if ($contents) return $contents;
		else return FALSE;
}


$apiKey ='ccv45s48kzqe98bh9bbppd2y';
$sharedSecret='8ZKD5V4vNp';
$sig = md5($apiKey.$sharedSecret.time());
$artist = urldecode ($_GET['artist']);
$artistURL = urlencode ($artist);


// get moods
$moodDataEncoded = curl_get_file_contents("http://api.rovicorp.com/data/v1/name/moods?apikey=$apiKey&sig=$sig&name=$artistURL");
if($moodDataEncoded == TRUE){
	$moodData = json_decode($moodDataEncoded);
}
else{
	echo '!invalid artist';
	exit;
}

// get styles
$styleDataEncoded = curl_get_file_contents("http://api.rovicorp.com/data/v1/name/musicstyles?apikey=$apiKey&sig=$sig&name=$artistURL");
if($styleDataEncoded == TRUE){
	$styleData = json_decode($styleDataEncoded);
}
else{
	echo 'invalid artist';
	exit;
}



// debugging
$genres = json_decode(file_get_contents("musicGenreData.txt"));

// put the moods into an array
$moods = array();
if ($moodData->moods == NULL) {
	echo 'invalid artist';
	exit;
}
foreach($moodData->moods as $temp){
	$moods[] = array($temp->name, $temp->weight);
}

// put the styles into an array
$styles = array();
foreach($styleData->musicStyles as $temp){

	$styles[] = array($temp->name, $temp->weight);
}


// set baseline scores
$aggressive = 0;
$suspicious = 0;
$confused = 0;
$oblivious = 0;

// use moodLookup hashtable to determine game moods
foreach($moods as $mood){

	//echo $moodLookup[$mood[0]].'<br/>'.$mood[0].'<br/>';
	switch ($moodlook[$mood[0]]) {
		case 'Aggressive':
			$aggressive = $aggressive + $mood[1];
			break;
		case 'Suspicious':
			$suspicious = $suspicious + $mood[1];
			break;
		case 'Confused':
			$confused = $confused + $mood[1];
			break;
		case 'Oblivious':
			$oblivious = $oblivious + $mood[1];
			break;
	}
}

// average out the moods
$divsor = count($moods);
$aggressive = $aggressive / $divsor;
$suspicious = $suspicious / $divsor;
$confused = $confused / $divsor;
$oblivious = $oblivious / $divsor;

// setup master genres
$avant = 0;
$blues = 0;
$childrens = 0;
$classical = 0;
$comedy = 0;
$country = 0;
$easy = 0;
$electronic = 0;
$folk = 0;
$holiday = 0;
$international = 0;
$jazz = 0;
$latin = 0;
$new = 0;
$pop = 0;
$rb = 0;
$rap = 0;
$reggae = 0;
$religious = 0;
$stage = 0;
$vocal = 0;

// tally master genre scores
foreach($styles as $subGenre){
	switch ($genrelook[$subGenre[0]]) {
		case 'Avant-Garde':
			$avant = $avant + $subGenre[1];
			break;
		case 'Blues':
			$blues = $blues + $subGenre[1];
			break;
		case 'Children\'s':
			$childrens = $childrens + $subGenre[1];
			break;
		case 'Classical':
			$classical = $classical + $subGenre[1];
			break;
		case 'Comedy/Spoken':
			$comedy = $comedy + $subGenre[1];
			break;
		case 'Country':
			$country = $country + $subGenre[1];
			break;
		case 'Easy Listening':
			$easy = $easy + $subGenre[1];
			break;
		case 'Electronic':
			$electronic = $electronic + $subGenre[1];
			break;
		case 'Folk':
			$folk = $folk + $subGenre[1];
			break;
		case 'Holiday':
			$holiday = $holiday + $subGenre[1];
			break;
		case 'International':
			$international = $international + $subGenre[1];
			break;
		case 'Jazz':
			$jazz = $jazz + $subGenre[1];
			break;
		case 'Latin':
			$latin = $latin + $subGenre[1];
			break;
		case 'New Age':
			$new = $new + $subGenre[1];
			break;
		case 'Pop/Rock':
			$pop = $pop + $subGenre[1];
			break;
		case 'R&B':
			$rb = $rb + $subGenre[1];
			break;
		case 'Rap':
			$rap = $rap + $subGenre[1];
			break;
		case 'Reggae':
			$reggae = $reggae + $subGenre[1];
			break;
		case 'Religious':
			$religious = $religious + $subGenre[1];
			break;
		case 'Stage & Screen':
			$stage = $stage + $subGenre[1];
			break;
		case 'Vocal':
			$vocal = $vocal + $subGenre[1];
			break;
	}
}
$genreBreakdown = array('Avant-Garde' => $avant, 'Blues' => $blues, 'Children\'s' => $childrens, 'Classical' =>$classical, 'Comedy/Spoken' => $comedy, 'Country' =>$country, 'Easy Listening' => $easy, 'Electronic' => $electronic, 'Folk' => $folk, 'Holiday' => $holiday, 'International' => $international, 'Jazz' => $jazz, 'Latin' => $latin, 'New Age' => $new, 'Pop/Rock' => $pop, 'R&B' => $rb, 'Rap' => $rap, 'Reggae' => $reggae, 'Religious' => $religious, 'Stage & Screen' => $stage, 'Vocal' => $vocal);

// determine which genre is the prevailing genre for the artist based on associated styles by weight
$genre = array_search(max($genreBreakdown), $genreBreakdown);

// output
$json = array();
$json['artist'] = $artist;
$json['genre'] = $genre;
$json['mood'] = array();
$json['mood']['agg'] = $aggressive;
$json['mood']['sus'] = $suspicious;
$json['mood']['con'] = $confused;
$json['mood']['obl'] = $oblivious;

$encoded = json_encode($json);
echo $encoded
//echo $artist.' - '.$genre.'<br/>Aggressive: '.$aggressive.'<br/>Suspicious: '.$suspicious.'<br/>Confused: '.$confused.'<br/>Oblivious: '.$oblivious;

?>