#include "SettingsMainAppMenu.h"

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

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


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

}

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

    switch (this->playerCursor.getPosition().y) {
        case 2 : {
            if (this->playerCursor.movedLeft()) {
                if (this->settings->shortenWindow()) {
                    this->settings->changeVideoMode(*this->renderWindow);
                }
            }
            if (this->playerCursor.movedRight()) {
                if (this->settings->widenWindow()) {
                    this->settings->changeVideoMode(*this->renderWindow);
                }
            }
            break;
        }
        case 3 : {
            if (this->playerCursor.movedLeft()) {
                this->settings->shortenStartTimer();
            }
            if (this->playerCursor.movedRight()) {
                this->settings->lengthenStartTimer();
            }
            break;
        }
    }

    if (this->enterReleased) {
        if (this->playerCursor.getPosition().y == 0) {
            this->menuStack->push(std::make_shared<SettingsKeybindsAppMenu>(this->menuStack, this->settings, this->renderWindow));
        }
        if (this->playerCursor.getPosition().y == 1) {
            this->menuStack->push(std::make_shared<SettingsControlsAppMenu>(this->menuStack, this->settings, this->renderWindow));
        }
    }
    if (this->escReleased) {
        this->menuStack->pop();
    }
}

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

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

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

    this->placeText(text, this->playerCursor, "CHANGE KEYBINDS", 5.f, 15.f, sf::Vector2u{0, 0});
    this->placeText(text, this->playerCursor, "CHANGE CONTROLS", 5.f, 25.f, sf::Vector2u{0, 1});
    this->placeText(text, this->playerCursor, "< WINDOW SIZE: " + std::to_string(windowSize.x) + "x" + std::to_string(windowSize.y) + " >", 5.f, 35.f, sf::Vector2u{0, 2});
    this->placeText(text, this->playerCursor, "< START TIMER: " + std::to_string(this->settings->getStartTimerLength()) + "s >", 5.f, 45.f, sf::Vector2u{0, 3});
    
    this->renderWindow->display();
}
