/**********************************************************************
 * 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 "bookmarkswidget.h"
#include <QToolBar>
#include <QStandardItemModel>
#include "documenttab.h"
#include "bookmark.h"
#include "core.h"
#include "bookmarksmanager.h"

namespace LeechCraft
{
namespace Monocle
{
	namespace
	{
		enum Roles
		{
			RBookmark = Qt::UserRole + 1
		};
	}

	BookmarksWidget::BookmarksWidget (DocumentTab *tab, QWidget *parent)
	: QWidget (parent)
	, Tab_ (tab)
	, Toolbar_ (new QToolBar ())
	, BMModel_ (new QStandardItemModel (this))
	{
		Ui_.setupUi (this);
		Ui_.BookmarksView_->setModel (BMModel_);
		Ui_.MainLayout_->insertWidget (0, Toolbar_);

		HandleDoc ({});

		auto addBm = Toolbar_->addAction (tr ("Add bookmark"),
				this, SLOT (handleAddBookmark ()));
		addBm->setProperty ("ActionIcon", "bookmark-new");

		auto removeBookmark = Toolbar_->addAction (tr ("Remove bookmark"),
				this, SLOT (handleRemoveBookmark ()));
		removeBookmark->setProperty ("ActionIcon", "list-remove");
		Ui_.BookmarksView_->addAction (removeBookmark);
	}

	void BookmarksWidget::HandleDoc (IDocument_ptr doc)
	{
		setEnabled (doc != nullptr);
		Doc_ = doc;

		ReloadBookmarks ();
	}

	void BookmarksWidget::ReloadBookmarks ()
	{
		BMModel_->clear ();
		BMModel_->setHorizontalHeaderLabels ({ tr ("Name") });

		if (!Doc_)
			return;

		for (const auto& bm : Core::Instance ().GetBookmarksManager ()->GetBookmarks (Doc_))
			AddBMToTree (bm);
	}

	void BookmarksWidget::AddBMToTree (const Bookmark& bm)
	{
		auto item = new QStandardItem (bm.GetName ());
		item->setEditable (false);
		item->setData (QVariant::fromValue<Bookmark> (bm), Roles::RBookmark);
		BMModel_->appendRow (item);
	}

	void BookmarksWidget::handleAddBookmark ()
	{
		if (!Doc_)
			return;

		const auto page = Tab_->GetCurrentPage ();
		const auto& center = Tab_->GetCurrentCenter ();

		Bookmark bm (tr ("Page %1").arg (page + 1), page, center);
		Core::Instance ().GetBookmarksManager ()->AddBookmark (Doc_, bm);
		AddBMToTree (bm);
	}

	void BookmarksWidget::handleRemoveBookmark ()
	{
		auto idx = Ui_.BookmarksView_->currentIndex ();
		if (!idx.isValid ())
			return;

		idx = idx.sibling (idx.row (), 0);
		const auto& bm = idx.data (Roles::RBookmark).value<Bookmark> ();
		Core::Instance ().GetBookmarksManager ()->RemoveBookmark (Doc_, bm);

		ReloadBookmarks ();
	}

	void BookmarksWidget::on_BookmarksView__activated (const QModelIndex& idx)
	{
		const auto& bm = idx.sibling (idx.row (), 0).data (Roles::RBookmark).value<Bookmark> ();
		Tab_->CenterOn (bm.GetPosition ());
	}
}
}
