/**
 * $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 __TEST_APP_H
#include <iwear_test/testapp.h>
#endif

#include <iwear_test/showmapfunctor.h>

namespace iwear{
namespace test{


void TestApp::notify_destroy_odo(const uid& odo_uid)
{
// {{{ method details

    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* TestApp::build_menu(void)
{
// {{{ method details

    MenuData* tmp_menu = new MenuData("Menu", 
				      "Griffin Test Menu", 
				      *this,
				      output_manager);

    // create a top level button
    this->buttons.insert(std::make_pair("riddle",
		       &(tmp_menu->add_button("images/button.png",
					      "riddle",
					      "Guess what!",
					      Rgb(255,255,255),true))));
    // create a top 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 submenu
    this->menus.
	insert(make_pair("iwearmap",
 			 tmp_menu->add_submenu("images/button1.png",
 					       "iwearmap",
 					       "iWearMap",
 					       Rgb(200,200,200),true)));
    // create the show-map button
    const Button* show_button = 
	&(tmp_menu->add_button(this->menus.find("iwearmap")->second,
			       "images/button.png",
			       "ShowMap",
			       "Guess what!",
			       Rgb(255,255,255),true));
    this->buttons.insert(std::make_pair("ShowMap", show_button));
    ShowMapFunctor* show_functor = new ShowMapFunctor(show_button, this);
    tmp_menu->register_odo_listener(*show_functor, LISTEN_CONTENT);
    
    return tmp_menu;

// }}}
}


void TestApp::display_data(void)
{
// {{{ method details

    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;
	    }
    }

// }}}
}

void TestApp::display_picture(string path){
    ImageData* new_image
	= new ImageData("map",
			"Map", 
			*this,
			output_manager,
			path, 
			JPG);

    this->displayables.
	insert(make_pair<OutputData*,bool>(new_image,false));
    
    output_manager.display(new_image, false, false);

    output_manager.stop_displaying(this->image);
    displayables.erase(this->image);
    
    this->image = new_image;
}


TestApp::TestApp(OutputManager& output_manager,
		   const string& application_name,
		   const string& application_description,
		   const string& icon_path)
    : DisplayCapable(application_name,
		     application_description,
		     icon_path),
      output_manager(output_manager)
{
// {{{ constructor details    

    this->image
	= new ImageData("linn",
			"Burg Linn", 
			*this,
			output_manager,
			"images/linn.png", 
			PNG);

    this->menu = this->build_menu();

    this->displayables.
	insert(make_pair<OutputData*,bool>(this->image,false));
    this->displayables.
	insert(make_pair<OutputData*,bool>(this->menu,false));
// }}}
}


TestApp::~TestApp(void)
{
// {{{ destructor details    

    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 griffin
} // namespace iwear

