/////////////////////////////////////////////////////////////////////
// File: Dashboard.cpp                                             //
// Date: 28/8/11                                                   //
// Desc: The main window which holds all UI screens. Similar to    //
//   the OS Desktop, this window is responsible for managing and   //
//   laying out windows within the game's UI.                      //
/////////////////////////////////////////////////////////////////////

#include "Dashboard.hpp"
#include "ballistic/resource.h"

/*--------------------------------------------------------------------------+
| Returns the global instance of the dashboard, instantiating on first use. |
+--------------------------------------------------------------------------*/
Dashboard& dashboard() {
    static Dashboard instance;
    return instance;
}


/********************/
/*** Screen class ***/
/********************/

/*--------------------------------------------------------------------------+
| Default Constructor. Sets the dashboard as the parent.                    |
+--------------------------------------------------------------------------*/
Screen::Screen() :
    QWidget(&dashboard()) {
}

void Screen::showEvent(QShowEvent*) {
    dashboard().notifyScreenShown(this);
}

void Screen::closeEvent(QCloseEvent*) {
    dashboard().notifyScreenClosed(this);
}


/***********************/
/*** Dashboard class ***/
/***********************/

/*--------------------------------------------------------------------------+
| Dashboard Constructor. Sets up window title, size, etc.                   |
+--------------------------------------------------------------------------*/
Dashboard::Dashboard() :
    QWidget(),
    currentScreen(NULL),
    previousScreen(NULL),
    notificationsEnabled(true) {
    setWindowTitle(APP_NAME);

    // Position in the center of the main desktop screen, 800x600 size
    // TODO: Fullscreen
    const QRect desktop = QApplication::desktop()->availableGeometry();
    resize(800, 600);
    setMinimumSize(800, 600);
    move((desktop.width()  - width())  / 2,
         (desktop.height() - height()) / 2);
}

/*--------------------------------------------------------------------------+
| Notifies the dashboard that the given screen has been shown.              |
| This allows it to hide any other previously showing screens.              |
+--------------------------------------------------------------------------*/
void Dashboard::notifyScreenShown(Screen* screen) {
    if (!notificationsEnabled) return;
    if (currentScreen) currentScreen->hide();
    previousScreen = currentScreen;
    currentScreen = screen;
}

/*--------------------------------------------------------------------------+
| Notifies the dashboard that the given screen has been closed.             |
| This allows it to show the screen that was previously showing.            |
+--------------------------------------------------------------------------*/
void Dashboard::notifyScreenClosed(Screen*) {
    if (!notificationsEnabled) return;
    if (previousScreen) {
        currentScreen = previousScreen;
        disableNotifications();
        currentScreen->show();
        enableNotifications();
    }
}