/**
 * $Id$
 * $Revision$
 * $Author$
 * $Date$
 *
 * This file is part of The iWear Framework.
 * In particular this file is part of the Framework Griffin
 *
 * 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 __DISPLAYHANDLER_H
#define __DISPLAYHANDLER_H

// Standard includes
#include <stdlib.h>

// iWear includes
#include <iwear/thread.h>
#include <iwear/threadlocked.h>
#include <iwear_output/outputdata.h>
#include <iwear_output_griffin/displayevent.h>
#include <iwear_output_griffin/layoutmanager.h>
#include <iwear/configuration.h>

// Other includes
#include <SDL/SDL.h>
#include <SDL/SDL_thread.h>
#include <SDL/SDL_ttf.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_rotozoom.h>
#include <SDL/SDL_imageFilter.h>



namespace iwear{
namespace output{

/**
 * @author Carsten Rachuy
 */
class DisplayHandler : public ThreadLocked {

 public:

    /**
     * Constructor
     */
    DisplayHandler(const string& type, Configuration& configuration, LayoutManager& layout_manager);
    
    /**
     * Destructor
     */
    virtual ~DisplayHandler(void);

    /**
     * Command for the handler to store the output data
     * supported internally and, if the application 
     * is the currently active one, display it
     */
    virtual void handle(OutputData* output_data) = 0;

    /**
     * Command for the handler to discard the
     * appropriate output data, throw it away and
     * stop updating the screen with it's content
     */
    virtual void discard(OutputData* output_data) = 0;

    /**
     * This method is called when a application switch
     * occured and has to be processed by the handler.
     */
    virtual void switch_focus_to(const uid& application_id) = 0;

    /**
     * The main 'display' method which blits the appropriate
     * data which should be displayed into the SDL_Surface provided
     */
    virtual void render(SDL_Surface* sdl_surface) = 0;

    /**
     * Push back an event into the queue
     */
    void push_event(DisplayEvent* display_event);

    /**
     * Locks the event queue and throws away everything
     */
    void lock_queue_and_discard_events(void);
    
    /**
     * @return the type of the handler
     */
    inline const string& get_type(void){
	return this->type;
    }

    /**
     * Return the number of output data objects this very module can
     * handle.
     */
    inline virtual const uint32_t get_number_of_displayable_output_data(void) = 0;

    /**
     * Returns true if this handler needs a redraw
     */
    inline bool need_redraw(void){
	return ( (this->event_queue.size() >= 1) || 
	 	  this->changed || 
		  this->layout_manager.render_area_changed(this->active_application_id,this->type));
    }

    /**
     * Get the actual active application id of this handler
     */
    inline uid& get_active_application_id(void){
	return this->active_application_id;
    }


 protected:

    /**
     * Configuration which can be read...
     */
    Configuration& configuration;

    /**
     * The layout manager
     */
    LayoutManager& layout_manager;

    /**
     * The type of the display handler
     */
    const string type;

    /**
     * The uid of the currenty displayed applictaion
     */
    uid active_application_id;

    /**
     * Pop the first event in the queue and return a pointer to it.
     * Note that the object has to be destroyed by the user after 
     * processing it.
     */
    DisplayEvent* pop_event(void);

    /**
     * Indicates whether the actual displayed content has changed or
     * not during the last cycle
     */
    bool changed;

 private:

    /**
     * Boolean which defines whether the event_queue is open or locked
     */
    bool event_queue_locked;

    // TODO: REMOVE
 protected:
    /**
     * The event queue
     */
    queue<DisplayEvent*> event_queue;

};


} // namespace output
} // namespace iwear

#endif // __DISPLAYHANDLER_H

