// {{{ the iWear header
/**
 * $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
#define __AUDIO_PLAYER_H

#ifndef __TEXTDATA_H
#include <iwear_output/textdata.h>
#endif

#ifndef __AUDIODATA_H
#include <iwear_output/audiodata.h>
#endif

#ifndef __MENUDATA_H
#include <iwear_uiservices/menudata.h>
#endif

#ifndef __IMAGEDATA_H
#include <iwear_output/imagedata.h>
#endif

#ifndef __APPLICATION_H
#include <iwear_uiservices/application.h>
#endif

#ifndef __AUDIO_PLAYER_FUNCTOR_H
#include <iwear_audioplayer/audioplayerfunctor.h>
#endif

#ifndef __AUDIO_PLAYER_ENUM_H
#include <iwear_audioplayer/audioplayerenum.h>
#endif

#ifndef __AUDIO_DATA_FUNCTOR_H
#include <iwear_audioplayer/audiodatafunctor.h>
#endif

#ifndef __PLAYLIST_H
#include <iwear_audioplayer/playlist.h>
#endif

#ifndef __INISAVER_H
#include <iwear/inisaver.h>
#endif

#ifndef __INILOADER_H
#include <iwear/iniloader.h>
#endif

#ifndef __IWEAR_MOLOADER_H
#include <iwear/moloader.h>
#endif

#include <dirent.h>
#include <sys/types.h>
#include <fstream>
#include <stdio.h>

/**/
using namespace iwear::output;
/**/
using namespace iwear::uiservices;

namespace iwear {
    namespace audioplayer {

// forward declaration
class AudioPlayerFunctor;
class AudioDataFunctor;
class Playlist;	

class AudioPlayer : public Application
{
    friend class AudioDataFunctor;
    friend class AudioPlayerFunctor;

public:
    
    /**
     * The constructor
     * @param as A pointer to the application service
     */
    AudioPlayer(ApplicationService* as,
	    const std::string& dn = "audioplayer",
	    const std::string& dd = "The application plays sound files");

    /**
     * A special constructor which is required to load the audio
     * player as a plugin (Development version)
     */
    AudioPlayer( void );

    /**
     * The Destructor. 
     */
    virtual ~AudioPlayer(void);

    void Run(void);
    
protected:

    /** 
     * To initial the text_data, playlist, audio_data and menu_data
     */
    void Init_Application(void);

    /**
     * This function create four buttons for the submenu of the
     * audioplayer application. They are next, play_pause, back and
     * stop button. Furthermore it create a submenu for playlist
     * container.
     * 
     */
    void build_menu(void);

    /**
     * This function creates the submenu which contains the
     * available playlists
     */
    void build_playlist_submenu(void);

    //void build_append_submenu(void);

    /**
     * This function displays the playlist of the audioplayer
     * application.
     */
    void display_playlist(void);

    /**
     * This function displays the submenu of the audioplayer
     * application.
     */
    void display_menu(void);

    /**
     * This function plays a audio file, if the PLAY_PAUSE button of
     * the audioplayer's submenu was pressed by the first time. If the
     * button was pressed by the second time, it makes a pause und by
     * the thrid time plays continues.
     */
    void play_pause(void);

    /**
     * This function stops to play a audio file.
     */
    void stop(void);

    /**
     * This function plays the next song of the playlist.
     *
     * @param audio_finished A flag to indicate if the current audio
     * data finished. 
     */
    void next(bool audio_finished = false);
    
    /**
     * This function plays the previous song of the playlist.
     */
    void back(void);

    /**
     * Select new playlist
     */
    void set_playlist(const uint32_t button_id);

    inline void set_append(bool append_value){
	this->append = append_value;
    }

    void save_append_playlists(void);

    void change_default_playlist(void);

    bool is_saved(void);

    bool is_saved(string& playlist_path, string& playlist_string);

    void volume_quieter(void);

    void volume_louder(void);

    void Final_Application(void);

    virtual void set_default_configuration(void);
    
    AudioPlayerFunctor* audio_player_functor;

    AudioDataFunctor* audio_data_functor;

    /**
     * A pointer to a text data which contains the current
     * playlist representation
     */
    TextData* text_data;

    /**
     * The playlist container
     */
    Playlist* playlist;
    
    /**
     * A pointer to the audio data 
     */
    AudioData* audio_data;
    
    /**
     * A pointer to a menu data. It's used for Output Manager to
     * display the submenu of the audioplayer.
     */
    MenuData* menu_data;
    
    /**
     * A flag to controll if a play action should be done, when the
     * play_pause button was pressed.
     */
    bool played;

    /**
     * A flag to controll if a pause action or a resume
     * action should be done, when the play_pause button was pressed.
     */
    bool paused;

    /**
     * A flag to controll if a selected playlist should append to another
     * playlist.
     */
    bool append;

    uint32_t default_playlist_id;

    map<const uint32_t, string> button_id2playlist_path;
    
    map<const uint32_t, AudioPlayerButton> button_id2button_type;

    /** Displays the about information. */
    void display_about_info();

    /** Stops displaying the about information */
    void stop_about_info();

    /** Execute on menu element activation */
    void on_element_activated(const uint32_t button_id);

    /** Execute on menu element selection */
    void on_element_selected(const uint32_t button_id);

    
private:

    /** The about information image data. */
    ImageData* about_data;

    /** The id of the about button. */
    uint32_t about_button_id;

    /**
     * Loader object which is required for multi-language support
     */
    MoLoader* mo_loader;

};

    } // namespace audioplayer
} // namespace iwear

#endif //__AUDIO_PLAYER_H

