/* StationSearcher.cpp */

/* Copyright (C) 2011-2020 Michael Lugmair (Lucio Carreras)
 *
 * 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 "StationSearcher.h"
#include "FMStreamParser.h"
#include "Utils/WebAccess/AsyncWebAccess.h"

#include <QUrl>

struct StationSearcher::Private
{
	QList<RadioStation> foundStations;
	QString				searchstring;
	int					currentPageIndex;
	StationSearcher::Mode mode;

	Private() :
		currentPageIndex(0),
		mode(StationSearcher::NewSearch)
	{}

	QString url()
	{
		if(mode == StationSearcher::Style)
		{
			return QString("http://fmstream.org/index.php?style=%1")
					.arg(searchstring);
		}

		else if(currentPageIndex == 0)
		{
			return QString("http://fmstream.org/index.php?s=%1&cm=0")
					.arg(searchstring);
		}

		else
		{
			return QString("http://fmstream.org/index.php?s=%1&n=%2")
					.arg(searchstring)
					.arg(currentPageIndex);
		}
	}


	void increase_page()
	{
		currentPageIndex += 200;
	}

	void decrease_page()
	{
		currentPageIndex = std::max(0, currentPageIndex - 200);
	}
};


StationSearcher::StationSearcher(QObject* parent) :
	QObject(parent)
{
	m = Pimpl::make<Private>();
}

StationSearcher::~StationSearcher() {}



void StationSearcher::startCall()
{
	AsyncWebAccess* wa = new AsyncWebAccess(this);

	connect(wa, &AsyncWebAccess::sigFinished, this, &StationSearcher::searchFinished);

	wa->run(m->url());
}

void StationSearcher::searchStyle(const QString& style)
{
	m->mode = StationSearcher::Style;
	m->searchstring = style;

	startCall();
}


void StationSearcher::searchStation(const QString& name)
{
	m->mode = StationSearcher::NewSearch;
	m->currentPageIndex = 0;
	m->searchstring = name;

	startCall();
}

void StationSearcher::searchPrevious()
{
	m->decrease_page();
	m->mode = StationSearcher::Incremental;

	startCall();
}

void StationSearcher::searchNext()
{
	m->increase_page();
	m->mode = StationSearcher::Incremental;

	startCall();
}

bool StationSearcher::canSearchNext() const
{
	return (m->foundStations.size() > 10 && m->mode != StationSearcher::Style);
}

bool StationSearcher::canSearchPrevious() const
{
	return (m->currentPageIndex >= 200 && m->foundStations.size() > 0 && m->mode != StationSearcher::Style);
}

StationSearcher::Mode StationSearcher::mode() const
{
	return m->mode;
}

QList<RadioStation> StationSearcher::foundStations() const
{
	return m->foundStations;
}

void StationSearcher::searchFinished()
{
	auto* wa = static_cast<AsyncWebAccess*>(sender());

	FMStreamParser parser(wa->data());
	m->foundStations = parser.stations();

	wa->deleteLater();

	emit sigStationsFound();
}
