/***************************************************************************
 *   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 <string>
#include "amarokCommander.h"
#include "amarokData.h"

#define MY_INT_SIZE 10

namespace
{
const std::string PLAYER_INTERFACE_NAME = "player ";
const std::string COLLECTION_INTERFACE_NAME = "collection ";
const std::string PLAYLIST_INTERFACE_NAME = "playlist ";
const std::string PLAYLISTBROWSER_INTERFACE_NAME = "playlistbrowser ";
}



AmarokCommander::AmarokCommander()
        : AmarokClient()
{}


AmarokCommander::~AmarokCommander()
{}


///////////////////////////////////////////////////////////////////////////////////////////////////////////
////// REQUEST_SET
///////////////////////////////////////////////////////////////////////////////////////////////////////////

void AmarokCommander::client_shutdown()
{
    AmarokData data = CMD_REQUEST_CLIENTDOWN;
    command("",data);
}

void AmarokCommander::server_shutdown()
{
    AmarokData data = CMD_REQUEST_SERVERDOWN;
    command("",data);

}

void AmarokCommander::playPause()
{
    command (PLAYER_INTERFACE_NAME + "playPause");
}

void AmarokCommander::playByIndex(unsigned int i)
{
    char text[MY_INT_SIZE];
    ::itoString(text,MY_INT_SIZE,i);
    command (PLAYLIST_INTERFACE_NAME + "playByIndex " + text);
}

void AmarokCommander::removeByIndex(unsigned int i)
{
    char text[MY_INT_SIZE];
    ::itoString(text,MY_INT_SIZE,i);
    command (PLAYLIST_INTERFACE_NAME + "removeByIndex " + text);
}

void AmarokCommander::seekRelative(int seconds)
{
    char text[MY_INT_SIZE];
    ::itoString(text,MY_INT_SIZE,seconds);
    command (PLAYER_INTERFACE_NAME + "seekRelative " + text);
}

void AmarokCommander::pause()
{
    command(PLAYER_INTERFACE_NAME + "pause");
}

void AmarokCommander::next()
{
    command(PLAYER_INTERFACE_NAME + "next");
}

void AmarokCommander::prev()
{
    command(PLAYER_INTERFACE_NAME + "prev");
}

void  AmarokCommander::stop()
{
    command(PLAYER_INTERFACE_NAME + "stop");
}

void AmarokCommander::clearPlaylist()
{
    command (PLAYLIST_INTERFACE_NAME + "clearPlaylist");
}

void AmarokCommander::shufflePlaylist()
{
    command (PLAYLIST_INTERFACE_NAME + "shufflePlaylist");
}

void AmarokCommander::mute()
{
    command(PLAYER_INTERFACE_NAME + "mute");
}

void AmarokCommander::volumeUp()
{
    command(PLAYER_INTERFACE_NAME + "volumeUp");
}

void AmarokCommander::volumeDown()
{
    command(PLAYER_INTERFACE_NAME + "volumeDown");
}

void AmarokCommander::showOsd()
{
    command(PLAYER_INTERFACE_NAME + "showOSD");
}

void AmarokCommander::loadPlaylist(const char* name)
{
    std::string tmp=PLAYLISTBROWSER_INTERFACE_NAME; // instead of + operator
    tmp.append("loadPlaylist \"");
    tmp.append(name);
    tmp.append("\"");
    command (tmp);
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////
////// REQUEST_STRING
///////////////////////////////////////////////////////////////////////////////////////////////////////////

bool AmarokCommander::getNowPlaying(AmarokData &data)
{
    data.setType(CMD_REQUEST_GET);
    return command(PLAYER_INTERFACE_NAME + "nowPlaying",data);
}

int AmarokCommander::getScore()
{
    AmarokData data = CMD_REQUEST_GET;

    if (command(PLAYER_INTERFACE_NAME + "score",data))
    {
        return data.getInteger();
    }
    return -1;
}

int AmarokCommander::getTotalTrackCount()
{
    AmarokData data = CMD_REQUEST_GET;
    if (command(PLAYLIST_INTERFACE_NAME + "getTotalTrackCount",data))
    {
        return data.getInteger();
    }
    return -1;
}

int AmarokCommander::getActiveIndex()
{
    AmarokData data=CMD_REQUEST_GET;
    if (command(PLAYLIST_INTERFACE_NAME + "getActiveIndex",data))
    {
        return data.getInteger();
    }
    return -1;
}

int AmarokCommander::getTrackCurrentTime()
{
    AmarokData data=CMD_REQUEST_GET;
    if (command(PLAYER_INTERFACE_NAME + "trackCurrentTime",data))
    {
        return data.getInteger();
    }
    return -1;
}

int AmarokCommander::getTrackPlayCounter()
{
    AmarokData data=CMD_REQUEST_GET;
    if (command(PLAYER_INTERFACE_NAME + "trackPlayCounter",data))
    {
        return data.getInteger();
    }
    return -1;
}

int AmarokCommander::getPlayStatus()
{
    AmarokData data=CMD_REQUEST_GET;
    if (command(PLAYER_INTERFACE_NAME + "status",data))
    {
        return data.getInteger();
    }
    return -1;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////
////// REQUEST_LIST
///////////////////////////////////////////////////////////////////////////////////////////////////////////

bool AmarokCommander::getCurrentPlaylist(AmarokPlaylist &list)
{
    return command(PLAYLIST_INTERFACE_NAME + "saveCurrentPlaylist",list);
}


bool AmarokCommander::queryLists(AmarokQueryResult &result)
{
    return command("EMPTY",result);

}


void AmarokCommander::addArtist(const char* name)
{
    AmarokData data = CMD_REQUEST_ADDARTIST;
    command(name,data);
}
void AmarokCommander::addAlbum(const char* name)
{
    AmarokData data = CMD_REQUEST_ADDALBUM;
    command(name,data);
}
void AmarokCommander::addGenre(const char* name)
{
    AmarokData data = CMD_REQUEST_ADDGENRE;
    command(name,data);
}
void AmarokCommander::addTitle(const char* name)
{
    AmarokData data = CMD_REQUEST_ADDTITLE;
    command(name,data);
}


///////////////////////////////////////////////////////////////////////////////////////////////////////////
////// global itoStr
///////////////////////////////////////////////////////////////////////////////////////////////////////////

void itoString(char * destStr, int count, int i)
{
    snprintf(destStr,count,"%d",i);
}
