// IAEngine: 2D Game Engine by IA
// Copyright (C) 2025 IASoft (PVT) LTD (oss@iasoft.dev)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

#include <IAEngine/Engine.hpp>
#include <InputManager.hpp>

namespace ia::iae
{
    EXTERN SDL_Window *g_windowHandle;

    BOOL InputManager::s_keys[256];
    BOOL InputManager::s_prevKeys[256];
    InputKey InputManager::s_axisInputs[4];
    Vec2 InputManager::s_pointerPosition{};
    Map<String, Handle> InputManager::s_actionNames;
    Vector<Vector<InputKey>> InputManager::s_actions;

    VOID InputManager::Initialize()
    {
        memset(s_keys, 0, sizeof(s_keys));
        memset(s_prevKeys, 0, sizeof(s_prevKeys));
    }

    VOID InputManager::Terminate()
    {
    }

    VOID InputManager::OnSDLEvent(IN SDL_Event *event)
    {
        memcpy(s_prevKeys, s_keys, sizeof(s_prevKeys));
        switch (event->type)
        {
        case SDL_EVENT_KEY_DOWN:
            s_keys[event->key.scancode] = true;
            break;
        
        case SDL_EVENT_KEY_UP:
            s_keys[event->key.scancode] = false;
            break;

        case SDL_EVENT_MOUSE_MOTION:
            s_pointerPosition = {event->motion.x, event->motion.y};
            break;

        default:
            break;
        }
    }

    Vec2 InputManager::GetAxis()
    {
        return Vec2{
            IsKeyDown(s_axisInputs[3]) + IsKeyDown(s_axisInputs[2]) * -1,
            IsKeyDown(s_axisInputs[1]) + IsKeyDown(s_axisInputs[0]) * -1
        };
    }

    VOID InputManager::SwitchModeToText()
    {
        SDL_StartTextInput(g_windowHandle);
    }

    VOID InputManager::SwitchModeToAction()
    {
        SDL_StopTextInput(g_windowHandle);
    }

    Vec2 InputManager::GetPointerPosition()
    {
        return s_pointerPosition;
    }

    BOOL InputManager::IsKeyDown(IN InputKey key)
    {
        return s_keys[(UINT8) key];
    }

    BOOL InputManager::WasKeyPressed(IN InputKey key)
    {
        return !s_prevKeys[(UINT8) key] && s_keys[(UINT8) key];
    }

    BOOL InputManager::WasKeyReleased(IN InputKey key)
    {
        return s_prevKeys[(UINT8) key] && !s_keys[(UINT8) key];
    }

    BOOL InputManager::IsActionDown(IN Handle action)
    {
        const auto& t = s_actions[action];
        for(const auto& k : t)
            if(IsKeyDown(k))
                return true;
        return false;
    }

    BOOL InputManager::WasActionPressed(IN Handle action)
    {
        const auto& t = s_actions[action];
        for(const auto& k : t)
            if(WasKeyPressed(k))
                return true;
        return false;
    }

    BOOL InputManager::WasActionReleased(IN Handle action)
    {
        const auto& t = s_actions[action];
        for(const auto& k : t)
            if(WasKeyReleased(k))
                return true;
        return false;
    }

    BOOL InputManager::IsActionDown(IN CONST String& action)
    {
        return IsActionDown(s_actionNames[action]);
    }

    BOOL InputManager::WasActionPressed(IN CONST String& action)
    {
        return WasActionPressed(s_actionNames[action]);
    }

    BOOL InputManager::WasActionReleased(IN CONST String& action)
    {
        return WasActionReleased(s_actionNames[action]);
    }

    Handle InputManager::BindAction(IN CONST String &name, IN InputKey key)
    {
        s_actions.pushBack({key});
        const auto handle = s_actions.size() - 1;
        s_actionNames[name] = handle; 
        return handle;
    }

    VOID InputManager::BindAxis(IN InputKey upKey, IN InputKey downKey, IN InputKey leftKey, IN InputKey rightKey)
    {
        s_axisInputs[0] = upKey;
        s_axisInputs[1] = downKey;
        s_axisInputs[2] = leftKey;
        s_axisInputs[3] = rightKey;
    }
} // namespace ia::iae

namespace ia::iae
{
    Vec2 Engine::GetInputAxis()
    {
        return InputManager::GetAxis();
    }

    VOID Engine::SwitchInputModeToText()
    {
        InputManager::SwitchModeToText();
    }

    VOID Engine::SwitchInputModeToAction()
    {
        InputManager::SwitchModeToAction();
    }

    Vec2 Engine::GetInputPointerPosition()
    {
        return InputManager::GetPointerPosition();
    }

    BOOL Engine::IsInputKeyDown(IN InputKey key)
    {
        return InputManager::IsKeyDown(key);
    }

    BOOL Engine::WasInputKeyPressed(IN InputKey key)
    {
        return InputManager::WasKeyPressed(key);
    }

    BOOL Engine::WasInputKeyReleased(IN InputKey key)
    {
        return InputManager::WasKeyReleased(key);
    }

    BOOL Engine::IsInputActionDown(IN Handle action)
    {
        return InputManager::IsActionDown(action);
    }

    BOOL Engine::WasInputActionPressed(IN Handle action)
    {
        return InputManager::WasActionPressed(action);
    }

    BOOL Engine::WasInputActionReleased(IN Handle action)
    {
        return InputManager::WasActionReleased(action);
    }

    BOOL Engine::IsInputActionDown(IN CONST String& action)
    {
        return InputManager::IsActionDown(action);
    }

    BOOL Engine::WasInputActionPressed(IN CONST String& action)
    {
        return InputManager::WasActionPressed(action);
    }

    BOOL Engine::WasInputActionReleased(IN CONST String& action)
    {
        return InputManager::WasActionReleased(action);
    }

    Handle Engine::BindInputAction(IN CONST String &name, IN InputKey key)
    {
        return InputManager::BindAction(name, key);
    }

    VOID Engine::BindInputAxis(IN InputKey upKey, IN InputKey downKey, IN InputKey leftKey, IN InputKey rightKey)
    {
        InputManager::BindAxis(upKey, downKey, leftKey, rightKey);
    }
} // namespace ia::iae