/**
 * osd_userinterface_submenu.c: Ssubmenu for several items
 * 
 * Copyright: See COPYING file that comes with this distribution
 *
 * Author: Matthias Feistel <hitman_47@users.sourceforge.net>, (C) 2006, 2007
 */

#include <vector>
#include <vdr/interface.h>
#include <vdr/menu.h>
#include <vdr/plugin.h>
#include <sstream>
#include "osd_userinterface_submenu.h"
#include "settings.h"
#include "osd_userinterface_data.h"
#include "osd_userinterface_search.h"

cUserInterface_Submenu::cUserInterface_Submenu( void )
:cOsdMenu( tr( "Submenu" ), 20 )
{
	///    memset(&Globals::m_searchartist, 0, sizeof(Globals::m_searchartist));
	WebServiceConnector = new cWebServiceConnector();	
	
	// OSD-Elemente aufbauen
	BuildItems( );
}

cUserInterface_Submenu::~cUserInterface_Submenu( void )
{
	if( WebServiceConnector )
	{
		delete WebServiceConnector;
		WebServiceConnector = 0;
	}
}

void cUserInterface_Submenu::BuildItems( void )
{
	Add( new cOsdItem(hk( tr( "Metadata...") ) ) );
	Add( new cOsdItem(hk( tr( "Search...") ) ) );
	Add( new cOsdItem(hk( tr( "Personal Radio") ) ) );
	Add( new cOsdItem(hk( tr( "Neighbourhood Radio") ) ) );
	
	Display( );
}

eOSState cUserInterface_Submenu::ProcessKey( eKeys Key )
{
	eOSState state = cOsdMenu::ProcessKey( Key );
	
	if( state == osUnknown )
	{
		switch( Key )
		{
			// cases fuer bereits belegte Tasten werden erwaehnt, damit die vom darueberliegenden Menue gleichermassen belegten Tasten ignoriert werden.
			case kOk:
				if( Current( ) == 0 )  // Current() describes the entry's position starting with 0
				{
					cSettings::GetInstance().SetOsd_Path( "data" );
					AddSubMenu( new cUserInterface_Data() );
				}
				else if( Current( ) == 2 )
				{
					cSettings::GetInstance().SetOsd_Path( "submenu" );
					
					std::stringstream sstream;
					std::string s3;
					sstream << "user/"<< cSettings::GetInstance().ParameterMap[ "Username" ]<< "/personal";
					s3 = sstream.str();
					
					int rc;
					rc = WebServiceConnector->Changestation( s3 );
					
					if( rc != 0 )
					{
						if( rc == 3 )
						{
							Skins.Message(
									mtError,
									tr( "Feature only available to subscribers!" ) );
						}
					}
					else cRemote::Put( kBack );  // step back to mainmenu after success
				}
				else if( !strcmp( Get(Current())->Text( ), tr( "Search..." ) ) )
				{
					cSettings::GetInstance().SetOsd_Path( "search" );
					AddSubMenu( new cUserInterface_Search() );
				}
				else if( Current( ) == 3 )
				{
					cSettings::GetInstance().SetOsd_Path( "submenu" );
					
					std::stringstream sstream;
					std::string s3;
					sstream << "user/"<< cSettings::GetInstance().ParameterMap[ "Username" ]<< "/neighbours";
					sstream >> s3;
					
					int rc;
					rc = WebServiceConnector->Changestation( s3 );
					
					if( rc != 0 )
					{
							Skins.Message(
									mtError,
									tr( "Action was not successful!" ) );
					}
					else cRemote::Put( kBack );  // step back to mainmenu after success
					
				}
				
			default:
				break;
		}
	}
	
	return state;
}
