/***************************************************************************
 *   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 _CORE_GATEWAY_
#define _CORE_GATEWAY_

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

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


class CoreGateway
{
public:

    CoreGateway( const char* user,
                 const char* pw);

    virtual ~CoreGateway();
    void runGateway(int port, bool newCache);
    virtual bool checkVersion()=0;

    bool updateCache();

    DebugTracer tracer;

protected:

    bool handleLogin(const std::string &userpasswd);
    /** request data from local application*/
    virtual bool requestData(const std::string &, std::vector<std::string> &result)=0;
    
    /** request data from net application*/
    virtual bool listRequest (TCPStream &stream, const int cmd, const std::string &parameter, std::vector<std::string>&result);

    virtual void addRequest (TCPStream &stream, int int_cmd);

    /** send data to net application*/
    virtual void sendAnswer (TCPStream &stream, const int cmd, std::vector<std::string>&result, bool ok=true);
    virtual void sendAnswer (TCPStream &stream, const int cmd, bool ok=true );
    
    bool saveCache(const char* file,std::vector<std::string> &result);
    bool loadCache(const char* file,std::vector<std::string> &result);

    virtual bool checkCache ()=0;
    virtual bool buildArtistsResultlistCache ()=0;
    virtual bool buildAlbumsResultlistCache ()=0;
    virtual bool buildGenresResultlistCache ()=0;

    virtual bool buildPlayResultlist (const std::string &cmd_parameter,std::vector<std::string> &result)=0;
    virtual bool buildArtistsResultlist (std::vector<std::string> &result)=0;
    virtual bool buildAlbumsResultlist (std::vector<std::string> &result)=0;
    virtual bool buildGenresResultlist (std::vector<std::string> &result)=0;

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


private:
    bool transreceiveData(TCPStream &stream);

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


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


};

#endif /*_CORE_GATEWAY_*/
