/**
 * web_servicecontroller_scrobbler.c: static function that can be call
 * in order to scrobble music
 * 
 * Copyright: See COPYING file that comes with this distribution
 *
 * Author: Matthias Feistel <hitman_47@users.sourceforge.net>, (C) 2007
 */

#include "web_serviceconnector_scrobbler.h"
#include "web_networkcontroller.h" // NetworkController.HttpPostRequest
#include <sstream>
#include <time.h> // time(0)
#include "config.h"  // CLIENTID
#include "utils.h" // MD5

int cWebServiceConnectorScrobbler::Submission(
				const std::string submissionurl, const std::string sessionid,
				const std::string artist, const std::string track, const std::string secs,
				const std::string source, const int position, const int total,
				const std::string album, const std::string tracktime,
				const std::string tracknumber, const std::string mb_trackid,
				const std::string rating )
{
	// test, whether required variables are plausible:
	
	
	// submit track, when it has played for at least 240secs or 50% of the track
	// but at least 30 secs:
	if( position > 240 || ( position > total / 2 && total >= 30 ) )
	{
		std::string buffer_HttpPostResponse;
		std::stringstream sstream_tmp;
		std::map<std::string,std::string> postdata;
		cNetworkController NetworkController;

		if( submissionurl.compare("") == 0 )
			return 1;


		postdata[ "s" ] = sessionid;
		postdata[ "a[0]" ] = artist;
		postdata[ "t[0]" ] = track;

		sstream_tmp.clear();
		sstream_tmp.str("");
		sstream_tmp << time(0);
		postdata[ "i[0]" ] = sstream_tmp.str();

		postdata[ "o[0]" ] = source;
		postdata[ "r[0]" ] = rating;
		postdata[ "l[0]" ] = secs;
		postdata[ "b[0]" ] = album;
		postdata[ "n[0]" ] = tracknumber;		
		postdata[ "m[0]" ] = mb_trackid;


		if( !NetworkController.HttpPostRequest(
				buffer_HttpPostResponse, submissionurl,	postdata ) )
		{
			return 2;
		}

	}				
	
	return 0;
}

int cWebServiceConnectorScrobbler::NowPlaying( 
			const std::string submissionurl, const std::string sessionid,
			const std::string artist, const std::string track,
			const std::string album, const std::string secs,
			const std::string tracknumber, const std::string mb_trackid )
{
	if( submissionurl.compare("") == 0 )
		return 1;
	
	
	std::string buffer_HttpPostResponse;
	std::map<std::string,std::string> postdata;
	cNetworkController NetworkController;
	

	postdata[ "s" ] = sessionid;
	postdata[ "a" ] = artist;
	postdata[ "t" ] = track;
	postdata[ "b" ] = album;
	postdata[ "l" ] = secs;
	postdata[ "n" ] = tracknumber;		
	postdata[ "m" ] = mb_trackid;


	if( !NetworkController.HttpPostRequest(
			buffer_HttpPostResponse, submissionurl,	postdata ) )
	{
		return 2;
	}
		
	
	return 0;
}

int cWebServiceConnectorScrobbler::HandshakeV12( std::string username,
		std::string password_md5,
		std::string &data_npurl, std::string &data_protourl, std::string &data_sessionid )
{
	std::string buffer_HttpGetResponse, stream_url, url_in, session,
			subscriber;
	std::stringstream url_in_stream, sstream1;
	
	std::map<std::string, std::string>::iterator elHandshakedataMap;
	std::map<std::string, std::string> HandshakedataMap;
	
	
	/*
	 http://post.audioscrobbler.com/?hs=true&p=1.2&
	 c=<client-id>&v=<client-ver>&u=<user>&t=<timestamp>&a=<auth>
	 
	 token := md5(md5(password) + timestamp)
	 */
	time_t timestamp = time ( NULL );
	std::stringstream tokenstream;
	std::string md5tokenstring, tokenstring;
	tokenstream << password_md5 << timestamp;
	tokenstring = tokenstream.str();
	
	std::map<std::string,std::string> postdata;
	
	cNetworkController NetworkController;
	
	md5tokenstring = cUtils::MD5( tokenstring );
	
	
	//AUDIOSCROBBLER_POST_DOMAIN
	url_in_stream <<"http://"<< AUDIOSCROBBLER_POST_DOMAIN << ":"
			<< AUDIOSCROBBLER_PORT << "/index.php"
		"?"<< "hs=true"<<"&"<< "p=1.2"<<"&"<< "c="<< AUDIOSCROBBLER_CLIENTID
			<<"&"<< "v="<< AUDIOSCROBBLER_CLIENTVERSION <<"&"<< "u="<< username
			<<"&"<< "t="<< timestamp<<"&"<< "a="<< md5tokenstring;
	
	url_in = url_in_stream.str();

	if( !NetworkController.HttpGetRequest( buffer_HttpGetResponse, url_in ) )
	{
		return 2;
	}

	// A possible answer of the response above, could be:
	/*
	 OK
	 035b206fda353rfdc408a58253e11e9
	 http://post.audioscrobbler.com:80/np_1.2
	 http://87.117.229.205:80/protocol_1.2
	 */

	// Here, we parse the response for    1.  Session ID	   2. Now-Playing URL	   3. Submission URL

	if( buffer_HttpGetResponse.find( "OK" ) == std::string::npos )
		return 1; // did not receive OK(200)-Code from Server

	std::string tmp;
	
	sstream1 << buffer_HttpGetResponse;
	
	while( getline( sstream1, tmp ) )
	{
		if( tmp.find( "OK" ) != std::string::npos )
		{
		}
		else if( tmp.find( "np" ) != std::string::npos )
			data_npurl = tmp;
		else if( tmp.find( "protocol_1.2" ) != std::string::npos )
			data_protourl = tmp;
		else
			data_sessionid = tmp;
	}
	
	if( data_sessionid.empty( ) )		
		return 1;
	
	return 0;
}
