#include "SettingsControlsAppMenu.h"

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

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


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

}

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

    Player& playerControls = this->settings->getMenu().getPlayerControls();

    switch (this->playerCursor.getPosition().y) {
        case 0 : {
            if (this->playerCursor.movedLeft()) {
                playerControls.setDAS(playerControls.getDAS() - 1);
            }
            if (this->playerCursor.movedRight()) {
                playerControls.setDAS(playerControls.getDAS() + 1);
            }
            break;
        }
        case 1 : {
            if (this->playerCursor.movedLeft()) {
                playerControls.setARR(playerControls.getARR() - 1);
            }
            if (this->playerCursor.movedRight()) {
                playerControls.setARR(playerControls.getARR() + 1);
            }
            break;
        }
        case 2 : {
            if (this->playerCursor.movedLeft()) {
                playerControls.setSDR(playerControls.getSDR() - 1);
            }
            if (this->playerCursor.movedRight()) {
                playerControls.setSDR(playerControls.getSDR() + 1);
            }
            break;
        }
    }

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

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

    const Player& playerControls = this->settings->getMenu().readPlayerControls();

    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, {}, "CONTROLS SETTINGS", 5.f, {});

    sf::Vector2u windowSize = this->renderWindow->getSize();

    this->placeText(text, this->playerCursor, "< DAS: " + std::to_string(playerControls.getDAS()) + " >", 5.f, 15.f, sf::Vector2u{0, 0});
    this->placeText(text, this->playerCursor, "< ARR: " + std::to_string(playerControls.getARR()) + " >", 5.f, 25.f, sf::Vector2u{0, 1});
    this->placeText(text, this->playerCursor, "< SDR: " + std::to_string(playerControls.getSDR()) + " >", 5.f, 35.f, sf::Vector2u{0, 2});
    
    this->renderWindow->display();
}
