#include "ControllerComponent.hpp"
#include <string>

ControllerComponent::ControllerComponent(TransformComponent *_transform, Vec2D _deltaPos)
{
    transform = _transform;
    deltaPos = _deltaPos;
}

ControllerComponent::~ControllerComponent()
{
}

void ControllerComponent::Update(SDL_Event e, int frame, std::vector<std::string> broadcastList)
{
    ParseAndUpdateBroadcast(broadcastList);
    const Uint8 *currentKeyStates = SDL_GetKeyboardState(NULL);

    Vec2D pos = transform->getPos();

    int dX = 0;
    int dY = 0;

    if (currentKeyStates[SDL_SCANCODE_LEFT] || currentKeyStates[SDL_SCANCODE_A])
    {
        // printf("Left \n");
        dX = -deltaPos.x;
    }
    else if (currentKeyStates[SDL_SCANCODE_RIGHT] || currentKeyStates[SDL_SCANCODE_D])
    {
        // printf("Right \n");
        dX = deltaPos.x;
    }

    if (currentKeyStates[SDL_SCANCODE_UP] || currentKeyStates[SDL_SCANCODE_W])
    {
        // printf("Left \n");
        dY = -deltaPos.y;
    }
    else if (currentKeyStates[SDL_SCANCODE_DOWN] || currentKeyStates[SDL_SCANCODE_S])
    {
        // printf("Right \n");
        dY = deltaPos.y;
    }

    transform->SetTransform(dX + pos.x, dY + pos.y);
}

void ControllerComponent::ShowComponentEditor(bool *showEditor)
{
    ImGuiWindowFlags window_flags = 0;
    window_flags |= ImGuiWindowFlags_MenuBar;
    window_flags |= ImGuiWindowFlags_NoCollapse;

    if (ImGui::Begin("Editing Controller Component", showEditor, window_flags))
    {
        ImGui::Text("Nothing to edit");
        ImGui::End();
    }
}
