/**********************************************************************
 * 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 "captchamanager.h"
#include <algorithm>
#include <QDialog>
#include <QDialogButtonBox>
#include <QVBoxLayout>
#include <QtDebug>
#include "xmppcaptchamanager.h"
#include "formbuilder.h"
#include "xmppbobmanager.h"

namespace LeechCraft
{
namespace Azoth
{
namespace Xoox
{
	CaptchaManager::CaptchaManager (XMPPCaptchaManager *captchaMgr,
			XMPPBobManager *bobMgr, QObject *parent)
	: QObject (parent)
	, CaptchaManager_ (captchaMgr)
	, BobManager_ (bobMgr)
	{
		connect (captchaMgr,
				SIGNAL (captchaFormReceived (const QString&, const QXmppDataForm&)),
				this,
				SLOT (handleCaptchaReceived (const QString&, const QXmppDataForm&)));
	}

	void CaptchaManager::handleCaptchaReceived (const QString& jid, const QXmppDataForm& dataForm)
	{
		auto builder = new FormBuilder (jid, BobManager_);

		auto dialog = new QDialog ();
		QWidget *widget = builder->CreateForm (dataForm, dialog);
		dialog->setWindowTitle (widget->windowTitle ().isEmpty () ?
				tr ("Enter CAPTCHA") :
				widget->windowTitle ());
		dialog->setLayout (new QVBoxLayout ());
		dialog->layout ()->addWidget (widget);
		QDialogButtonBox *box = new QDialogButtonBox (QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
		dialog->layout ()->addWidget (box);

		connect (box,
				SIGNAL (accepted ()),
				dialog,
				SLOT (accept ()));
		connect (box,
				SIGNAL (rejected ()),
				dialog,
				SLOT (reject ()));

		Pendings_.push_back ({
				jid,
				std::shared_ptr<FormBuilder> (builder),
				dialog
			});

		connect (dialog,
				SIGNAL (finished (int)),
				this,
				SLOT (handleDialogFinished (int)));

		dialog->show ();
	}

	void CaptchaManager::handleDialogFinished (int result)
	{
		auto dialog = qobject_cast<QDialog*> (sender ());

		const auto pos = std::find_if (Pendings_.begin (), Pendings_.end (),
				[dialog] (const PendingCaptcha& pending)
					{ return pending.Dialog_ == dialog; });
		if (pos == Pendings_.end ())
		{
			qWarning () << Q_FUNC_INFO
					<< "sender dialog not found";
			return;
		}

		if (result == QDialog::Accepted)
		{
			const auto& form = pos->FB_->GetForm ();
			CaptchaManager_->SendResponse (pos->JID_, form);
		}

		pos->Dialog_->deleteLater ();
		Pendings_.erase (pos);
	}
}
}
}
