#include "PlayerCursor.h"

#include "Keybinds.h"
#include "Settings.h"

#include <vector>
#include <algorithm>
#include <SFML/Graphics.hpp>

static const int MENU_DAS = FRAMES_PER_SECOND / 2;


PlayerCursor::PlayerCursor(std::vector<unsigned int> rows) :
    rows(rows) {

    this->position = sf::Vector2u({0, 0});
    this->leftDAS = 0;
    this->rightDAS = 0;
    this->upDAS = 0;
    this->downDAS = 0;
}

void PlayerCursor::updatePosition() {
    (sf::Keyboard::isKeyPressed(sfKey::Left)) ? (this->leftDAS++) : (this->leftDAS = 0);
    if (this->shouldMove(this->leftDAS)) {
        this->moveLeft();
    }

    (sf::Keyboard::isKeyPressed(sfKey::Right)) ? (this->rightDAS++) : (this->rightDAS = 0);
    if (this->shouldMove(this->rightDAS)) {
        this->moveRight();
    }

    (sf::Keyboard::isKeyPressed(sfKey::Up)) ? (this->upDAS++) : (this->upDAS = 0);
    if (this->shouldMove(this->upDAS)) {
        this->moveUp();
    }

    (sf::Keyboard::isKeyPressed(sfKey::Down)) ? (this->downDAS++) : (this->downDAS = 0);
    if (this->shouldMove(this->downDAS)) {
        this->moveDown();
    }
}

bool PlayerCursor::movedLeft() const {
    return this->shouldMove(this->leftDAS);
}

bool PlayerCursor::movedRight() const {
    return this->shouldMove(this->rightDAS);
}

bool PlayerCursor::movedUp() const {
    return this->shouldMove(this->upDAS);
}

bool PlayerCursor::movedDown() const {
    return this->shouldMove(this->downDAS);
}

void PlayerCursor::goToPosition(const sf::Vector2u& newPosition) {
    if (this->rows.size() > newPosition.y) {
        if (this->rows.at(newPosition.y) > newPosition.x) {
            this->position = newPosition;
        }
    }
}

bool PlayerCursor::addPosition(unsigned int x, unsigned int y) {
    if (y >= this->rows.size()) return false;
    if (x > this->rows.at(y)) return false;

    this->rows.at(y)++;
    if ((y == this->position.y) && (x <= this->position.x)) {
        this->position.x++;
    }
    return true;
}

bool PlayerCursor::removePosition(unsigned int x, unsigned int y) {
    if (y >= this->rows.size()) return false;
    if (x >= this->rows.at(y)) return false;

    this->rows.at(y)--;
    if ((y == this->position.y) && (x < this->position.x)) {
        this->position.x--;
    }
    return true;
}

bool PlayerCursor::addRow(unsigned int position, unsigned int width) {
    if (position > this->rows.size()) return false;

    this->rows.insert(this->rows.begin() + position, width);
    if (position <= this->position.y) {
        this->position.y++;
    }
    return true;
}

bool PlayerCursor::removeRow(unsigned int position) {
    if (position >= this->rows.size()) return false;

    this->rows.erase(this->rows.begin() + position);
    if (position < this->position.y) {
        this->position.y--;
    }
    return true;
}

const sf::Vector2u& PlayerCursor::getPosition() const {
    return this->position;
}

bool PlayerCursor::shouldMove(int DAS) const {
    return (DAS == 1
        || (DAS > MENU_DAS && (DAS % 5) == 0)
        || (DAS > (FRAMES_PER_SECOND * 2)));
}

void PlayerCursor::moveLeft() {
    if (this->position.x == 0) {
        this->position.x = this->rows.at(this->position.y) - 1;
    }
    else {
        this->position.x--;
    }
}

void PlayerCursor::moveRight() {
    if (this->position.x == this->rows.at(this->position.y) - 1) {
        this->position.x = 0;
    }
    else {
        this->position.x++;
    }
}

void PlayerCursor::moveUp() {
    if (this->position.y == 0) {
        this->position.y = this->rows.size() - 1;
    }
    else {
        this->position.y--;
    }

    if (this->position.x >= this->rows.at(this->position.y)) {
        this->position.x = this->rows.at(this->position.y) - 1;
    }
}

void PlayerCursor::moveDown() {
    if (this->position.y == this->rows.size() - 1) {
        this->position.y = 0;
    }
    else {
        this->position.y++;
    }

    if (this->position.x >= this->rows.at(this->position.y)) {
        this->position.x = this->rows.at(this->position.y) - 1;
    }
}
