/***************************************************************************
 *   Copyright (C) 2006 by Jens Koehler                                    *
 *   irimi_at_gmx_de                                                       *
 *                                                                         *
 *   This program 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; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program 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 this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#ifndef _AMAROK_GATEWAY_
#define _AMAROK_GATEWAY_

#include <string>
#include <streambuf>
#include <vector>

#include "../Sockets.h"
#include "debugTracer.h"
#include "coregateway.h"



class AmarokGateway: public CoreGateway
{
public:

    AmarokGateway( const char* user,
                  const char* pw,
                  const char* album_cache_file,
                  const char* artist_cache_file,
                  const char* genre_cache_file );
    
    virtual ~AmarokGateway();
    bool checkVersion();
    virtual bool checkCache ();


protected:

    virtual bool buildArtistsResultlistCache ();
    virtual bool buildAlbumsResultlistCache ();
    virtual bool buildGenresResultlistCache ();

    virtual void addArtists(const std::vector<std::string> &artistList);
    virtual void addAlbums (const std::vector<std::string> &albumList);
    virtual void addTitles(const std::vector<std::string> &titleList);
    virtual void addGenres (const std::vector<std::string> &genreList);

    virtual bool requestData(const std::string &, std::vector<std::string> &result);
    virtual bool listRequest (TCPStream &stream, const int cmd, const std::string &parameter, std::vector<std::string>&result);


private:
    bool addMedia(const std::string &url);
    void addMedias(const std::vector<std::string> &list);


    bool queryAmarok(const std::string &query,std::vector<std::string> &result);
    bool queryArtist(const std::string &qArtist,std::vector<std::string> &result);
    bool queryAlbumArtist(const std::string &qAlbum,std::vector<std::string> &result);
    bool queryGenreArtist(const std::string &qGenre, std::vector<std::string> &result);
    bool queryAlbum(const std::string &qGenre,std::vector<std::string> &result);
    bool queryGenre(const std::string &qGenre,std::vector<std::string> &result);

    bool checkIgnoreReduceData(std::string &strLine);
    bool popen ( const std::string &command, std::vector<std::string> &result);
    bool readPlaylistFile (std::vector<std::string> &result);
    
    
    bool buildPlayResultlist (const std::string &cmd_parameter, std::vector<std::string> &result);
    bool buildArtistsResultlist (std::vector<std::string> &result);
    bool buildAlbumsResultlist (std::vector<std::string> &result);
    bool buildGenresResultlist (std::vector<std::string> &result);


    void getParameterRequest (TCPStream &stream,std::vector<std::string>&parameterList);

    struct Entity
    {
        const char*     str;
        unsigned int    strLength;
        char            chr;
    };
    enum
    {
        NUM_ENTITY = 5,
        MAX_ENTITY_LENGTH = 6

    };
    static Entity entity[ NUM_ENTITY ];


    const char *p_user;
    const char *p_pw;
    bool b_login;

    const char *p_album_cache_file;
    const char *p_artist_cache_file;
    const char *p_genre_cache_file;

};

#endif /*_AMAROK_GATEWAY_*/
