/**********************************************************************
 * 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 "customsitesmanager.h"
#include "xmlsettingsmanager.h"
#include <QStandardItemModel>
#include <QtDebug>
#include <xmlsettingsdialog/datasourceroles.h>

namespace LeechCraft
{
namespace Poshuku
{
namespace SpeedDial
{
	CustomSitesManager::CustomSitesManager ()
	: Model_ { new QStandardItemModel { this } }
	{
		Model_->setHorizontalHeaderLabels ({ tr ("Site name"), "URL" });
		Model_->horizontalHeaderItem (0)->setData (DataSources::DataFieldType::String,
				DataSources::DataSourceRole::FieldType);
		Model_->horizontalHeaderItem (1)->setData (DataSources::DataFieldType::Url,
				DataSources::DataSourceRole::FieldType);

		LoadSettings ();
	}

	QAbstractItemModel* CustomSitesManager::GetModel () const
	{
		return Model_;
	}

	TopList_t CustomSitesManager::GetTopList () const
	{
		TopList_t result;
		for (const auto& addr : GetAddresses ())
			result.append ({ addr.second, addr.first });
		return result;
	}

	AddrList_t CustomSitesManager::GetAddresses () const
	{
		AddrList_t result;

		for (int i = 0; i < Model_->rowCount (); ++i)
		{
			const QUrl url { Model_->item (i, 1)->text () };
			const auto& name = Model_->item (i, 0)->text ();

			result.append ({ name, url });
		}

		return result;
	}

	void CustomSitesManager::LoadSettings ()
	{
		const auto& addrs = XmlSettingsManager::Instance ()
				.property ("Addresses").value<AddrList_t> ();

		for (const auto& addr : addrs)
			Add (addr);
	}

	void CustomSitesManager::SaveSettings ()
	{
		const auto& variant = QVariant::fromValue (GetAddresses ());
		XmlSettingsManager::Instance ().setProperty ("Addresses", variant);
	}

	void CustomSitesManager::Add (const Addr_t& addr)
	{
		QList<QStandardItem*> row
		{
			new QStandardItem (addr.first),
			new QStandardItem (addr.second.toString ())
		};

		for (auto item : row)
			item->setEditable (false);

		Model_->appendRow (row);
	}

	void CustomSitesManager::addRequested (const QString&, const QVariantList& datas)
	{
		Add ({ datas.value (0).toString (), QUrl { datas.value (1).toString () } });
		SaveSettings ();
	}

	void CustomSitesManager::modifyRequested (const QString&, int row, const QVariantList& datas)
	{
		Model_->item (row, 0)->setText (datas.value (0).toString ());
		Model_->item (row, 1)->setText (datas.value (1).toString ());

		SaveSettings ();
	}

	void CustomSitesManager::removeRequested (const QString&, const QModelIndexList& list)
	{
		for (const auto& item : list)
			Model_->removeRow (item.row ());

		SaveSettings ();
	}
}
}
}
