#include "gui/guiscenemanager.h"

#include "ui/about_us/aboutus.h"
#include "ui/dialog_form/dialogform.h"
#include "ui/main_menu/mainmenu.h"
#include "ui/main_menu/mainmenubackground.h"

#include "ui/hotseat_recruitment/recruitmentscene.h"
#include "ui/hotseat_intro/playervsplayerintro.h"
#include "ui/hotseat_prebattle/prebattlescene.h"
#include "ui/hotseat_game/hotseatgame.h"

#include <QResource>
#include <QApplication>
#include <QFontDatabase>
#include <QTimer>

#define PROJECT_VERSION "v.0.3.0dev"

GuiSceneManager::GuiSceneManager(QObject *parent) : QObject(parent) {
    window_ = new QMainWindow(nullptr, Qt::Window | Qt::FramelessWindowHint);
    window_->setWindowTitle(QString("Honourished ") + PROJECT_VERSION);
    window_->setWindowState(Qt::WindowFullScreen);
    window_->show();

    current_scene_ = "none";

    QResource::registerResource(QApplication::applicationDirPath() + "/res/data000.gtr");
    QResource::registerResource(QApplication::applicationDirPath() + "/res/data001.gtr");
    QResource::registerResource(QApplication::applicationDirPath() + "/res/data002.gtr");
    QResource::registerResource(QApplication::applicationDirPath() + "/res/data003.gtr");
    QResource::registerResource(QApplication::applicationDirPath() + "/res/data004.gtr");
    QResource::registerResource(QApplication::applicationDirPath() + "/res/data005.gtr");
    QResource::registerResource(QApplication::applicationDirPath() + "/res/data006.gtr");
    QResource::registerResource(QApplication::applicationDirPath() + "/res/data007.gtr");
    QResource::registerResource(QApplication::applicationDirPath() + "/res/data008.gtr");
    QResource::registerResource(QApplication::applicationDirPath() + "/res/data009.gtr");

    QFontDatabase::addApplicationFont(":/assets/common/fonts/pixel.ttf");

    registerScene("main_menu", new MainMenu(window_));
    registerScene("main_menu_background", new MainMenuBackground(window_));
    registerScene("pvp_intro", new PlayerVsPlayerIntro(window_));
    registerScene("recruit_army", new RecruitmentScene(window_));
    registerScene("prebattle", new PreBattleScene(window_));
    registerScene("hotseatgame", new HotSeatGame(window_));
    registerScene("about_us", new AboutUs(window_));
    registerScene("dialog_form", new DialogForm(window_));

    QTimer::singleShot(100, [=] {
        changeScene("main_menu");
    });
}

GuiSceneManager::~GuiSceneManager() {}

bool GuiSceneManager::registerScene(QString scene_name, Scene* scene) {
    if (scene_name == "none")
        return false;

    if (scenes_.count(scene_name) != 0) {
        delete scene;
        return false;
    }

    scenes_[scene_name] = scene;
    return true;
}

bool GuiSceneManager::changeScene(QString scene_name, QString args) {
    if (scene_name == "main_window") {
        return false;
    }

    if (current_scene_ != "none") {
        scenes_[current_scene_]->deinit();
        scenes_[current_scene_]->hide();
        window_->takeCentralWidget();
    }

    scenes_[scene_name]->parseArgs(args);
    scenes_[scene_name]->init();
    window_->setCentralWidget(scenes_[scene_name]);
    scenes_[scene_name]->show();
    current_scene_ = scene_name;
    return true;
}

Scene *GuiSceneManager::getScene(QString scene_name)
{
    return scenes_[scene_name];
}

bool GuiSceneManager::show(QString scene_name)
{
    if (scenes_.count(scene_name) == 0)
        return false;

    scenes_[scene_name]->resize(window_->size());
    scenes_[scene_name]->show();
    return true;
}

bool GuiSceneManager::hide(QString scene_name)
{
    if (scenes_.count(scene_name) == 0)
        return true;

    scenes_[scene_name]->hide();
    return false;
}
