/**********************************************************************
 * 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 "entriesdelegate.h"
#include <QComboBox>
#include <QStringListModel>
#include <util/tags/tagslineedit.h>
#include <util/tags/tagscompleter.h>
#include <interfaces/core/itagsmanager.h>
#include "entriesmodel.h"
#include "core.h"
#include "accountsmanager.h"
#include "currenciesmanager.h"

namespace LeechCraft
{
namespace Poleemery
{
	QWidget* EntriesDelegate::createEditor (QWidget *parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
	{
		switch (index.column ())
		{
		case EntriesModel::Columns::Account:
		case EntriesModel::Columns::EntryCurrency:
			return new QComboBox (parent);
		case EntriesModel::Columns::Categories:
		{
			auto result = new Util::TagsLineEdit (parent);
			auto completer = new Util::TagsCompleter (result);

			const auto& cats = Core::Instance ().GetOpsManager ()->GetKnownCategories ().toList ();
			completer->OverrideModel (new QStringListModel (cats, completer));

			result->AddSelector ();

			return result;
		}
		default:
			return QStyledItemDelegate::createEditor (parent, option, index);
		}
	}

	void EntriesDelegate::setEditorData (QWidget *editor, const QModelIndex& index) const
	{
		switch (index.column ())
		{
		case EntriesModel::Columns::Account:
		{
			auto box = qobject_cast<QComboBox*> (editor);
			const auto currentAccId = index.data (Qt::EditRole).toInt ();
			int toFocus = -1;
			for (const auto& acc : Core::Instance ().GetAccsManager ()->GetAccounts ())
			{
				if (acc.ID_ == currentAccId)
					toFocus = box->count ();
				box->addItem (acc.Name_, static_cast<int> (acc.ID_));
			}
			box->setCurrentIndex (toFocus);
			break;
		}
		case EntriesModel::Columns::EntryCurrency:
		{
			auto box = qobject_cast<QComboBox*> (editor);
			const auto currentCurrency = index.data (Qt::EditRole).toString ();

			const auto& enabledCurrencies = Core::Instance ()
					.GetCurrenciesManager ()->GetEnabledCurrencies ();
			box->addItems (enabledCurrencies);

			const auto idx = enabledCurrencies.indexOf (currentCurrency);
			box->setCurrentIndex (idx);

			break;
		}
		case EntriesModel::Columns::Categories:
		{
			auto edit = qobject_cast<Util::TagsLineEdit*> (editor);
			edit->setTags (index.data (Qt::EditRole).toStringList ());
			break;
		}
		default:
			QStyledItemDelegate::setEditorData (editor, index);
			break;
		}
	}

	void EntriesDelegate::setModelData (QWidget *editor, QAbstractItemModel *model, const QModelIndex& index) const
	{
		switch (index.column ())
		{
		case EntriesModel::Columns::Account:
		{
			auto box = qobject_cast<QComboBox*> (editor);
			model->setData (index, box->itemData (box->currentIndex ()));
			break;
		}
		case EntriesModel::Columns::EntryCurrency:
		{
			auto box = qobject_cast<QComboBox*> (editor);
			model->setData (index, box->currentText ());
			break;
		}
		case EntriesModel::Columns::Categories:
		{
			auto box = qobject_cast<Util::TagsLineEdit*> (editor);
			auto itm = Core::Instance ().GetCoreProxy ()->GetTagsManager ();
			model->setData (index, itm->Split (box->text ()));
			break;
		}
		default:
			QStyledItemDelegate::setModelData (editor, model, index);
			break;
		}
	}
}
}
