/**********************************************************************
 * LeechCraft - modular cross-platform feature rich internet client.
 * Copyright (C) 2006-2013  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 "radiocustomstreams.h"
#include <QStandardItemModel>
#include <QtDebug>
#include <interfaces/media/iradiostation.h>
#include <interfaces/media/audiostructs.h>
#include "radiocustomstation.h"
#include "xmlsettingsmanager.h"
#include "core.h"

Q_DECLARE_METATYPE (QList<QUrl>);

namespace LeechCraft
{
namespace LMP
{
	namespace
	{
		enum CustomRole
		{
			UrlRole = Media::RadioItemRole::MaxRadioRole + 1
		};
	}

	RadioCustomStreams::RadioCustomStreams (QObject *parent)
	: QObject (parent)
	, Root_ (new QStandardItem (tr ("Custom streams")))
	{
		Root_->setIcon (Core::Instance ().GetProxy ()->GetIcon ("favorites"));
		Root_->setData (Media::RadioType::CustomAddableStreams, Media::RadioItemRole::ItemType);
		Root_->setData ("org.LeechCraft.LMP.Custom", Media::RadioItemRole::RadioID);
		Root_->setEditable (false);

		LoadSettings ();
	}

	QList<QStandardItem*> RadioCustomStreams::GetRadioListItems () const
	{
		return { Root_ };
	}

	Media::IRadioStation_ptr RadioCustomStreams::GetRadioStation (QStandardItem *item, const QString&)
	{
		QList<QUrl> urls;
		if (item == Root_)
			urls = GetAllUrls ();
		else
			urls << item->data (CustomRole::UrlRole).toUrl ();
		return std::make_shared<RadioCustomStation> (urls, this);
	}

	void RadioCustomStreams::RefreshItems (const QList<QStandardItem*>&)
	{
	}

	void RadioCustomStreams::Add (const QUrl& url)
	{
		if (GetAllUrls ().contains (url))
			return;

		CreateItem (url);

		SaveSettings ();
	}

	void RadioCustomStreams::Remove (const QModelIndex& index)
	{
		for (auto i = 0; i < Root_->rowCount (); ++i)
			if (Root_->child (i)->index () == index)
			{
				Root_->removeRow (i);
				SaveSettings ();
				break;
			}
	}

	void RadioCustomStreams::CreateItem (const QUrl& url)
	{
		auto item = new QStandardItem (url.toString ());
		item->setEditable (false);

		item->setData (url, CustomRole::UrlRole);
		item->setData (Media::RadioType::SingleTrack, Media::RadioItemRole::ItemType);

		Media::AudioInfo info;
		info.Length_ = 0;
		info.Other_ ["URL"] = url;
		item->setData (QVariant::fromValue (QList<Media::AudioInfo> { info }),
				Media::RadioItemRole::TracksInfos);

		Root_->appendRow (item);
	}

	QList<QUrl> RadioCustomStreams::GetAllUrls () const
	{
		QList<QUrl> result;
		for (auto i = 0; i < Root_->rowCount (); ++i)
			result << Root_->child (i)->data (CustomRole::UrlRole).toUrl ();
		return result;
	}

	void RadioCustomStreams::LoadSettings ()
	{
		const auto& urls = XmlSettingsManager::Instance ()
				.property ("CustomRadioUrls").value<QList<QUrl>> ();
		for (const auto& url : urls)
			CreateItem (url);
	}

	void RadioCustomStreams::SaveSettings () const
	{
		const auto& urlsVar = QVariant::fromValue (GetAllUrls ());
		XmlSettingsManager::Instance ().setProperty ("CustomRadioUrls", urlsVar);
	}
}
}
