#include <QApplication>
#include <QGraphicsOpacityEffect>
#include <QPropertyAnimation>
#include <QSound>
#include "intro.h"
#include "manager.h"


Manager *g_Manager = 0;


Manager::Manager(QWidget *parent) : QWidget(parent)
{
	g_Manager = this;
	resize(800,480);

	Intro *intro = new Intro(this);
	intro->raise();
	connect(intro, SIGNAL(setStage(int)), this, SLOT(setStage(int)));

	mainmenu = new MainMenu(this);
	connect(mainmenu, SIGNAL(setStage(int)), this, SLOT(setStage(int)));
	mainmenu->hide();

	slideshow = 0;
	photos = 0;
	videos = 0;
	music = 0;
	info = 0;
	settings = 0;
}


Manager::~Manager()
{

}


void Manager::fadeInWidget(QWidget* w)
{
	w->show();
	w->setFocus();
	QGraphicsOpacityEffect* eff = new QGraphicsOpacityEffect(this);
	w->setGraphicsEffect(eff);
	QPropertyAnimation* anim = new QPropertyAnimation(eff, "opacity");
	anim->setDuration(500);
	anim->setStartValue(0);
	anim->setEndValue(1);
	anim->setEasingCurve(QEasingCurve::InBack);
	anim->start(QPropertyAnimation::DeleteWhenStopped);
}


void Manager::fadeOutWidget(QWidget* w)
{
	QGraphicsOpacityEffect* eff = new QGraphicsOpacityEffect(this);
	w->setGraphicsEffect(eff);
	QPropertyAnimation* anim = new QPropertyAnimation(eff, "opacity");
	anim->setDuration(500);
	anim->setStartValue(1);
	anim->setEndValue(0);
	anim->setEasingCurve(QEasingCurve::OutBack);
	anim->start(QPropertyAnimation::DeleteWhenStopped);
	connect(anim, &QPropertyAnimation::finished, [w]() { w->hide(); });
}


void Manager::setStage(int stage)
{
	switch (stage) {
		case -1:
			QSound::play("sounds/Delete.wav");
		case -2:
			if (slideshow)
				fadeOutWidget(slideshow);
			if (photos)
				fadeOutWidget(photos);
			if (music)
				fadeOutWidget(music);
			if (videos)
				fadeOutWidget(videos);
			if (info)
				fadeOutWidget(info);
			if (settings)
				fadeOutWidget(settings);
			mainmenu->show();
			mainmenu->setFocus();
			break;
		case 0:
			if (!slideshow) {
				slideshow = new SlideShow(this);
				connect(slideshow, SIGNAL(setStage(int)), this, SLOT(setStage(int)));
			}
			fadeInWidget(slideshow);
			break;
		case 1:
			if (!photos) {
				photos = new PhotoList(this);
				connect(photos, SIGNAL(setStage(int)), this, SLOT(setStage(int)));
			}
			fadeInWidget(photos);
			break;
		case 2:
			if (!music) {
				music = new MusicList(this);
				connect(music, SIGNAL(setStage(int)), this, SLOT(setStage(int)));
			}
			fadeInWidget(music);
			break;
		case 3:
			if (!videos) {
				videos = new VideoList(this);
				connect(videos, SIGNAL(setStage(int)), this, SLOT(setStage(int)));
			}
			fadeInWidget(videos);
			break;
		case 4:
			if (!info) {
				info = new Information(this);
				connect(info, SIGNAL(setStage(int)), this, SLOT(setStage(int)));
			}
			fadeInWidget(info);
			break;
		case 5:
			if (!settings) {
				settings = new Settings(this);
				connect(settings, SIGNAL(setStage(int)), this, SLOT(setStage(int)));
			}
			fadeInWidget(settings);
			break;
		case 6:
			// Turn Off
			// system("shutdown -t now");
			qApp->quit();
			break;
		default:
			return;
	}
}

