#include "gui/GuiSceneManager.h"
#include "gui/GUI.h"
#include "gui/MainMenu.h"
#include "gui/PlayerVsPlayerIntro.h"
#include "gui/PreBattleScene.h"
#include "hotseatgame\gui\HotSeatGame.h"

#include <QResource>
#include <QApplication>

GuiSceneManager::GuiSceneManager(QObject *parent) : QObject(parent) {
    gui_ = new GUI();
    gui_->show();
    current_scene_ = "none";

    QResource::registerResource(QApplication::applicationDirPath() + "/res/gui_data.gtr");
    QResource::registerResource(QApplication::applicationDirPath() + "/res/unit_data.gtr");
    QResource::registerResource(QApplication::applicationDirPath() + "/res/spell_data.gtr");
    QResource::registerResource(QApplication::applicationDirPath() + "/res/effect_data.gtr");

    registerScene("main_menu", new MainMenu(gui_->mainContentScene()));
    registerScene("pvp_intro", new PlayerVsPlayerIntro(gui_->mainContentScene()));
    registerScene("recruit_army", new RecruitmentScene(gui_->mainContentScene()));
    registerScene("prebattle", new PreBattleScene(gui_->mainContentScene()));
    registerScene("hotseatgame", new HotSeatGame());

    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_]->hide();

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

bool GuiSceneManager::hideMainWindow() {
    gui_->hide();
    return true;
}

bool GuiSceneManager::showMainWindow() {
    gui_->show();
    return true;
}

bool GuiSceneManager::hideDetachedScene(QString scene_name) {
    if (scenes_.count(scene_name) == 0 || !scenes_[scene_name])
        return false;
    scenes_[scene_name]->hide();
    return true;
}

bool GuiSceneManager::showDetachedScene(QString scene_name, QString args) {
    if (scenes_.count(scene_name) == 0 || !scenes_[scene_name])
        return false;
    scenes_[scene_name]->parseArgs(args);
    scenes_[scene_name]->init();
    scenes_[scene_name]->show();
    return true;
}
