// 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 <Physics.hpp>
#include <IAEngine/Nodes/Node2D.hpp>
#include <IAEngine/Components/PhysicsComponent.hpp>

namespace ia::iae
{
    PhysicsComponent::PhysicsComponent(IN Node2D *node) : IComponent(node)
    {
        m_physicsHandle = Physics::RegisterComponent(this);
    }

    VOID PhysicsComponent::Draw()
    {
    }

    VOID PhysicsComponent::DebugDraw()
    {
    }

    VOID PhysicsComponent::Update()
    {
        m_velocity = {};
    }

    VOID PhysicsComponent::FixedUpdate()
    {
    }

    Handle PhysicsComponent::CreateCollider()
    {
        m_colliders.pushBack({});
        return m_colliders.size() - 1;
    }

    Handle PhysicsComponent::AddCollider(IN Collider collider)
    {
        m_colliders.pushBack(collider);
        return m_colliders.size() - 1;
    }

    PhysicsComponent::Collider &PhysicsComponent::GetCollider(IN Handle handle)
    {
        return m_colliders[handle];
    }

    VOID PhysicsComponent::Move(IN glm::vec2 direction)
    {
        IA_ASSERT(m_isDynamic);
        const auto v = direction * m_movementSpeed;
        m_velocity += v;
        for(const auto& t: m_colliders)
        {
            if(!Physics::CanMove(m_physicsHandle, t, v))
                return;
        }
        m_node->SetLocalPosition(m_node->GetLocalPosition() + v);
    }

    VOID PhysicsComponent::Jump(IN FLOAT32 force)
    {
    }

    VOID PhysicsComponent::OnCollision(IN PhysicsComponent *other)
    {
    }

} // namespace ia::iae