/**
 * setup_parameters.c: Setup submenu for changing user parameters
 * 
 * Copyright: See COPYING file that comes with this distribution
 *
 * Author: Matthias Feistel <hitman_47@users.sourceforge.net>, (C) 2007
 */

#include "setup_parameters.h"
#include "settings.h"
#include "utils.h"
#include "settings_xml.h"
#include "setup_favourites.h"

cSetupParameters::cSetupParameters( const std::string profilename )
:m_profilename(profilename)
{
	// Load certain profile:
	std::map<std::string,std::string> ParameterMap;
	
	cXmlconfig::Load( profilename, ParameterMap, m_FavouriteList );

	if( !ParameterMap[ "Username" ].compare( "" ) ) // no username defined
		ParameterMap[ "Username" ] = profilename;
	
	strcpy( newUsername, ParameterMap[ "Username" ].c_str( ) );
	
	newReceivermode = atoi( ParameterMap[ "Receivermode" ].c_str( ) );
	newAlbumcovermode = atoi( ParameterMap[ "Albumcovermode" ].c_str( ) );
	newOsd_Mode = atoi( ParameterMap[ "Osd_Mode" ].c_str( ) );
	
	newOsd_Autostartmode = atoi( ParameterMap[ "Osd_Autostartmode" ].c_str( ) );
	
	newOsd_Refreshinterval = atoi( ParameterMap[ "Osd_Refreshinterval" ].c_str( ) );
	newScrobblemode = atoi( ParameterMap[ "Scrobblemode" ].c_str( ) );
	
	
	// StraItem-Arrays:	
	m_array_Receivermode[0] = (char*)tr( "Off" );
	m_array_Receivermode[1] = (char*)tr( "SoftdeviceReceiver" );
	m_array_Osd_Mode[0] = (char*)tr( "graphical" );
	m_array_Osd_Mode[1] = (char*)tr( "textual" );

	
	BuildOsdItems( );
}

void cSetupParameters::BuildOsdItems( void )
{
	Clear( );
	
	
	///newpassword leeren, damit es nicht im Menue als Klartext angezeigt wird.
	memset( &newPassword, 0, sizeof(newPassword) );
	
	Add( new cMenuEditStrItem( tr( "Username" ),
			newUsername, sizeof( newUsername ), FileNameChars ) );
	
	Add( new cMenuEditStrItem( tr( "Password" ),
			newPassword, sizeof( newPassword ), FileNameChars ) );
	
	
#ifdef LASTFM_USE_SOFTDEVICE_RECEIVER
	Add( new cMenuEditStraItem( tr( "Receivermode" ), &newReceivermode, 2, m_array_Receivermode ) );
#endif //LASTFM_USE_SOFTDEVICE_RECEIVER
#ifdef LASTFM_HAVE_MAGICK
	Add(new cMenuEditBoolItem( tr( "Show albumcover?" ), &newAlbumcovermode ) );
#endif //LASTFM_HAVE_MAGICK
	
	Add( new cMenuEditStraItem( tr( "Osdmode" ), &newOsd_Mode, 2, m_array_Osd_Mode ) );
	
	Add( new cMenuEditIntItem ( tr ( "Osd refresh interval" ),
			&newOsd_Refreshinterval, 0, 60 ) );

	Add( new cMenuEditBoolItem( tr( "Scrobblemode" ), &newScrobblemode ) );

	Add( new cMenuEditBoolItem( tr( "Show Osd at startup?" ), &newOsd_Autostartmode ) );
	
	Add( new cOsdItem( hk( tr( "Favourites...") ) ) );
}

