<?php
	//
	// m85query -- an object for using earfull query APIs
	//
	
	final class MusicQuery
	{
		//------------------------------------------------------------------------------
		//
		// Use curl to make a rest call to the query server,
		// decode the JSON that's returned
		//
		private function restCall($cmd)
		{
			$debugSQL = false;
			
			$url = "https://musicquery.pairoducks.com:236/$cmd";
		
			if ($debugSQL)
				print "restCall($url)\n";
			$curl = curl_init();
			curl_setopt($curl, CURLOPT_URL, $url);
			curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
			$jsonstr = curl_exec($curl);
			curl_close($curl);
			if ($debugSQL)
				print "restCall result:$jsonstr\n";
		
			return json_decode($jsonstr);
		}
	
		//
		// Access to singleton
		//
    	public static function Instance()
    	{
        	static $inst = null;
        	if ($inst === null) {
            	$inst = new MusicQuery();
        	}
        	return $inst;
    	}

		//
		// No direct instantiation, must use singleton
		//
		private function __construct()
		{
		}
		
		//
		// Return an array of dictionaries with id, title
		//
		public function genre_list()
		{
			return $this->restCall("genrelist");
		}
		
		//
		// Return a count of genres
		//
		public function genre_count()
		{
			$response = $this->restCall("genrecount");
			return $response->count;
		}
		
		//
		// Return an array of dictionaries with artist info
		//
		public function artists_in_genre($genre_id)
		{
			return $this->restCall("artistsingenre?genre_id=$genre_id");
		}
		
		//
		// Return a count of artists in given genre
		//
		public function artists_in_genre_count($genre_id)
		{
			$response = $this->restCall("artistsingenrecount?genre_id=$genre_id");
			return $response->count;
		}
		
		//
		// Return an array of dictionaries with album info
		//
		public function albums_in_genre($genre_id)
		{
			return $this->restCall("albumsingenre?genre_id=$genre_id");
		}
		
		//
		// Return a count of albums in given genre
		//
		public function albums_in_genre_count($genre_id)
		{
			$response = $this->restCall("albumsingenrecount?genre_id=$genre_id");
			return $response->count;
		}
		
		//
		//
		//
		public function genres_for_artist($artist_id)
		{
			return $this->restCall("genresforartist?artist_id=$artist_id");
		}
		
		//
		//
		//
		public function albums_for_artist($artist_id)
		{
			return $this->restCall("albumsforartist?artist_id=$artist_id");
		}

		//
		//
		//
		public function albums_for_artist_count($artist_id)
		{
			$response = $this->restCall("albumsforartistcount?artist_id=$artist_id");
			return $response->count;
		}
		
		//
		//
		//
		public function teach_artist_id()
		{
			$response = $this->restCall("teachartistid");
			return $response->artist_id;
		}

		//
		//
		//
		public function lecture_genre_id()
		{
			$response = $this->restCall("lecturegenreid");
			return $response->genre_id;
		}

		//
		//
		//
		public function albums_for_teach_genre($genre_id)
		{
			return $this->restCall("albumesforteachgenre?genre_id=$genre_id");
		}
		
		//
		//
		//
		public function albums_for_teach_genre_count($genre_id)
		{
			$response = $this->restCall("albumesforteachgenrecount?genre_id=$genre_id");
			return $response->count;
		}
		
		//
		//
		//
		public function various_artists_id()
		{
			$response = $this->restCall("variousartistid");
			return $response->artist_id;
		}

		//
		//
		//
		public function albums_for_various_artists_genre($genre_id)
		{
			return $this->restCall("variousartistsingenre?genre_id=$genre_id");
		}
		
		//
		//
		//
		public function albums_for_various_artists_genre_count($genre_id)
		{
			$response = $this->restCall("variousartistsingenrecount?genre_id=$genre_id");
			return $response->count;
		}
		
		//
		//
		//
		public function genres_for_album($album_id)
		{
			return $this->restCall("genresforalbum?album_id=$album_id");
		}
		
		//
		//
		//
		public function discs_for_album($album_id)
		{
			return $this->restCall("discsforalbum?album_id=$album_id");
		}
		
		//
		//
		//
		public function discs_for_album_count($album_id)
		{
			$response =  $this->restCall("discsforalbumcount?album_id=$album_id");
			return $response->count;
		}
		
		//
		//
		//
		public function songs_for_disc($disc_id)
		{
			return $this->restCall("songsfordisc?disc_id=$disc_id");
		}
		
		//
		//
		//
		public function songs_for_disc_count($disc_id)
		{
			$response =   $this->restCall("songsfordisccount?disc_id=$disc_id");
			return $response->count;
		}
		
		//
		//
		//
		public function songs_for_album($album_id)
		{
			return $this->restCall("songsforalbum?album_id=$album_id");
		}
		
		//
		//
		//
		public function songs_for_album_count($album_id)
		{
			$response = $this->restCall("songsforalbumcount?album_id=$album_id");
			return $response->count;
		}
		
		//
		//
		//
		public function songs_for_album_top($album_id)
		{
			return $this->restCall("songsforalbumtop?album_id=$album_id");
		}
		
		//
		//
		//
		public function songs_for_album_top_count($album_id)
		{
			$response =  $this->restCall("songsforalbumtopcount?album_id=$album_id");
			return $response->count;
		}
		
		//
		//
		//
		public function genre_for_id($genre_id)
		{
			return $this->restCall("genreforid?genre_id=$genre_id");
		}
		
		//
		//
		//
		public function artist_for_id($artist_id)
		{
			return $this->restCall("artistforid?artist_id=$artist_id");
		}
		
		//
		//
		//
		public function album_for_id($album_id)
		{
			return $this->restCall("albumforid?album_id=$album_id");
		}
		
		//
		//
		//
		public function disc_for_id($disc_id)
		{
			return $this->restCall("discforid?disc_id=$disc_id");
		}
		
		//
		//
		//
		public function song_for_id($song_id)
		{
			return $this->restCall("songforid?song_id=$song_id");
		}
		
		//
		//
		//
		public function images_for_artist($artist_id)
		{
			return $this->restCall("imagesforartist?artist_id=$artist_id");
		}
		
		//
		//
		//
		public function images_for_album($album_id)
		{
			return $this->restCall("imagesforalbum?album_id=$album_id");
		}
		
		//
		//
		//
		public function images_for_disc($disc_id)
		{
			return $this->restCall("imagesfordisc?disc_id=$disc_id");
		}
		
		//
		//
		//
		public function artists_for_album($album_id)
		{
			return $this->restCall("artistsforalbum?album_id=$album_id");
		}
		
		//
		//
		//
		public function artists_for_song($song_id)
		{
			return $this->restCall("artistsforsong?song_id=$song_id");
		}
		
		//
		//
		//
		public function singles_songs()
		{
			return $this->restCall("singlessongs");
		}
		
		//
		//
		//
		public function singles_songs_count()
		{
			$response = $this->restCall("singlessongscount");
			return $response->count;
		}
		
		//
		//
		//
		public function singles_album_id()
		{
			$response = $this->restCall("singlesalbumid");
			return $response->album_id;
		}
	}
	
?>
