/**********************************************************************
 * 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 "util.h"
#include <QStandardItem>
#include <util/sll/urloperator.h>
#include <util/svcauth/vkauthmanager.h>
#include <interfaces/core/iiconthememanager.h>
#include <interfaces/media/iradiostationprovider.h>
#include <interfaces/media/audiostructs.h>

namespace LeechCraft
{
namespace TouchStreams
{
	bool CheckAuthentication (QStandardItem *rootItem,
			Util::SvcAuth::VkAuthManager *authMgr, const ICoreProxy_ptr& proxy)
	{
		if (authMgr->HadAuthentication ())
			return true;

		auto item = new QStandardItem (QObject::tr ("Authenticate"));
		item->setEditable (false);
		item->setIcon (proxy->GetIconThemeManager ()->GetIcon ("emblem-locked"));
		item->setData ("auth", Media::RadioItemRole::RadioID);
		item->setData (Media::RadioType::RadioAction, Media::RadioItemRole::ItemType);
		rootItem->appendRow (item);
		return false;
	}

	boost::optional<Media::AudioInfo> TrackMap2Info (const QVariantMap& map)
	{
		const auto& url = QUrl::fromEncoded (map ["url"].toString ().toUtf8 ());
		if (!url.isValid ())
			return {};

		Media::AudioInfo info;
		info.Title_ = map ["title"].toString ();
		info.Artist_ = map ["artist"].toString ();
		info.Length_ = map ["duration"].toInt ();
		info.Other_ ["URL"] = url;
		return info;
	}

	QString TrackMap2RadioId (const QVariantMap& map)
	{
		QUrl radioID { "vk://track" };
		Util::UrlOperator { radioID }
				("audio_id", map.value ("id").toString ())
				("owner_id", map.value ("owner_id").toString ());

#if QT_VERSION >= 0x050000
		return radioID.toString (QUrl::FullyEncoded);
#else
		return radioID.toString ();
#endif
	}
}
}
