/* Network-Integrated Multimedia Middleware (NMM)
 * http://www.motama.com/nmm.html
 *  
 * Copyright (C) 2006-2007
 * Motama GmbH, Saarbruecken, Germany
 * http://www.motama.com
 *  
 * This file is part of NMM. 
 * 
 * NMM 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; only version 2 of the License.      
 * 
 * NMM 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 NMM; if not, write to the Free Software Foundation,    
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#ifndef _NMM_OSDMANAGERNODE_HPP_
#define _NMM_OSDMANAGERNODE_HPP_ _NMM_OSDMANAGERNODE_HPP_

#include "nmm/nmm_prefix.hpp"

#include "nmm/multimedia/GenericMultiplexerNode.hpp"

#include "nmm/plugins/video/osd/manager/interfaces/IOSDManagerImpl.hpp"
#include "nmm/plugins/video/osd/manager/interfaces/IWidgetFactoryImpl.hpp"
#include "nmm/plugins/video/osd/manager/interfaces/IBitmapReader.hpp" // XXX: (fw) currently the OSDManagerNode implements IBitmapReader

#include "Window.hpp"

#define RGBA32 0
#define YVA12  1

#include "visibility.hpp"

namespace NMM {
  class Widget;  
  class MessageUnit;
  class TimeUnit;
  class ProgressbarUnit;
  class SymbolUnit;
  class ColorUnit;
  class BitmapReader;
  class BitmapFont;
  class ColorBarUnit;

  class DLLEXPORT OSDManagerNode : public GenericMultiplexerNode,
                         public IOSDManagerImpl,
                         public IWidgetFactoryImpl,
                         public IBitmapReaderImpl // XXX: (fw) currently the OSDManagerNode implements IBitmapReader
                                                  // XXX: (fw) (years later) Yeah, that's pretty obivous ... so probably it means this is bad?!
  {
    
    typedef map<int, Window*> windows_t;
    typedef map<int, Widget*> widgets_t;
    typedef map<string, BitmapFont*> fonts_t;
    
  private:
    Buffer*       lastBuffer;
    bool          paused;
    int           max_window_id;
    int           max_widget_id;
    windows_t     windows;      // map of window entries
    widgets_t     widgets;      // map of widgets
    fonts_t       fonts;
    BitmapReader* bmreader;
    bool          send_symbol_change;
    string        new_symbol;
    bool          doupdate;
    
    MessageUnit*     message;
    TimeUnit*        time;
    ProgressbarUnit* bar;
    ColorBarUnit*    color_bar;
    SymbolUnit*      symbol;
    bool             create_time;
    
    ThreadMutex      osd_mutex;
    int image_width;        ///< width of the image in the input stream
    int image_height;       ///< height of the image in the input stream
    int ratio_x;            ///< x-aspect-ratio of the image in the input stream
    int ratio_y;            ///< y-aspect-ratio of the image in the input stream
    int reference_width;    // this is the width we scale to
    int reference_height;   // this is the height we scale to
    double scale_factor_x;
    double scale_factor_y;
    int color;              ///< colorspace: RGBA32 or YVA12
    Timestamp old_timestamp;
    
    Buffer* out_buffer;
    bool send_format_changed;
    bool format_changed_sent;
    bool save_working_flag;
    Buffer* save_in_buffer;

  protected:
    virtual Result doInit();
    virtual Result doInitOutput();
    virtual Result doFlush();
    virtual Result doStart();
    virtual Result doStop();
    
    virtual Message* processBuffer(Buffer *in_buffer);
    
    // BEGIN> IOSManager interface
    virtual Result loadFont(const string& id, const string& xmlfilename, const string& fontname);
    virtual list<string> getFontList() const;
    virtual Result update();
    virtual IWidget* getWidget(const nmm_int32& id);
    virtual IWindow* getWindow(const nmm_int32& id);
    virtual IWindow* createWindow();
    virtual void deleteWindow(const IWindow& window);
    virtual void deleteWidget(const IWidget& widget);
    virtual Result displayMessage(const string& header, const string& text, const string& label1, const string& label2 = "",
                                  const string& label3 = "", const nmm_uint32& sel = 0);
    virtual Result deleteMessage();
    virtual Result displayTime(const string& header = "", const nmm_uint32& x = 500, const nmm_uint32& y = 410);
    virtual Result createTime(const nmm_bool& ct);
    virtual Result resetTime();
    virtual Result hideTime();
    virtual Result setTime(const nmm_uint64& time);
    virtual nmm_uint64 getTime();
    virtual Result displaySymbol(const string& symbol, const nmm_uint32& x = 600, const nmm_uint32& y = 50);
    virtual Result hideSymbol();
    virtual Result displayProgressBar(const string& header = "", const nmm_uint32& x = 50, const nmm_uint32& y = 470,
                                      const nmm_uint32& width = 620);
    virtual Result hideProgressBar();
    virtual void setMaxProgress(const nmm_uint64& max);
    virtual void setProgress(const nmm_uint64& progress);
    virtual void updateProgress();

    virtual Result displayColorBar(const nmm_uint32& x = 80, const nmm_uint32& y = 480);
    virtual Result hideColorBar();
    virtual void setColorBarText(const string& red, const string& green, const string& yellow, const string& blue);
    virtual void setRedColorBarText(const string& text);
    virtual void setGreenColorBarText(const string& text);
    virtual void setYellowColorBarText(const string& text);
    virtual void setBlueColorBarText(const string& text);
    virtual string getRedColorBarText();
    virtual string getGreenColorBarText();
    virtual string getYellowColorBarText();
    virtual string getBlueColorBarText();

    virtual Result messageLeft();
    virtual Result messageRight();
    virtual string messageID();
    virtual void setPause(const nmm_bool& value);
    virtual void sendPauseEvent();
    virtual IBitmapReader* getBitmapReader();
    
    virtual void checkCharDimension(const string& id, const nmm_uchar& c, nmm_uint32& x, nmm_uint32& y) const;
    virtual nmm_uint32 getMaxWidth(const string& id) const;
    virtual nmm_uint32 getMaxHeight(const string& id) const;
    virtual void setLineSpace(const string& id, const nmm_uint32& space);
    virtual nmm_uint32 getLineSpace(const string& id) const;
    // END< IOSManager interface
    
    // BEGIN> IWidgetFactory interface
    virtual IWidgetComposite* createWidgetComposite();
    virtual IProgressBar* createProgressBar();
    virtual ITextView* createTextView();
    virtual ITimeView* createTimeView();
    virtual IImageView* createImageView();
    virtual IRadioButtons* createRadioButtons();
    virtual IButtons* createButtons();
    virtual IBorderDecorator* createBorderDecorator(const IWidget& internal);
    virtual IHeaderDecorator* createHeaderDecorator(const IWidget& internal);
    virtual IPlaneDecorator* createPlaneDecorator(const IWidget& internal);
    virtual IScrollDecorator* createScrollDecorator(const IWidget& internal);
    // END< IWidgetFactory interface
    
    // BEGIN> IBitmapReader interface
    virtual nmm_bool loadImage(const string& id, const string& filename, const IBitmapReader::Format& f);
    virtual void getImage(const string& id, nmm_uint32& w, nmm_uint32& h);
    virtual nmm_bool createImage(const string& id, const nmm_uint32& width, const nmm_uint32& height,
                                 const nmm_uint32& color1, const nmm_uint32& color2, const nmm_uint32& color3,
                                 const IBitmapReader::Format& f);
    virtual void freeImage(const string& id);
    virtual void freeAll();
    // END< IBitmapReader interface
    
    // BEGIN> IFormatChange interface
    virtual Result setFormat(const Format& format);
    // END< IFormatChange interface
    
    void sendSymbolChange(const string& sym);
    
  public:
    OSDManagerNode(const char *name = "OSDManagerNode",
                   const StreamQueue::Mode queue_mode = StreamQueue::MODE_SUSPEND,
                   const int queue_size = 1 );
    virtual ~OSDManagerNode();
    
    
    /**
     * Get own widget.
     * Returns a pointer to the local widget object from an interface.
     * This is done using the widget ID. We assume that the widget that the interface
     * refers to was created locally.
     */
    Widget* getOwnWidget(const IWidget& iwidget);
    
    /**
     * Get own window.
     * Returns a pointer to the local window object from an interface.
     * This is done using the window ID. We assume that the window that the interface
     * refers to was created locally.
     */
    Window* getOwnWindow(const IWindow& iwindow);
    
    /** Get own widget by id. */
    Widget* getOwnWidget(int id);

    /** Get own window by id. */
    Window* getOwnWindow(int id);
    
    
    /** Add a window. */
    void addWindow( Window* win );
    
    /** Remove a window. */
    void delWindow( Window* win );
    
    /** Add a widget. */
    void addWidget( Widget* win );
    
    /** Remove a widget. */
    void delWidget( Widget* win );
    
    
    void setFontList( fonts_t f ) { fonts = f; };
    void setBitmapReader( BitmapReader* br ) { bmreader = br; };
    
    /**
     * add font to list
     * \param id font identifier
     * \param font pointer to bitmap font
     */
    void setFont( string id, BitmapFont* font );
    
    /**
     * \returns pointer to Bitmapfont
     * \param id font identifier
     */
    BitmapFont* getFont( string id ) const;
  };
}

#include "nmm/nmm_suffix.hpp"

#endif //_NMM_VIDEO_FILTER_OSDMANAGERNODE_HPP_
