#include "RigidbodyComponent.hpp"


RigidbodyComponent::RigidbodyComponent(TransformComponent* pos, Vec2D dim, Vec2D vel, float m, 
                        bool elastic, bool grav) : IPhysicsComponent{pos, dim} {
    velocity = vel;
    mass = m;
    isElastic = elastic;
    usesGravity = grav;
}

RigidbodyComponent::~RigidbodyComponent(){
    // TODO
}

void RigidbodyComponent::ApplyForce(Vec2D force){
    // F=ma
    Vec2D acceleration = force / GetMass();
    ApplyAcceleration(acceleration);
}

void RigidbodyComponent::ApplyAcceleration(Vec2D acceleration){
    velocity += acceleration;
}

void RigidbodyComponent::ApplyVelocity(){
    m_position->SetTransform(m_position->getPos() + velocity);
}

void RigidbodyComponent::Update(SDL_Event e, int frame, std::vector<std::string> broadcastList){
    ParseAndUpdateBroadcast(broadcastList);
    m_position->SetTransform(m_position->getPos() + velocity);
}

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

    if(ImGui::Begin("Editing Rigidbody Component", showEditor, window_flags)){
        ImGui::InputFloat("Mass", &mass);
        ImGui::Checkbox("Elastic?", &isElastic);
        ImGui::Checkbox("Gravity?", &usesGravity);
        ImGui::End();
    }
}

// SETTERS

void RigidbodyComponent::SetVelocity(Vec2D vel){
    velocity = vel;
}

// GETTERS

bool RigidbodyComponent::IsKinematic(){return true;}
bool RigidbodyComponent::IsElastic(){return isElastic;}
bool RigidbodyComponent::UsesGravity(){return usesGravity;}
Vec2D RigidbodyComponent::GetVelocity(){return velocity;}
float RigidbodyComponent::GetMass(){return mass;}