/**
 * osd_userinterface_data_recentTracks.c: Presents metadata
 * 
 * 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 "osd_userinterface_data_recentTracks.h"
#include "settings.h"
#include "web_metadata.h"

cUserInterface_Data_recentTracks::cUserInterface_Data_recentTracks( std::string filterword )
:cOsdMenu( tr( filterword.c_str( ) ), 20 )
{
	m_filterword = filterword;
	if( strcmp( filterword.c_str( ), "loved" ) == 0 )
		m_filter = "recentlovedtracks";
	else if( strcmp( filterword.c_str( ), "banned" ) == 0 )
		m_filter = "recentbannedtracks";
	else
		m_filter = "recenttracks";

	cSettings::GetInstance().SetOsd_Path( m_filter.c_str( ) );
	// OSD-Elemente aufbauen
	BuildItems( );
}

cUserInterface_Data_recentTracks::~cUserInterface_Data_recentTracks( void )
{
	cSettings::GetInstance().SetOsd_Path( "data" );
}

void cUserInterface_Data_recentTracks::BuildItems( void )
{
	WebServiceConnector = new cWebServiceConnector();
	cMetaData_recentTracks
			MetaData_recentTracks = WebServiceConnector->recentTracks( m_filterword );

	if( WebServiceConnector )
	{
		delete WebServiceConnector;
		WebServiceConnector = 0;
	}

	std::vector<std::string>::iterator
			it_name = MetaData_recentTracks.m_name.begin( );
	std::vector<std::string>::iterator
			it_date = MetaData_recentTracks.m_date.begin( );

	for( std::vector<std::string>::iterator
			it = MetaData_recentTracks.m_artist.begin( ); it
			!= MetaData_recentTracks.m_artist.end( ); it++ )
	{
		std::string it_string = *it;

		std::string it_string_name = *it_name;
		std::string it_string_date = *it_date;

		Add( new cOsdItem( hk( it_string.c_str( ) ) ) );
		Add( new cOsdItem( hk( it_string_name.c_str() ) ) );
		Add( new cOsdItem( hk( it_string_date.c_str() ) ) );
		Add( new cOsdItem( hk( "" ) ) ); ///Trennzeile
		it_name++;
		it_date++;
	}

	Display( );
}

eOSState cUserInterface_Data_recentTracks::ProcessKey( eKeys Key )
{
	eOSState state = cOsdMenu::ProcessKey( Key );
	if( !strcmp( cSettings::GetInstance().GetOsd_Path( ), m_filter.c_str( ) ) )
	{
		switch( Key )
		{
		// cases f�r bereits belegte Tasten werden erw�hnt, damit die vom dar�berliegenden Men� gleicherma�en belegten Tasten ignoriert werden.
		case kOk:
			return osContinue;
		default:
			break;
		}
	}

	return state;
}
