#include "adapters/windows/input/WindowsKeyInput.h"

#include <Windows.h>

#include "foundation/input/KeyNames.h"

namespace ta::adapters::windows::input
{
    bool WindowsKeyInput::pressed(const int virtual_key) const noexcept
    {
        return ta::foundation::input::is_valid_key_index(virtual_key) &&
            (GetAsyncKeyState(virtual_key) & 1) != 0;
    }

    int WindowsKeyInput::first_pressed_key() const noexcept
    {
        for (int key = 1; key < static_cast<int>(ta::foundation::input::key_names.size()); ++key)
        {
            if (ta::foundation::input::is_valid_key_index(key) && (GetAsyncKeyState(key) & 1) != 0)
                return key;
        }
        return 0;
    }

    void WindowsKeyInput::clear_pressed_state() const noexcept
    {
        for (int key = 1; key < static_cast<int>(ta::foundation::input::key_names.size()); ++key)
        {
            if (ta::foundation::input::is_valid_key_index(key))
                static_cast<void>(GetAsyncKeyState(key));
        }
    }
}
