// 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 <Physics.hpp>

namespace ia::iae
{
    BOOL g_physicsDebugDrawEnabled = false;

    Vector<PhysicsComponent *> g_physicsComponents;

    VOID Physics::Initialize()
    {
    }

    VOID Physics::Terminate()
    {
    }

    VOID Physics::Update()
    {
        if(Engine::WasInputKeyPressed(InputKey::F6))
            g_physicsDebugDrawEnabled = !g_physicsDebugDrawEnabled;
    }

    VOID Physics::DebugDraw()
    {
        if (!g_physicsDebugDrawEnabled)
            return;

        for (const auto &t : g_physicsComponents)
        {
            for (const auto &c : t->Colliders())
            {
                auto color = Color{192, 0, 0, 0xFF};
                if (c.IsTrigger)
                    color = {64, 96, 192, 192};
                Engine::DebugDraw_SetColor(color);
                Engine::DebugDraw_SetStrokeWidth(2);
                Engine::DebugDraw_StrokeRect(t->GetNode()->GetPosition() + c.Position -
                                                 Engine::GetActiveCamera()->GetNode()->GetPosition(),
                                             c.Size);
            }
        }
    }

    Handle Physics::RegisterComponent(IN PhysicsComponent *component)
    {
        g_physicsComponents.pushBack(component);
        return g_physicsComponents.size() - 1;
    }

    BOOL Physics::CanMove(IN Handle handle, IN CONST PhysicsComponent::Collider &collider, IN glm::vec2 movement)
    {
        const auto comp = g_physicsComponents[handle];
        const auto pos = comp->GetNode()->GetPosition() + movement + collider.Position;
        for (const auto &t : g_physicsComponents)
        {
            if (t == comp)
                continue;
            for (const auto &tc : t->Colliders())
            {
                const auto tPos = t->GetNode()->GetPosition() + tc.Position;
                const auto xColliding = ((pos.x + collider.Size.x) >= tPos.x) && ((tPos.x + tc.Size.x) >= pos.x);
                const auto yColliding = ((pos.y + collider.Size.y) >= tPos.y) && ((tPos.y + tc.Size.y) >= pos.y);
                if (xColliding && yColliding)
                {
                    // Collision callback
                    comp->OnCollision(t);
                    t->OnCollision(comp);

                    // Overlap block
                    if (tc.IsTrigger)
                        continue;
                    return false;
                }
            }
        }
        return true;
    }
} // namespace ia::iae