/*
 * ResetTriggerFactory.cpp
 *
 *  Created on: Aug 19, 2020
 *      Author: fredrick
 */

#include "ResetTriggerFactory.h"

namespace JamSpook {

ResetTriggerFactory::ResetTriggerFactory(weak_ptr<AssetSystem>    assetSystem,
                                         weak_ptr<GraphicsSystem> graphicsSystem,
                                         weak_ptr<PhysicsSystem>  physicsSystem,
                                         weak_ptr<RenderLayer>    renderLayer):
        mAssetSystem(assetSystem),
        mGraphicsSystem(graphicsSystem),
        mPhysicsSystem(physicsSystem),
        mRenderLayer(renderLayer)
{}

ResetTriggerFactory::~ResetTriggerFactory()
{}

shared_ptr<Entity> ResetTriggerFactory::createResetTrigger(const vec3 position,
                                                           const vec3 dimensions)
{
    // Create instance
    shared_ptr<Entity> entity = make_shared<Entity>();
    entity->setEntityTag("end-trigger");
    SceneGraph::addEntity(entity);
    mat4 transform = translate(mat4(1), position);

    // Add physics component
    ColliderFactory* colliderFactory = new ColliderFactory(mPhysicsSystem, mAssetSystem);
    shared_ptr<ResetTriggerPhysicsComponent> physicsComponent =
            make_shared<ResetTriggerPhysicsComponent>(
                    transform,
                    entity,
                    getShared(mPhysicsSystem),
                    getShared(mPhysicsSystem),
                    getShared(mPhysicsSystem),
                    colliderFactory->createBoxTriggerVolume(position,
                                                            quat(vec3(0,0,0)),
                                                            vec3(dimensions.x/2, dimensions.y/2, dimensions.z/2),
                                                            1.0f,
                                                            entity,
                                                            entity->getEntityTag()));
    delete colliderFactory;
    // Note: Editing of rigid body params, maybe only has effect is it has been added to the world.
    physicsComponent->getCollider()->getRigidBody()->setLinearFactor(btVector3(0, 0, 0));  // deny positional movement movement in any axis
    physicsComponent->getCollider()->getRigidBody()->setAngularFactor(btVector3(0, 0, 0)); // deny rotational movement movement in any axis
    physicsComponent->getCollider()->getRigidBody()->setRestitution(1);
    entity->addComponent(physicsComponent);

    // Add graphics component
    shared_ptr<GraphicsComponent> graphicsComponent = make_shared<GraphicsComponent>(transform,
                                                                                     entity,
                                                                                     getShared(mGraphicsSystem),
                                                                                     mRenderLayer);
    entity->addComponent(graphicsComponent);

    // Add the ModelRenderable
    ModelRenderableFactory* modelFactory = new ModelRenderableFactory(mAssetSystem);
    shared_ptr<ModelRenderable> modelRenderable = modelFactory->createModel("cube.bcosm",
                                                                            "trigger-diffuse.png",
                                                                            "cube-model");
    delete modelFactory;
    graphicsComponent->addRenderable(modelRenderable);

    // Return instance
    return entity;
}

} // namespace JamSpook
