/**
 * osd_userinterface_search.c: Search for artists and tags
 * 
 * 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_search.h"
#include "settings.h"
#include "osd_userinterface_search_favourites.h"

cUserInterface_Search::cUserInterface_Search( void )
:cOsdMenu( tr( "search" ), 20 )
{
	cSettings::GetInstance().SetOsd_Path( "search" );
	strncpy( m_searchword, "", 511 );

	WebServiceConnector = new cWebServiceConnector();

	// OSD-Elemente aufbauen
	BuildItems( );
}

cUserInterface_Search::~cUserInterface_Search( void )
{
	DELETENULL( WebServiceConnector );
	cSettings::GetInstance().SetOsd_Path( "submenu" );
}

void cUserInterface_Search::BuildItems( void )
{
	SetHelp( 0, 0, 0, tr( "Favourites..." ) );

	Add ( new cMenuEditStrItem ( tr ( "similarArtist" ),
			m_searchword, sizeof( m_searchword ), FileNameChars ) );
	Add ( new cMenuEditStrItem ( tr ( "Tag" ),
			m_searchword, sizeof( m_searchword ), FileNameChars ) );

	Display( );
}

eOSState cUserInterface_Search::ProcessKey( eKeys Key )
{
	std::string searchelement;

	eOSState state = cOsdMenu::ProcessKey( Key );
	if( !strcmp( cSettings::GetInstance().GetOsd_Path( ), "search" ) )
	{
		switch( Key )
		{
		// cases fuer bereits belegte Tasten werden erwaehnt, damit die vom darueberliegenden Menue gleichermassen belegten Tasten ignoriert werden.
		case kOk:
			///Aritsts koennen hier eingegeben und gesucht werden
			///            if ( strcmp( Get( Current() )->Text(), tr("similarArtist") ) > 0 )

			searchelement = Get( Current() )->Text( );

			if( searchelement.find( tr( "similarArtist" ) )
					!= std::string::npos )
			{
				char buff[ 512 ];
				snprintf( buff, sizeof(buff ), "artist/%s/similarartists",m_searchword );
                int rc;
                rc = WebServiceConnector->Changestation( buff );
                
                if(rc != 0)
                {
                    if( rc == 2 )
                    {
                        Skins.Message( mtError, tr( "No such artist!" ) );
                    }
                }
            }

            ///Globaltags koennen hier eingegeben und gesucht werden
            ////       else if( strcmp( Get( Current() )->Text(), tr("Tag") ) > 1 )
            else if ( searchelement.find( tr( "Tag" ) ) != std::string::npos )
            {
                ///rlog << "transmitting globaltag";

                char buff[ 512 ];
                snprintf( buff, sizeof( buff ), "globaltags/%s", m_searchword );
                int rc;
                rc = WebServiceConnector->Changestation( buff );
                if( rc != 0 )
                {
                    ///rlog << "WebServiceConnector->changeStation: rc=" << rc;

                    if( rc == 2 )
                    {
                        ///rlog << "WebServiceConnector->changeStation: No such globaltags found!";
                        Skins.Message( mtError, tr( "No such globaltags found!" ) );
                    }
                }
            }

            return osBack;

            ///Hier werden die Favoriten behandelt
        case kBlue:
            if( strcmp( Get(Current() )->Text(), tr ( "similarArtist" ) ) > 0 )
            {
                cSettings::GetInstance().SetOsd_Path( "search_favourites" );
                AddSubMenu( new cUserInterface_Search_Favourites( ( char*)m_searchword ) );
            }
            else if( strcmp( Get(Current() )->Text(), tr( "Tag" ) ) > 0)
            {
                cSettings::GetInstance().SetOsd_Path( "search_favourites" );
                AddSubMenu( new cUserInterface_Search_Favourites( ( char*)m_searchword ) );
            }

            break;

        case kBack:
            return osBack;

        default:
            state = osContinue;
            break;

        } // switch

    } // if(!strcmp(cSettings::GetInstance().GetOsd_Path(), "search"))

    return state;
}
