#include <Display/WidgetDisplay/CreditsDisplay/CreditsDisplay.hpp>
#include <Display/WidgetDisplay/MainMenuDisplay/MainMenuDisplay.hpp>
#include <Utilities/Utilities.hpp>
#include <Material/Material.hpp>
#include <Widget/Layout/Column/Column.hpp>
#include <Widget/Layout/Center/Center.hpp>
#include <Widget/Button/Button.hpp>
#include <Log/Log.hpp>
#include <Display/WidgetDisplay/WorldSelection/WorldSelection.hpp>
#include <Display/WidgetDisplay/ServerSelection/ServerSelection.hpp>
#include <Widget/Layout/CompoundWidget/Deck/Deck.hpp>
#include <Widget/Slider/Slider.hpp>
#include <Widget/Panel/Panel.hpp>
#include <Widget/Label/Label.hpp>
#include <Settings/Settings.hpp>
#include <Display/WidgetDisplay/SettingsDisplay/SettingsDisplay.hpp>
#include <Widget/Layout/Row/Row.hpp>
#include <Version/Version.hpp>
#include <Widget/AnimationView/AnimationView.hpp>
#include <Widget/IconLabel/IconLabel.hpp>

void MainMenuDisplay::onSinglePlayer()
{
    Display::navPush<WorldSelection>();
}

void MainMenuDisplay::onMultiPlayer()
{
    Display::navPush<ServerSelection>();
}

void MainMenuDisplay::onSettings()
{
    Display::navPush<SettingsDisplay>();
}

void MainMenuDisplay::onCredits()
{
    Display::navPush<CreditsDisplay>();
}

void MainMenuDisplay::onQuit()
{
    Log::info("Selected 'quit' from main menu!");
    endGame();
}

Widget* MainMenuDisplay::makeVersionViewer()
{
    if (Version::ofThisBuild)
    {
        return new Center(new Label(formatString("$+", Version::ofThisBuild), Color::faded));
    }
    else
    {
        return new Center(
            new Deck<Row, Center>(
                new IconLabel(Material::alert),
                new Label("DEVELOPMENT BUILD", Color::warning),
                new IconLabel(Material::alert)
            )
        );
    }
}

MainMenuDisplay::MainMenuDisplay()
{
    setWidget(
        new Center(
            new Column(
                new Center(new AnimationView(Animation::logo)),

                new Panel(glm::ivec2(0, 4)),
                makeVersionViewer(),
                new Panel(glm::ivec2(0, 8)),

                new Center(
                    new Column(
                        (new Button("Single-player", [this](){this->onSinglePlayer();}))->withIcon(Material::singleplayer),
                        new Panel(glm::ivec2(0, 2)),
                        (new Button("Multi-player", [this](){this->onMultiPlayer();}))->withIcon(Material::multiplayer),
                        new Panel(glm::ivec2(0, 2)),
                        (new Button("Settings", [this](){this->onSettings();}))->withIcon(Material::settings),
                        new Panel(glm::ivec2(0, 2)),
                        (new Button("Credits", [this](){this->onCredits();}))->withIcon(Material::heart),
                        new Panel(glm::ivec2(0, 8)),
                        (new Button("Quit", [this](){this->onQuit();}))->withIcon(Material::exitIcon)->withFaceColor(Color::back)
                    )
                )
            )
        )
    );
}