/***************************************************************************
 *   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.             *
 ***************************************************************************/

#include <iostream>
#include <string>
#include "amarokClient.h"

#include <vdr/tools.h>

using namespace std;


AmarokClient::AmarokClient() : p_stream (0), b_connected(false)
{}

AmarokClient::~AmarokClient()
{
  delete p_stream;
}
bool AmarokClient::connect(const char* host, int port)
{
  try
  {
    p_stream = new TCPClientStream (host, port);
  }
  catch (const SocketRunTimeException &e)
  {
    esyslog("socket exception: %s",e.what());
    return false;
  }
  b_connected=true;
  return true;
}

bool AmarokClient::command(const string &str_cmd)
{
  if (isConnected())
  {
    AmarokData tmp = CMD_REQUEST_SET;
    return command(str_cmd,tmp);
  }
  return false;

}


bool AmarokClient::command(const string &str_cmd,AmarokData &data)
{
  if (isConnected())
  {
    try
    {
      //cout << "sendCmd(" << data.getType() << " " << str_cmd << endl;
      sendCmd((int)data.getType());
      sendCmd(str_cmd);
      return handleAnswer (data );
    }
    catch (const SocketRunTimeException &e)
    {
      esyslog("socket exception: %s",e.what());
    }
  }
  return false;

}



bool AmarokClient::handleAnswer(AmarokData &data)
{

  string answer;
  int int_answ;
  bool bRet = false ;
  switch (data.getType())
  {
  case CMD_REQUEST_LOGIN:
    waitforAnswer(int_answ);
    if (int_answ == CMD_REQUEST_LOGIN)
    {
      bRet = true;
    }

    break;
  case CMD_REQUEST_PLAYLIST:
    waitforAnswer(int_answ);
    if (int_answ == CMD_REQUEST_PLAYLIST)
    {
      data.clearList();
      do
      {
        getline(*p_stream, answer);

        if (!answer.empty())
          data.addData(answer);

      }
      while (answer.empty() || answer.find("</playlist>")!=0);
      data.buildList();
      bRet = true;
    }
    break;
  case CMD_REQUEST_ARTISTLIST:
  case CMD_REQUEST_GENRELIST:
  case CMD_REQUEST_ALBUMLIST:
  case CMD_REQUEST_STATISTICS:
  case CMD_REQUEST_LIST:
    waitforAnswer(int_answ);
    if (int_answ == data.getType())
    {
      data.clearList();
      do
      {
        getline(*p_stream, answer);
        if (!answer.empty())
            data.addData(answer);
      }
      while (answer.empty() || answer.find("</list>")!=0);
      data.buildList();
      bRet = true;
    }

    break;
  case CMD_REQUEST_GET:
    waitforAnswer(int_answ);
    if (int_answ == CMD_REQUEST_GET)
    {
      do
      {
        getline(*p_stream, answer);
        if (!answer.empty())
          data.addData(answer);
      }
      while (answer.empty());
      bRet = true;
    }

    break;
  case CMD_REQUEST_SET: // no data !
    waitforAnswer(int_answ);
    bRet = (int_answ == ((int)data.getType()));
    break;
  case CMD_REQUEST_ADDARTIST:
  case CMD_REQUEST_ADDGENRE:
  case CMD_REQUEST_ADDALBUM:
    waitforAnswer(int_answ);
    bRet = (int_answ == ((int)data.getType()));
    break;
  case CMD_REQUEST_CLIENTDOWN:
    break;
  case CMD_REQUEST_SERVERDOWN:
    break;
  default:
    esyslog("handleAnswer:unknown CMD");
    break;
  }
  answer.clear();
  return bRet;
}


/** actualy just a very simple login functionality */
bool AmarokClient::login (const char *user, const char *pw)
{
  string auth (user);
  auth.append(",");
  auth.append(pw);
  AmarokData data = CMD_REQUEST_LOGIN;
  return command(auth,data);
}

void AmarokClient::sendCmd(const string &answ)
{
  *p_stream << answ << endl;

}

void AmarokClient::sendCmd(const int answ)
{
  *p_stream << answ << endl;

}

void AmarokClient::waitforAnswer(int &cmd)
{

  *p_stream >> cmd;

}

void AmarokClient::waitforAnswer(string &cmd)
{

  *p_stream >> cmd;

}