void cSetupParameters::Store( void )
{	
	// check, whether we edit an active user using this plugin, and fill the
	// parameters according to his profile:
	if( cSettings::GetInstance().CurrentProfilename.compare( m_profilename ) != 0 )
	{
		std::map<std::string,std::string> ParameterMap;
		
		ParameterMap[ "Username" ] = newUsername;
		
		
		// Passwort nicht mitspeichern, wenn es leer gelassen wurde.
		if( strcmp( newPassword, "" ) != 0 )
		{
			ParameterMap[ "Password_md5" ] = cUtils::MD5( newPassword );
		}
		
		// reset password
		strcpy( newPassword, "" );

		
#ifdef LASTFM_USE_SOFTDEVICE_RECEIVER
		ParameterMap[ "Receivermode" ] = itoa( newReceivermode );
#endif //LASTFM_USE_SOFTDEVICE_RECEIVER
#ifdef LASTFM_HAVE_MAGICK
		ParameterMap[ "Albumcovermode" ] = itoa( newAlbumcovermode );
#endif //LASTFM_HAVE_MAGICK
		ParameterMap[ "Osd_Mode" ] = itoa( newOsd_Mode );
		ParameterMap[ "Osd_Refreshinterval" ] = itoa( newOsd_Refreshinterval );
		ParameterMap[ "Osd_Autostartmode" ] = itoa( newOsd_Autostartmode );
		ParameterMap[ "Scrobblemode" ] = itoa( newScrobblemode );

		
		// Store the data to the XML configfile:
		cXmlconfig::Save( m_profilename, ParameterMap, m_FavouriteList );
	}
	else
	{
		if( cSettings::GetInstance().ParameterMap[ "Username" ].compare( newUsername ) )
			cSettings::GetInstance().ParameterMap[ "Username" ] = newUsername;
		
		
		// Passwort nicht mitspeichern, wenn es leer gelassen wurde.
		if( strcmp( newPassword, "" ) )
		{
			cSettings::GetInstance().ParameterMap[ "Password_md5" ]
					= cUtils::MD5( newPassword );
		}
		
		
		// reset password
		strcpy( newPassword, "" );
		
		
#ifdef LASTFM_USE_SOFTDEVICE_RECEIVER
		if( cSettings::GetInstance().ParameterMap[ "Receivermode" ].compare( itoa( newReceivermode ) ) )
		cSettings::GetInstance().ParameterMap[ "Receivermode" ] = itoa( newReceivermode );
#endif //LASTFM_USE_SOFTDEVICE_RECEIVER
#ifdef LASTFM_HAVE_MAGICK
		if( cSettings::GetInstance().ParameterMap[ "Albumcovermode" ].compare( itoa( newAlbumcovermode ) ) )
		cSettings::GetInstance().ParameterMap[ "Albumcovermode" ] = itoa( newAlbumcovermode );
#endif //LASTFM_HAVE_MAGICK
		if( cSettings::GetInstance().ParameterMap[ "Osd_Mode" ].compare( itoa( newOsd_Mode ) ) )
			cSettings::GetInstance().ParameterMap[ "Osd_Mode" ] = itoa( newOsd_Mode );
		
		if( cSettings::GetInstance().ParameterMap[ "Osd_Refreshinterval" ].compare( itoa( newOsd_Refreshinterval ) ) )
			cSettings::GetInstance().ParameterMap[ "Osd_Refreshinterval" ]
					= itoa( newOsd_Refreshinterval );
		
		if( cSettings::GetInstance().ParameterMap[ "Osd_Autostartmode" ].compare( itoa( newOsd_Autostartmode ) ) )
			cSettings::GetInstance().ParameterMap[ "Osd_Autostartmode" ] = itoa( newOsd_Autostartmode );

		if( cSettings::GetInstance().ParameterMap[ "Scrobblemode" ].compare( itoa( newScrobblemode ) ) )
			cSettings::GetInstance().ParameterMap[ "Scrobblemode" ] = itoa( newScrobblemode );
		
	
		cSettings::GetInstance().FavouriteList = m_FavouriteList;
		
		// Store the data to the XML configfile:
		cXmlconfig::Save( m_profilename, cSettings::GetInstance().ParameterMap, m_FavouriteList );
	}
	
}

eOSState cSetupParameters::ProcessKey( eKeys Key )
{
	eOSState state=cOsdMenu::ProcessKey( Key );
	
	switch( state )
	{
		/** when user presses kBack, setup shall store even then,
		 * because of the favourites */
		case osBack:
			Store( );
			break;
			
		case osUnknown:
			if( Key==kOk )
			{
				if( !strcmp( Get(Current())->Text( ), tr( "Favourites..." ) ) )
				{
					return AddSubMenu ( new cSetupFavourites( m_FavouriteList ) );
				}
				else
				{
					Store( );
					state = osBack;
				}
				
			}
			break;
			
		default:
			break;
	}
	
	return state;
}
