/***************************************************************************
 * copyright            : (C) 2006 Ian Monroe <ian@monroe.nu>              *
 **************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/
#include "amarok.h"
#include "amarokconfig.h"
#include "daapclient.h"
#include "daapreader/authentication/hasher.h"
#include "debug.h"
#include "proxy.h"

#include <tdeapplication.h>

using namespace Daap;

//input url: daap://host:port/databaseId/music.ext
/*
        bundle->setUrl( Amarok::QStringx("http://%1:3689/databases/%2/items/%3.%4?%5").args(
            TQStringList() << m_host
                        << m_databaseId
                        << TQString::number( (*it).asMap()["miid"].asList()[0].asInt() )
                        << (*it).asMap()["asfm"].asList()[0].asString()
                        << m_loginString ) );

*/
Proxy::Proxy(KURL stream, DaapClient* client, const char* name)
    : TQObject(client, name)
    , m_proxy( new Amarok::ProcIO() )
{
    DEBUG_BLOCK
    //find the request id and increment it
    const TQString hostKey = stream.host() + ':' + TQString::number(stream.port());
    const int revisionId = client->incRevision( hostKey );
    const int sessionId = client->getSession( hostKey );
    //compose URL
    KURL realStream = realStreamUrl( stream, sessionId );

    //get hash
    char hash[33] = {0};
    GenerateHash( 3
        , reinterpret_cast<const unsigned char*>((realStream.path() + realStream.query()).ascii())
        , 2
        , reinterpret_cast<unsigned char*>(hash)
        , revisionId );

    // Find free port
    MyServerSocket* socket = new MyServerSocket();
    const int port = socket->port();
    debug() << "Proxy server using port: " << port << endl;
    delete socket;
    m_proxyUrl = KURL( TQString("http://localhost:%1/daap.mp3").arg( port ) );
    //start proxy
    m_proxy->setComm( TDEProcess::Communication( TDEProcess::AllOutput ) );
    *m_proxy << "amarok_proxy.rb";
    *m_proxy << "--daap";
    *m_proxy << TQString::number( port );
    *m_proxy << realStream.url();
    *m_proxy << AmarokConfig::soundSystem();
    *m_proxy << hash;
    *m_proxy << TQString::number( revisionId );
    *m_proxy << Amarok::proxyForUrl( realStream.url() );

    if( !m_proxy->start( KProcIO::NotifyOnExit, true ) ) {
        error() << "Failed to start amarok_proxy.rb" << endl;
        return;
    }

    TQString line;
    while( true ) {
        kapp->processEvents();
        m_proxy->readln( line );
        if( line == "AMAROK_PROXY: startup" ) break;
    }
    debug() << "started amarok_proxy.rb --daap " << TQString::number( port ) << ' ' << realStream.url() << ' ' << AmarokConfig::soundSystem() << ' ' << hash << ' ' << revisionId << endl;
    connect( m_proxy, TQ_SIGNAL( processExited( TDEProcess* ) ), this, TQ_SLOT( playbackStopped() ) );
    connect( m_proxy, TQ_SIGNAL( readReady( KProcIO* ) ), this, TQ_SLOT( readProxy() ) );
}

Proxy::~Proxy()
{
    delete m_proxy;
}

void
Proxy::playbackStopped()
{
    deleteLater();
}

void
Proxy::readProxy()
{
    TQString line;

    while( m_proxy->readln( line ) != -1 )
    {
        debug() << line << endl;
    }
}

KURL Proxy::realStreamUrl( KURL fakeStream, int sessionId )
{
    KURL realStream;
    realStream.setProtocol( "http" );
    realStream.setHost(fakeStream.host());
    realStream.setPort(fakeStream.port());
    realStream.setPath( "/databases" + fakeStream.directory() + "/items/" + fakeStream.fileName() );
    realStream.setQuery( TQString("?session-id=") + TQString::number(sessionId) );
    return realStream;
}

#include "proxy.moc"
