/**********************************************************************
 * LeechCraft - modular cross-platform feature rich internet client.
 * Copyright (C) 2006-2014  Georg Rudoy
 *
 * Boost Software License - Version 1.0 - August 17th, 2003
 *
 * Permission is hereby granted, free of charge, to any person or organization
 * obtaining a copy of the software and accompanying documentation covered by
 * this license (the "Software") to use, reproduce, display, distribute,
 * execute, and transmit the Software, and to prepare derivative works of the
 * Software, and to permit third-parties to whom the Software is furnished to
 * do so, all subject to the following:
 *
 * The copyright notices in the Software and this entire statement, including
 * the above license grant, this restriction and the following disclaimer,
 * must be included in all copies of the Software, in whole or in part, and
 * all derivative works of the Software, unless such copies or derivative
 * works are solely in the form of machine-executable object code generated by
 * a source language processor.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 **********************************************************************/

#include "sbmanager.h"
#include <QFileInfo>
#include <QtDebug>
#include "msnaccount.h"
#include "msnmessage.h"
#include "msnbuddyentry.h"
#include "callbacks.h"
#include "zheetutil.h"

namespace LC
{
namespace Azoth
{
namespace Zheet
{
	SBManager::SBManager (Callbacks *cb, MSNAccount *acc)
	: QObject (acc)
	, Account_ (acc)
	, CB_ (cb)
	{
		connect (CB_,
				SIGNAL (gotSB (MSN::SwitchboardServerConnection*, const MSNBuddyEntry*)),
				this,
				SLOT (handleGotSB (MSN::SwitchboardServerConnection*, const MSNBuddyEntry*)));
		connect (CB_,
				SIGNAL (buddyJoinedSB (MSN::SwitchboardServerConnection*, const MSNBuddyEntry*)),
				this,
				SLOT (handleBuddyJoined (MSN::SwitchboardServerConnection*, const MSNBuddyEntry*)));
		connect (CB_,
				SIGNAL (buddyLeftSB (MSN::SwitchboardServerConnection*,const MSNBuddyEntry*)),
				this,
				SLOT (handleBuddyLeft (MSN::SwitchboardServerConnection*, const MSNBuddyEntry*)));
		connect (CB_,
				SIGNAL (messageDelivered (int)),
				this,
				SLOT (handleMessageDelivered (int)));
	}

	void SBManager::SendMessage (MSNMessage *msg, const MSNBuddyEntry *entry)
	{
		if (Switchboards_.contains (entry))
		{
			const int id = Switchboards_ [entry]->
					sendMessage (ZheetUtil::ToStd (msg->GetBody ()));
			msg->SetID (id);
			PendingDelivery_ [id] = msg;
			return;
		}

		PendingMessages_ [entry] << msg;
		Account_->GetNSConnection ()->requestSwitchboardConnection (entry);
	}

	void SBManager::SendNudge (const QString&, const MSNBuddyEntry *entry)
	{
		if (Switchboards_.contains (entry))
		{
			Switchboards_ [entry]->sendNudge ();
			return;
		}

		PendingNudges_ << entry;
		Account_->GetNSConnection ()->requestSwitchboardConnection (entry);
	}

	void SBManager::SendFile (const QString& file, uint id, const MSNBuddyEntry *entry)
	{
		const QFileInfo info (file);
		MSN::fileTransferInvite ft =
		{
			1,
			id,
			ZheetUtil::ToStd (entry->GetHumanReadableID ()),
			ZheetUtil::ToStd (file),
			ZheetUtil::ToStd (info.fileName ()),
			std::string (),
			static_cast<decltype (ft.filesize)> (info.size ())
		};
		if (Switchboards_.contains (entry))
		{
			Switchboards_ [entry]->sendFile (ft);
			return;
		}

		PendingTransfers_ [entry] << ft;
		Account_->GetNSConnection ()->requestSwitchboardConnection (entry);
	}

	void SBManager::handleGotSB (MSN::SwitchboardServerConnection *conn, const MSNBuddyEntry *entry)
	{
		conn->inviteUser (ZheetUtil::ToStd (entry->GetHumanReadableID ()));
	}

	void SBManager::handleBuddyJoined (MSN::SwitchboardServerConnection *conn, const MSNBuddyEntry *entry)
	{
		Switchboards_ [entry] = conn;

		for (auto msg : PendingMessages_.take (entry))
			SendMessage (msg, entry);

		for (auto ft : PendingTransfers_.take (entry))
			conn->sendFile (ft);

		if (PendingNudges_.remove (entry))
			SendNudge (QString (), entry);
	}

	void SBManager::handleBuddyLeft (MSN::SwitchboardServerConnection*, const MSNBuddyEntry *entry)
	{
		Switchboards_.remove (entry);
	}

	void SBManager::handleMessageDelivered (int id)
	{
		if (!PendingDelivery_.contains (id))
		{
			qWarning () << Q_FUNC_INFO
					<< "unknown message ID"
					<< id
					<< PendingDelivery_;
			return;
		}

		PendingDelivery_.take (id)->SetDelivered ();
	}
}
}
}
