#include "GameBoardAppMenu.h"

#include "AppMenu.h"
#include "../PlayerCursor.h"

#include <stack>
#include <memory>
#include <SFML/Graphics.hpp>


GameBoardAppMenu::GameBoardAppMenu(std::shared_ptr<MenuStack> menuStack, std::shared_ptr<Settings> settings, std::shared_ptr<sf::RenderWindow> renderWindow) :
    AppMenu(menuStack, settings, renderWindow),
    playerCursor({1, 1}) {

}

void GameBoardAppMenu::computeFrame() {
    this->updateMetaBinds();
    this->playerCursor.updatePosition();

    Menu& menu = this->settings->getMenu();

    switch (this->playerCursor.getPosition().y) {
        case 0 : {
            if (this->playerCursor.movedLeft()) {
                menu.setBoardWidth(std::max(1, menu.getBoardWidth() - 1));
            }
            if (this->playerCursor.movedRight()) {
                menu.setBoardWidth(std::min(MAXIMUM_BOARD_WIDTH, menu.getBoardWidth() + 1));
            }
            break;
        }
        case 1 : {
            if (this->playerCursor.movedLeft()) {
                menu.setBoardHeight(std::max(1, menu.getBoardHeight() - 1));
            }
            if (this->playerCursor.movedRight()) {
                menu.setBoardHeight(std::min(MAXIMUM_BOARD_HEIGHT, menu.getBoardHeight() + 1));
            }
            break;
        }
    }

    if (this->escReleased) {
        this->menuStack->pop();
    }
}

void GameBoardAppMenu::drawFrame() const {
    this->renderWindow->clear(sf::Color(200, 200, 200));

    const Menu& menu = this->settings->getMenu();

    sf::Text text(this->pressStartFont, "", this->settings->getWindowSizeMultiplier() * 2);
    text.setFillColor(sf::Color(0, 0, 0));
    text.setOutlineColor(sf::Color(255, 255, 255));

    this->placeTitle(text, {}, "BOARD SETTINGS", 5.f, {});

    this->placeText(text, this->playerCursor, "< BOARD WIDTH: " + std::to_string(menu.getBoardWidth()) + " >", 5.f, 15.f, sf::Vector2u{0, 0});
    this->placeText(text, this->playerCursor, "< BOARD HEIGHT: " + std::to_string(menu.getBoardHeight()) + " >", 5.f, 25.f, sf::Vector2u{0, 1});
    
    this->renderWindow->display();
}
