/**********************************************************************
 * 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 "m3u.h"
#include <QFile>
#include <QFileInfo>
#include <QDir>
#include <QUrl>
#include <QtDebug>

namespace LeechCraft
{
namespace LMP
{
namespace M3U
{
	QStringList Read (const QString& path)
	{
		QFile file (path);
		if (!file.open (QIODevice::ReadOnly))
		{
			qWarning () << Q_FUNC_INFO
					<< "unable to open"
					<< path
					<< file.errorString ();
			return QStringList ();
		}

		QStringList result;
		while (!file.atEnd ())
		{
			const auto& line = file.readLine ().trimmed ();
			if (line.startsWith ('#'))
				continue;

			result << QString::fromUtf8 (line.constData ());
		}
		return result;
	}

	void Write (const QString& path, const QStringList& lines)
	{
		QFile file (path);
		if (!file.open (QIODevice::WriteOnly))
		{
			qWarning () << Q_FUNC_INFO
					<< "unable to open"
					<< path
					<< file.errorString ();
			return;
		}

		file.write (lines.join ("\n").toUtf8 ());
	}

	QList<AudioSource> Read2Sources (const QString& path)
	{
		const auto& m3uDir = QFileInfo (path).absoluteDir ();

		QList<AudioSource> result;
		Q_FOREACH (QString src, Read (path))
		{
			QUrl url (src);
#ifdef Q_OS_WIN32
			if (url.scheme ().size () > 1)
#else
			if (!url.scheme ().isEmpty ())
#endif
			{
				result << url;
				continue;
			}

			src.replace ('\\', '/');

			const QFileInfo fi (src);
			if (fi.isRelative ())
				src = m3uDir.absoluteFilePath (src);

			if (fi.suffix () == "m3u" || fi.suffix () == "m3u8")
				result += Read2Sources (src);
			else
				result << src;
		}
		return result;
	}

	void Write (const QString& path, const QList<AudioSource>& sources)
	{
		QStringList strings;
		for (const AudioSource& source : sources)
			strings << source.ToUrl ().toString ();
		Write (path, strings);
	}
}
}
}
