#include "gui/pages/now_playing.h"
#include "core/anime_db.h"
#include "core/strings.h"
#include "gui/widgets/anime_info.h"
#include "gui/widgets/poster.h"
#include "gui/widgets/text.h"
#include "services/services.h"

#include <QHBoxLayout>
#include <QLabel>
#include <QStackedWidget>
#include <QVBoxLayout>
#include <QWidget>

#include "anitomy/anitomy.h"

namespace NowPlayingPages {

Default::Default(QWidget *parent) : QWidget(parent)
{
	QVBoxLayout *layout = new QVBoxLayout(this);
	layout->setContentsMargins(0, 0, 0, 0);

	title_.SetText(Strings::Translate("Now Playing"));
	layout->addWidget(&title_);

	layout->addStretch();
}

Playing::Playing(QWidget *parent) : QWidget(parent)
{
	QHBoxLayout *layout = new QHBoxLayout(this);

	_main.setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);

	QVBoxLayout *main_layout = new QVBoxLayout(&_main);
	main_layout->setContentsMargins(0, 0, 0, 0);

	main_layout->addWidget(&_title);

	_info.setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
	_info.layout()->setContentsMargins(0, 0, 0, 0);
	main_layout->addWidget(&_info);

	/* "sidebar", includes... just the anime image :) */
	QVBoxLayout *sidebar_layout = new QVBoxLayout(&_sidebar);
	sidebar_layout->setContentsMargins(0, 0, 0, 0);

	sidebar_layout->addWidget(&_poster);

	sidebar_layout->addStretch();

	layout->addWidget(&_sidebar);
	layout->addWidget(&_main);
	layout->setSpacing(10);
	layout->setContentsMargins(0, 0, 0, 0);
}

int Playing::GetPlayingAnime()
{
	return _id;
}

int Playing::GetPlayingEpisode()
{
	return _episode;
}

void Playing::SetPlayingAnime(const Anime::Anime &anime, const anitomy::Elements &info)
{
	if (_id == anime.GetId() &&
	    _episode == Strings::ToInt(Strings::ToUtf8String(info.get(anitomy::kElementEpisodeNumber))))
		return;
	_id = anime.GetId();
	_episode = Strings::ToInt(Strings::ToUtf8String(info.get(anitomy::kElementEpisodeNumber)));
	_title.SetText(anime.GetUserPreferredTitle());
	_info.SetAnime(anime);
	_poster.SetAnime(anime);

	updateGeometry();
}

} // namespace NowPlayingPages

NowPlayingPage::NowPlayingPage(QWidget *parent) : QFrame(parent)
{
	setBackgroundRole(QPalette::Base);
	QVBoxLayout *layout = new QVBoxLayout(this);

	setFrameShape(QFrame::Box);
	setFrameShadow(QFrame::Sunken);
	setAutoFillBackground(true);

	stack_.addWidget(&default_);
	stack_.addWidget(&playing_);
	layout->addWidget(&stack_);
	timer_.setSingleShot(true);

	QObject::connect(&timer_, &QTimer::timeout, this, &NowPlayingPage::TimerDone);

	SetDefault();
}

void NowPlayingPage::SetDefault()
{
	/* stop any timer */
	timer_.stop();

	stack_.setCurrentIndex(static_cast<int>(Subpages::Default));
}

int NowPlayingPage::GetPlayingId()
{
	return playing_.GetPlayingAnime();
}

void NowPlayingPage::SetPlaying(const Anime::Anime &anime, const anitomy::Elements &info)
{
	/* ok */
	timer_.stop();

	playing_.SetPlayingAnime(anime, info);
	stack_.setCurrentIndex(static_cast<int>(Subpages::Playing));
	updateGeometry();

	/* now, start the timer
	 * FIXME hardcoding the interval length is a hack at best
	 * (12 * 60 * 1000) == 720000 msec, which is 12 minutes. */
	timer_.setInterval(12 * 60 * 1000);
	timer_.start();
}

void NowPlayingPage::TimerDone(void)
{
	Anime::Anime &anime = Anime::db.items[playing_.GetPlayingAnime()];

	if (!anime.IsInUserList())
		anime.AddToUserList();

	const int progress = anime.GetUserProgress();
	const int playing_ep = playing_.GetPlayingEpisode();

	if (progress > playing_ep) {
		anime.SetUserProgress(playing_ep);
		Services::UpdateAnimeEntry(anime.GetId());
	}
}
