/**
 * osd_userinterface_profileselections.c: Presents profiles to be selected
 * 
 * Copyright: See COPYING file that comes with this distribution
 *
 * Author: Matthias Feistel <hitman_47@users.sourceforge.net>, (C) 2007
 */

#include "osd_userinterface_profileselection.h"
#include "osd_userinterface.h"
#include "settings_xml.h"
#include "settings.h"

cUserInterface_Profileselection::cUserInterface_Profileselection( void )
:cOsdMenu(tr( "profileselection" ) )
{
	BuildItems( );
}

void cUserInterface_Profileselection::BuildItems( void )
{
	Clear( );
	
	
	// Profileselection (or creation of a new profile):
	std::list<std::string> profilenames = cXmlconfig::GetProfilenames( );
	
	for( std::list<std::string>::iterator it = profilenames.begin( ); it
			!= profilenames.end( ); it++ )
	{
		std::string it_string = *it;
		
		if( !it_string.empty( ) )
			Add( new cOsdItem( it_string.c_str( ) ) );
	}
	
}

eOSState cUserInterface_Profileselection::ProcessKey( eKeys Key )
{
	eOSState state = cOsdMenu::ProcessKey ( Key );
	
	std::map<std::string,std::string> ParameterMap;
	std::list<std::string> FavouriteList;
	
	if( state == osUnknown )
	{
		switch( Key )
		{
			case kOk:
				cSettings::GetInstance().CurrentProfilename = Get( Current() )->Text();
				
				
				
				// Load certain profile:

				cXmlconfig::Load( cSettings::GetInstance().CurrentProfilename,
						ParameterMap, FavouriteList );
				
				for( std::map<std::string,std::string>::iterator elParameterMap = ParameterMap.begin( );
					 elParameterMap != ParameterMap.end( );
					 elParameterMap++
				)
				{
						cSettings::GetInstance().ParameterMap[ elParameterMap->first.c_str() ] = elParameterMap->second;
				}

				for( std::list<std::string>::iterator it = FavouriteList.begin( );
					it != FavouriteList.end( );
					it++ )
				{
					std::string it_string = *it;
					cSettings::GetInstance().FavouriteList.push_back( it_string );
				}
				
				

				cRemote::CallPlugin("lastfm");
				break;
				
			default:
				break;
		}
		
	}
	
	return state;
}
