#include "SettingsKeybindsAppMenu.h"

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

#include <stack>
#include <memory>
#include <string>
#include <regex>
#include <filesystem>
#include <algorithm>
#include <SFML/Graphics.hpp>


SettingsKeybindsAppMenu::SettingsKeybindsAppMenu(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, 1, 1, 1, 1, 1, 1, 1, 1}) {

    this->selectedAnAction = false;

    for (Action action : ACTION_LIST_IN_ORDER) {
        std::string textureName = ACTION_NAMES[action];
        textureName = std::regex_replace(textureName, std::regex(" "), "");

        const Asset& textureData = getResource("data/images/keybinds/" + textureName + ".png");

        this->iconTextures[action] = sf::Texture(textureData.data, textureData.size, false, {{0, 0}, {16, 16}});
    }
}

void SettingsKeybindsAppMenu::computeFrame() {
    this->updateMetaBinds();

    if (!this->selectedAnAction) {
        this->playerCursor.updatePosition();

        if (this->playerCursor.movedLeft()) {
            this->settings->selectPreviousKeybinds();
        }
        if (this->playerCursor.movedRight()) {
            this->settings->selectNextKeybinds();
        }
    }
    else {
        bool addedKeybind = false;
        for (const auto& [key, string] : KEYS_TO_STRING) {
            if (sf::Keyboard::isKeyPressed(key) && (key != sfKey::Enter) && (key != sfKey::Escape)) {
                this->settings->getKeybinds().addKey(this->actionSelected, key);
                addedKeybind = true;
            }

            if (addedKeybind) {
                this->selectedAnAction = false;
                break;
            }
        }
    }

    if (this->enterReleased && this->settings->getKeybinds().isModifiable()) {
        this->selectedAnAction = !selectedAnAction;
        this->actionSelected = ACTION_LIST_IN_ORDER[this->playerCursor.getPosition().y - 1];
    }
    if (this->escReleased) {
        if (this->selectedAnAction) {
            this->settings->getKeybinds().clearKeys(this->actionSelected);
            this->selectedAnAction = false;
        }
        else {
            this->menuStack->pop();
        }
    }
}

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

    if (this->settings->getKeybindsLayout() == CUSTOMIZABLE_KEYBINDS) {
        this->placeText(text, this->playerCursor, "< CUSTOM >", 5.f, 15.f, sf::Vector2u{0, 0});
    }
    else {
        this->placeText(text, this->playerCursor, "< DEFAULT " + std::to_string(this->settings->getKeybindsLayout() + 1) + " >", 5.f, 15.f, sf::Vector2u{0, 0});
    }

    if (this->selectedAnAction) {
        text.setOutlineColor(sf::Color(255, 0, 0));
    }

    int i = 0;
    int firstElem = std::clamp(((int) this->playerCursor.getPosition().y) - 2, 0, 8);

    for (Action action : ACTION_LIST_IN_ORDER) {
        if (i >= firstElem && i < (firstElem + 3)) {
            sf::String string;
            bool firstKey = true;
            for (sfKey key : this->settings->getKeybinds().getKeybinds(action)) {
                if (KEYS_TO_STRING.contains(key)) {
                    std::string keyString = KEYS_TO_STRING.at(key);
                    if (firstKey) {
                        string += keyString;
                        firstKey = false;
                    }
                    else {
                        string += ", " + keyString;
                    }
                }
            }

            this->placeText(text, this->playerCursor, setStringToUpperCase(ACTION_NAMES[action]), 15.f, ((i - firstElem) * 10) + 25.f, sf::Vector2u{0, (unsigned int) i + 1});
            text.setOutlineThickness(0);
            this->placeText(text, {}, string, 40.f, ((i - firstElem) * 10) + 25.f, {});

            sf::Sprite sprite(this->iconTextures[action]);
            sprite.setOrigin(sprite.getLocalBounds().getCenter());
            sprite.setPosition(sf::Vector2f(8.f, ((i - firstElem) * 10) + 25.f) * (float) this->settings->getWindowSizeMultiplier());
            sprite.setScale(sprite.getScale() * ((float) this->settings->getWindowSizeMultiplier() / 2));
            this->renderWindow->draw(sprite);
        }
        i++;
    }

    this->renderWindow->display();
}
