/**
 * audio_control.c: The control interface
 * 
 * Copyright: See COPYING file that comes with this distribution
 *
 * Author: Matthias Feistel <hitman_47@users.sourceforge.net>, (C) 2007
 */

#include "audio_control.h"
#include "settings.h"  // receivermode if-clause

cAudioControl *cAudioControl::gpo_config = 0;

cAudioControl& cAudioControl::GetInstance( void )
{
	if( NULL == gpo_config )
		gpo_config = new cAudioControl();
	
	return *gpo_config;
}

cAudioControl::cAudioControl( void ) :
	cControl(player = new cAudioPlayer( ))
{
	gpo_config = this;
	WebConn = new cWebServiceConnector();
	
#ifdef LASTFM_USE_SOFTDEVICE_RECEIVER
	ReceiverController = 0;
	if( atoi( cSettings::GetInstance().ParameterMap[ "Receivermode" ].c_str() ) )
	ReceiverController = new cReceiverController();
#endif //LASTFM_USE_SOFTDEVICE_RECEIVER
}

cAudioControl::~cAudioControl( void )
{
	cSettings::GetInstance().CurrentProfilename.clear( );
	
	if( WebConn )
		DELETENULL( WebConn );
	 
#ifdef LASTFM_USE_SOFTDEVICE_RECEIVER
	if( ReceiverController )
		DELETENULL( ReceiverController );	
#endif //LASTFM_USE_SOFTDEVICE_RECEIVER
	
	if( player )
		DELETENULL( player );
}

void cAudioControl::Hide( void )
{
	// vdr-mp3-plugin (mp3.c):
#if APIVERSNUM >= 10500
	SetNeedsFastResponse(false);
#else
	needsFastResponse = false;
#endif
}

void cAudioControl::Next( void )
{
	if( player )
		player->Next();
}

eOSState cAudioControl::ProcessKey( eKeys Key )
{
	eOSState state = osContinue;
	
	if( !player->Active( ) )
	{
		return osEnd;
	}
	
	switch( Key & ~k_Repeat )
	{
		case kRed:
			if( WebConn->Control( "love" ) != 0 )
			{
				Skins.Message( mtError,
						tr( "Control(\"love\") was not successful!" ) );
			}
			break;
			
		case kGreen:
			if( WebConn->Control( "skip" ) != 0 )
			{
				Skins.Message( mtError,
						tr( "Control(\"skip\") was not successful!" ) );
			}
			break;
			
		case kYellow:
			if( WebConn->Control( "ban" ) != 0 )
			{
				Skins.Message( mtError,
						tr( "Control(\"ban\") was not successful!" ) );
			}
			break;
			
#ifdef LASTFM_USE_SOFTDEVICE_RECEIVER
			case k3:
			if ( ReceiverController )
				ReceiverController->ChannelUpDown(+1);
			break;
			
			case k1:
			if ( ReceiverController )
				ReceiverController->ChannelUpDown(-1);
			break;
#endif //LASTFM_USE_SOFTDEVICE_RECEIVER
			
		case kBlue:
			state = osEnd;
			break;
			
		default:
			break;
	}
	
	return state;
}
