/**********************************************************************
 * 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 "composemessagetabfactory.h"
#include <QMessageBox>
#include <util/sll/visitor.h>
#include "composemessagetab.h"
#include "core.h"
#include "structures.h"
#include "messageinfo.h"
#include "messagebodies.h"

namespace LeechCraft
{
namespace Snails
{
	ComposeMessageTabFactory::ComposeMessageTabFactory (const AccountsManager *accsMgr,
			const MsgTemplatesManager *templatesMgr, QObject *parent)
	: QObject { parent }
	, AccsMgr_ { accsMgr }
	, TemplatesMgr_ { templatesMgr }
	{
	}

	ComposeMessageTab* ComposeMessageTabFactory::MakeTab () const
	{
		return new ComposeMessageTab { AccsMgr_, TemplatesMgr_ };
	}

	void ComposeMessageTabFactory::PrepareComposeTab (const Account_ptr& account)
	{
		const auto cmt = MakeTab ();

		cmt->SelectAccount (account);
		cmt->PrepareLinked (MsgType::New, {}, {});

		emit gotTab (cmt->GetTabClassInfo ().VisibleName_, cmt);
	}

	void ComposeMessageTabFactory::PrepareLinkedTab (MsgType type,
			const Account_ptr& account, const MessageInfo& msgInfo,
			const boost::variant<MessageBodies, Account::FetchWholeMessageResult_t>& bodiesVar)
	{
		const auto cmt = MakeTab ();

		cmt->SelectAccount (account);

		Util::Visit (bodiesVar,
				[cmt, type, msgInfo] (const MessageBodies& bodies) { cmt->PrepareLinked (type, msgInfo, bodies); },
				[cmt, type, msgInfo] (const Account::FetchWholeMessageResult_t& future)
				{
					cmt->setEnabled (false);

					Util::Sequence (cmt, future) >>
							[cmt, type, msgInfo] (const auto& result)
							{
								Util::Visit (result.AsVariant (),
										[cmt, type, msgInfo] (const MessageBodies& bodies)
										{
											cmt->setEnabled (true);
											cmt->PrepareLinked (type, msgInfo, bodies);
										},
										[cmt] (const auto& err)
										{
											QMessageBox::critical (cmt,
													"LeechCraft",
													tr ("Unable to fetch message: %1.")
														.arg (Util::Visit (err,
																[] (auto err) { return err.what (); })));

											cmt->Remove ();
										});
							};
				});

		emit gotTab (cmt->GetTabClassInfo ().VisibleName_, cmt);
	}
}
}
