#include "window.h"
#include "config.h"
#include "anime.h"
#if APPLE
#include "sys/osx/dark_theme.h"
#elif WIN32
#include "sys/win32/dark_theme.h"
#endif

Session session = {
	.config = Config()
};

/* note that this code was originally created for use in 
   wxWidgets, but I thought the API was a little meh, so
   I switched to Qt. */

MainWindow::MainWindow(QWidget* parent) :
           QMainWindow(parent) {
	/* Menu Bar */
	QAction* action;
	QMenuBar* menubar = new QMenuBar(parent);
	QMenu* menu = menubar->addMenu("&File");
	QMenu* submenu = menu->addMenu("&Library folders");
	action = new QAction("&Add new folder...");
	submenu->addAction(action);
	action = new QAction("&Scan available episodes");
	menu->addAction(action);

	menu->addSeparator();

	action = menu->addAction("Play &next episode");
	action = menu->addAction("Play &random episode");
	menu->addSeparator();
	action = menu->addAction("E&xit", qApp, &QApplication::quit);

	menu = menubar->addMenu("&Services");
	action = new QAction("Synchronize &list");

	menu->addSeparator();

	submenu = menu->addMenu("&AniList");
	action = menu->addAction("Go to my &profile");
	action = menu->addAction("Go to my &stats");

	submenu = menu->addMenu("&Kitsu");
	action = submenu->addAction("Go to my &feed");
	action = submenu->addAction("Go to my &library");
	action = submenu->addAction("Go to my &profile");

	submenu = menu->addMenu("&MyAnimeList");
	action = submenu->addAction("Go to my p&anel");
	action = submenu->addAction("Go to my &profile");
	action = submenu->addAction("Go to my &history");

	menu = menubar->addMenu("&Tools");
	submenu = menu->addMenu("&Export anime list");
	action = submenu->addAction("Export as &Markdown...");
	action = submenu->addAction("Export as MyAnimeList &XML...");

	menu->addSeparator();

	action = menu->addAction("Enable anime &recognition");
	action->setCheckable(true);
	action = menu->addAction("Enable auto &sharing");
	action->setCheckable(true);
	action = menu->addAction("Enable &auto synchronization");
	action->setCheckable(true);

	menu->addSeparator();

	action = menu->addAction("&Settings");

	setMenuBar(menubar);
	
	/* Side toolbar */
	QToolBar* toolbar = new QToolBar(parent);
	QActionGroup* tb_action_group = new QActionGroup(toolbar);

	action = toolbar->addAction("Now Playing");
	action->setActionGroup(tb_action_group);
	action->setCheckable(true);

	toolbar->addSeparator();

	action = toolbar->addAction("Anime List", [this]() {
		setCentralWidget(anime_list_page);
	});
	action->setActionGroup(tb_action_group);
	action->setCheckable(true);
	action->setChecked(true);
	anime_list_page = new AnimeListPage(parent);
	SetActivePage(anime_list_page);
	action = toolbar->addAction("History");
	action->setActionGroup(tb_action_group);
	action->setCheckable(true);
	action = toolbar->addAction("Statistics");
	action->setActionGroup(tb_action_group);
	action->setCheckable(true);

	toolbar->addSeparator();

	action = toolbar->addAction("Search");
	action->setActionGroup(tb_action_group);
	action->setCheckable(true);
	action = toolbar->addAction("Seasons");
	action->setActionGroup(tb_action_group);
	action->setCheckable(true);
	action = toolbar->addAction("Torrents");
	action->setActionGroup(tb_action_group);
	action->setCheckable(true);

	toolbar->setMovable(false);
	toolbar->setFloatable(false);

	addToolBar(Qt::LeftToolBarArea, toolbar);
	
	ThemeChanged();
}

void MainWindow::SetStyleSheet(enum Themes theme) {
	switch (theme) {
		case DARK: {
			QFile f(":qdarkstyle/dark/darkstyle.qss");
			if (!f.exists())
				return; // fail
			f.open(QFile::ReadOnly | QFile::Text);
			QTextStream ts(&f);
			setStyleSheet(ts.readAll());
			break;
		}
		default:
			setStyleSheet("");
			break;
	}
}

void MainWindow::ThemeChanged() {
	switch (session.config.theme) {
		case LIGHT: {
#if APPLE
			if (osx::DarkThemeAvailable())
				osx::SetToLightTheme();
			else
				SetStyleSheet(LIGHT);
#else
			SetStyleSheet(LIGHT);
#endif
			break;
		}
		case DARK: {
#if APPLE
			if (osx::DarkThemeAvailable())
				osx::SetToDarkTheme();
			else
				SetStyleSheet(DARK);
#else
			SetStyleSheet(DARK);
#endif
			break;
		}
		case OS: {
#if APPLE
			if (osx::DarkThemeAvailable())
				osx::SetToAutoTheme();
			else
				SetStyleSheet(LIGHT);
#elif defined(WIN32)
			if (win32::DarkThemeAvailable()) {
				if (win32::IsInDarkTheme()) {
					SetStyleSheet(DARK);
				} else {
					SetStyleSheet(LIGHT);
				}
			}
#else
			SetStyleSheet(LIGHT);
#endif
			break;
		}
	}
}

void MainWindow::SetActivePage(QWidget* page) {
	this->setCentralWidget(page);
}

void MainWindow::closeEvent(QCloseEvent* event) {
	session.config.Save();
	event->accept();
}

int main(int argc, char** argv) {
	QApplication app(argc, argv);

	session.config.Load();

	MainWindow window;

	window.resize(941, 750);
	window.setWindowTitle("Weeaboo");
	window.show();

	return app.exec();
}
