/* PodcastTest.cpp
 *
 * Copyright (C) 2011-2024 Michael Lugmair
 *
 * This file is part of sayonara player
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "test/Common/SayonaraTest.h"

#include "Database/Connector.h"
#include "Database/Podcasts.h"
#include "Utils/Utils.h"
#include "Utils/FileUtils.h"
#include "Utils/Streams/Station.h"
#include "Utils/Algorithm.h"

#include <QMap>

class PodcastTest :
	public Test::Base
{
	Q_OBJECT

public:
	PodcastTest() :
		Test::Base("PodcastTest")
	{}

	~PodcastTest() override = default;


private:
	DB::Podcasts* pod()
	{
		auto* db = DB::Connector::instance();
		return db->podcastConnector();
	}

	QList<Podcast> all_podcasts()
	{
		QList<Podcast> podcasts;
		pod()->getAllPodcasts(podcasts);
		return podcasts;
	}

	QList<QString> all_podcast_names()
	{
		QList<Podcast> podcasts = all_podcasts();

		QList<QString> ret;
		Util::Algorithm::transform(podcasts, ret, [](const Podcast& p){
			return p.name();
		});

		std::sort(ret.begin(), ret.end(), [=](const QString& s1, const QString& s2){
			return ((s1.size() < s2.size()) || (s1.size() == s2.size() && (s1 < s2)));
		});

		return ret;
	}

private slots:
	void test_insert_and_delete();
	void test_update();
};

void PodcastTest::test_insert_and_delete()
{
	QList<Podcast> podcasts = all_podcasts();
	QVERIFY(podcasts.size() == 0);

	for(int i=0; i<15; i++)
	{
		QString name = QString("PodcastName%1").arg(i);
		QString url = QString("http://podcast_url/%1").arg(i);

		pod()->addPodcast(Podcast(name, url, false));
	}

	podcasts = all_podcasts();
	QVERIFY(podcasts.count() == 15);

	bool success = pod()->addPodcast(Podcast("PodcastName0", QString("http://newinvalid.url"), false));
	podcasts = all_podcasts();
	QVERIFY(podcasts.count() == 15);

	QVERIFY(success == false);

	for(int i=0; i<podcasts.size(); i++)
	{
		QString name = podcasts[i].name();
		QString url = podcasts[i].url();

		QString exp_name = QString("PodcastName%1").arg(i);
		QString exp_url = QString("http://podcast_url/%1").arg(i);
		QVERIFY(name == exp_name);
		QVERIFY(url == exp_url);
	}

	int count = podcasts.count();
	for(auto it=podcasts.begin(); it != podcasts.end(); it++)
	{
		pod()->deletePodcast(it->name());

		count --;

		auto new_podcasts = all_podcasts();
		QVERIFY(new_podcasts.count() == count);
	}
}

void PodcastTest::test_update()
{
	QList<Podcast> podcasts = all_podcasts();
	QVERIFY(podcasts.size() == 0);

	for(int i=0; i<15; i++)
	{
		QString name = QString("PodcastName%1").arg(i);
		QString url = QString("http://podcast_url/%1").arg(i);

		pod()->addPodcast(Podcast(name, url, false));
	}

	podcasts = all_podcasts();
	QVERIFY(podcasts.count() == 15);

	for(int i=0; i< podcasts.size(); i++)
	{
		Podcast podcast = podcasts[i];
		podcast.setUrl( QString("NeueUrl%1").arg(i) );
		bool b = pod()->updatePodcast(podcast.name(), podcast);
		QVERIFY(b == true);
	}

	{
		Podcast p("asdfasd", "http://newinvalid.url", false);
		bool success = pod()->updatePodcast("asdfkjweroinwe", p);
		QVERIFY(success == false);
	}

	podcasts = all_podcasts();
	for(int i=0; i<podcasts.size(); i++)
	{
		QString url = podcasts[i].url();
		QString name = podcasts[i].name();

		QString exp_name = QString("PodcastName%1").arg(i);
		QString exp_url = QString("NeueUrl%1").arg(i);

		QVERIFY(name == exp_name);
		QVERIFY(url == exp_url);

		pod()->deletePodcast(name);
	}

	podcasts = all_podcasts();
	QVERIFY(podcasts.size() == 0);
}

QTEST_GUILESS_MAIN(PodcastTest)

#include "PodcastTest.moc"

