#include "GameDistributionAppMenu.h"

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

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


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

    for (int i = 1; i <= this->settings->getLoadablePiecesSize(); i++) {
        this->playerCursor.addRow(i, 1);
    }
}

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

    PiecesList& piecesList = this->settings->getMenu().getPiecesList();

    if (this->playerCursor.getPosition().y == 0) {
        if (this->playerCursor.movedLeft()) {
            piecesList.setDistributionMode(DistributionMode((int) piecesList.getDistributionMode() - 1));
        }
        if (this->playerCursor.movedRight()) {
            piecesList.setDistributionMode(DistributionMode((int) piecesList.getDistributionMode() + 1));
        }
    }
    else {
        if (piecesList.getDistributionMode() != CUSTOM) {
            this->playerCursor.goToPosition({0, 0});
        }
        else {
            if (this->playerCursor.movedLeft()) {
                this->settings->decreaseDistribution(this->playerCursor.getPosition().y);
            }
            if (this->playerCursor.movedRight()) {
                this->settings->increaseDistribution(this->playerCursor.getPosition().y);
            }
        }
    }

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

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

    const DistributionMode distributionMode = this->settings->getMenu().readPiecesList().getDistributionMode();
    const std::vector<int>& distributions = this->settings->getDistributions();

    int firstElem = std::clamp(((int) this->playerCursor.getPosition().y) - 1, 0, this->settings->getLoadablePiecesSize() - 3);
    if (firstElem == 0) {
        this->placeText(text, this->playerCursor, "< DISTRIBUTION MODE: " + getPiecesDistributionName(distributionMode) + " >", 5.f, 15.f, sf::Vector2u{0, 0});
    }
    else {
        this->placeText(text, this->playerCursor, "< SIZE " + std::to_string(firstElem) + " PROBABILITY: " + std::to_string(distributions.at(firstElem)) + " >", 5.f, 15.f, sf::Vector2u{(unsigned int) firstElem, 0});
    }
    
    if (distributionMode != CUSTOM) {
        text.setFillColor(sf::Color(100, 100, 100));
    }

    this->placeText(text, this->playerCursor, "< SIZE " + std::to_string(firstElem + 1) + " PROBABILITY: " + std::to_string(distributions.at(firstElem + 1)) + " >", 5.f, 25.f, sf::Vector2u{0, (unsigned int) firstElem + 1});
    this->placeText(text, this->playerCursor, "< SIZE " + std::to_string(firstElem + 2) + " PROBABILITY: " + std::to_string(distributions.at(firstElem + 2)) + " >", 5.f, 35.f, sf::Vector2u{0, (unsigned int) firstElem + 2});
    this->placeText(text, this->playerCursor, "< SIZE " + std::to_string(firstElem + 3) + " PROBABILITY: " + std::to_string(distributions.at(firstElem + 3)) + " >", 5.f, 45.f, sf::Vector2u{0, (unsigned int) firstElem + 3});
    
    this->renderWindow->display();
}
