/**
 * $Id$
 * $Revision$
 * $Author$
 * $Date$
 *
 * This file is part of The iWear Framework.
 * In particular this file is part of the Framework Test Library
 *
 * The iWear Framework is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by the
 * Free Software Foundation as in version 2 of the License.

 * 
 * The iWear Framework is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * The iWear Framework; if not, write to the Free Software Foundation, Inc., 59
 * Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifndef __AUDIO_PLAYER_H
#include <iwear_test/audioplayer.h>
#endif

using namespace iwear::test;

namespace iwear{
namespace test{


void AudioPlayer::notify_destroy_odo(const uid& odo_uid)
{
    map<OutputData*,bool>::iterator iter;
    
    for(iter = this->displayables.begin();
	iter != this->displayables.end(); iter++){
	if(iter->first->get_uid() == odo_uid){
	    this->displayables.erase(iter);
	}
    }
}

MenuData* AudioPlayer::build_menu(void)
{
    MenuData* tmp_menu = new MenuData("Menu", 
				      "Audio Player Menu", 
				      *this,
				      output_manager);

    // create a exit level button
    /*
    const Button* exit_button = &(tmp_menu->add_button("images/button.png",
					       "Exit",
					       "Guess what!",
					       Rgb(255,255,255),true));
    this->buttons.insert(std::make_pair("Exit", exit_button));
    
    ExitFunctor* exit_functor = new ExitFunctor(exit_button);
    tmp_menu->register_odo_listener(*exit_functor, LISTEN_CONTENT);
    */

    // create a audio player submenu
     this->menus.
 	insert(make_pair("audioplayer",
 			 tmp_menu->add_submenu("images/submenu.png",
 					       "audioplayer",
 					       "iWear audio player",
 					       Rgb(200,200,200),true)));
     // create the play button
     const Button* play_button = 
	 &(tmp_menu->add_button(this->menus.find("audioplayer")->second,
				"images/button.png",
				"Start",
				"Guess what!",
				Rgb(255,255,255),true));
     this->buttons.insert(std::make_pair("Start", play_button));
     AudioPlayFunctor* play_functor = 
	 new AudioPlayFunctor(play_button, &(this->output_manager), this->audio_data);
     tmp_menu->register_odo_listener(*play_functor, LISTEN_CONTENT);

     // create the pause button
     const Button* pause_button = 
	 &(tmp_menu->add_button(this->menus.find("audioplayer")->second,
				"images/button.png",
				"Pause",
				"Guess what!",
				Rgb(255,255,255),true));
     this->buttons.insert(std::make_pair("Pause", pause_button));
     AudioPauseFunctor* pause_functor = 
	 new AudioPauseFunctor(pause_button, this->audio_data);
     tmp_menu->register_odo_listener(*pause_functor, LISTEN_CONTENT);

     // create the stop button
     const Button* stop_button = 
	 &(tmp_menu->add_button(this->menus.find("audioplayer")->second,
				"images/button.png",
				"Stop",
				"Guess what!",
				Rgb(255,255,255),true));
     this->buttons.insert(std::make_pair("Stop", stop_button));
     AudioStopFunctor* stop_functor = 
	 new AudioStopFunctor(stop_button, &(this->output_manager),this->audio_data);
     tmp_menu->register_odo_listener(*stop_functor, LISTEN_CONTENT);

     return tmp_menu;
}


void AudioPlayer::display_data(void)
{
    map<OutputData*,bool>::iterator iter;
    for(iter = this->displayables.begin();
	iter != this->displayables.end(); iter++)
    {
	OutputData* tmp = iter->first;
	switch(tmp->get_output_data_type())
	    {
// 	    case IMAGE_DATA: 
// 		this->output_manager.
// 		    display(static_cast<ImageData*>(tmp),false,false);
// 		iter->second = true;
// 		break;
	    case MENU_DATA:
		this->output_manager.
		    display(static_cast<MenuData*>(tmp),false,false);
		iter->second = true;
		break;
	    default:
		// No matter what it is, there's no chance to display it.
		break;
	    }
    }
}


AudioPlayer::AudioPlayer(OutputManager& output_manager,
		   const string& application_name,
		   const string& application_description,
		   const string& wav_file_url,			 
		   const string& icon_path)
    : DisplayCapable(application_name,
		     application_description,
		     icon_path),
      output_manager(output_manager)
{
    this->audio_data = new AudioData("Rock",
				     "Rock2",
				     *this, 
				     output_manager, 
				     wav_file_url,
				     RAW);
    this->menu = this->build_menu();
    this->displayables.
	insert(make_pair<OutputData*,bool>(this->menu,false));
}


AudioPlayer::~AudioPlayer(void)
{
    map<OutputData*, bool>::iterator iter;
    for(iter = this->displayables.begin();
	iter != this->displayables.end(); iter++)
    {

	d_nons << ANSI_CYAN << __FFL__ 
	       << ANSI_NORMAL 
	       << "\n\tAnwendung hat Displayables: " 
	       << this->displayables.size() << endl;

	if(iter->second)
	{
	    OutputData* tmp = iter->first;
	    switch(tmp->get_output_data_type())
		{
//  		case IMAGE_DATA: 
//  		    this->output_manager.
//  			stop_displaying(static_cast<ImageData*>(tmp));
//  		    iter->second = true;
//  		    break;
		case MENU_DATA:
		    this->output_manager.
			stop_displaying(static_cast<MenuData*>(tmp));
		    iter->second = true;
		    break;
		default:
		    // No matter what it is, there's no chance to display it.
		    break;
		}
	}
  
    }
}

// }}}


} // namespace test
} // namespace iwear

