#include <algorithm>

#include "Input.h"
#include "hostVmShared.h"
#include "PicoRam.h"

Input::Input(PicoRam* memory)
{
    _memory = memory;

    std::fill(_currentKDown, _currentKDown + MaxPlayers, 0);
    std::fill(_currentKHeld, _currentKHeld + MaxPlayers, 0);
    for (int p = 0; p < MaxPlayers; p++) {
        std::fill(_framesHeld[p], _framesHeld[p] + 8, 0);
    }
}

void Input::SetState(uint8_t kdown, uint8_t kheld){
    SetPlayerState(0, kdown, kheld);
}

void Input::SetPlayerState(uint8_t p, uint8_t kdown, uint8_t kheld){
    if (p >= MaxPlayers) {
        return;
    }

    _currentKDown[p] = kdown;
    _currentKHeld[p] = kheld;
    //key 6 (PAUSE MENU) only fires for one frame, even if held
    if ((_currentKHeld[p] & BITMASK(6)) && !(_currentKDown[p] & BITMASK(6))) {
        _currentKHeld[p] = _currentKHeld[p] & ~(BITMASK(6));
    }
    //memory only stores buttons 0-5, not 6 or 7
    _memory->hwState.buttonStates[p] = kheld & 0x3F;

    uint8_t repeatDelay = _memory->hwState.btnpRepeatDelay == 0 
        ? 15 
        : _memory->hwState.btnpRepeatDelay;

    if (repeatDelay == 255){
        return;
    }

    uint8_t repeatInterval = _memory->hwState.btnpRepeatInterval == 0 
        ? 4 
        : _memory->hwState.btnpRepeatDelay;

    for (int i = 0; i < 7; i ++) {
        bool down = BITMASK(i) & kheld;

        _framesHeld[p][i] = down ? _framesHeld[p][i] + 1 : 0;

        //update kdown to be true if held for 15 frames, then every 4th after that.
        //from wiki:
        //btnp() implements a keyboard-like repeat mechanism: if the player holds 
        //the button for 15 frames, it registers as on again for one frame, then 
        //again every four frames after that. The frame counter resets when the 
        //player releases the button. 

        bool repeatPressed = 
            (_framesHeld[p][i] == repeatDelay) ||
            (_framesHeld[p][i] / repeatDelay >= 1 && _framesHeld[p][i] % repeatInterval == 0);

        if (repeatPressed) {
            _currentKDown[p] = _currentKDown[p] | BITMASK(i);
        }
    }
}

void Input::SetMouse(int16_t mouseX, int16_t mouseY, uint8_t mouseBtnState){
    _mouseX = mouseX;
    _mouseY = mouseY;
    _mouseBtnState = mouseBtnState;
}

void Input::SetKeyboard(bool kbDown, std::string kbKey){
	_kbDown = kbDown;
	_kbKey = kbKey;
}

uint16_t Input::btn(){
    return _currentKHeld[0] | ((uint16_t)_currentKHeld[1] << 8);
}

//todo: repetition behavior to match pico 8
uint16_t Input::btnp(){
    return _currentKDown[0] | ((uint16_t)_currentKDown[1] << 8);
}

bool Input::btn(uint8_t i){
    return btn(i, 0);
}

//todo: repetition behavior to match pico 8
bool Input::btnp(uint8_t i){
    return btnp(i, 0);
}

bool Input::btn(uint8_t i, uint8_t p){
    if (p >= MaxPlayers || i >= 8) {
        return false;
    }
    return BITMASK(i) & _currentKHeld[p];
}

//todo: repetition behavior to match pico 8
bool Input::btnp(uint8_t i, uint8_t p){
    if (p >= MaxPlayers || i >= 8) {
        return false;
    }
    return BITMASK(i) & _currentKDown[p];
}

int16_t Input::getMouseX() {
    return _mouseX;
}

int16_t Input::getMouseY() {
    return _mouseY;
}

uint8_t Input::getMouseBtnState() {
    return _mouseBtnState;
}

bool Input::getKeyDown() {
    return _kbDown;
}

const char* Input::getKey() {
    // Found this bug in Terra
    // PICO-8 behavior: stat(31) appears to consume the key from the buffer
    // After reading, stat(30) should return false until next key press
    _kbDown = false;
    return _kbKey.c_str();
}

