/*
 * GoalFactory.cpp
 *
 *  Created on: Aug 14, 2020
 *      Author: fredrick
 */

#include "GoalFactory.h"

namespace JamSpook {

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

GoalFactory::~GoalFactory()
{}

shared_ptr<Entity> GoalFactory::createGoal(const vec3 position)
{
    // Create instance
    shared_ptr<Entity> entity = make_shared<Entity>();
    entity->setEntityTag("goal");
    SceneGraph::addEntity(entity);
    mat4 transform = translate(mat4(1), position);

    // Add physics component
    ColliderFactory* colliderFactory = new ColliderFactory(mPhysicsSystem, mAssetSystem);
    shared_ptr<GoalPhysicsComponent> physicsComponent =
            make_shared<GoalPhysicsComponent>(
                    transform,
                    entity,
                    getShared(mPhysicsSystem),
                    getShared(mPhysicsSystem),
                    getShared(mPhysicsSystem),
                    colliderFactory->createConvexHullCollider(position,
                                                              quat(vec3(0,0,0)),
                                                              "goal.bcosp",  // filePath
                                                              "goal-shape", // name
                                                              100.0f, // mass
                                                              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("goal.bcosm",
                                                                            "goal.png",
                                                                            "goal-model");
    delete modelFactory;
    graphicsComponent->addRenderable(modelRenderable);

    // Return instance
    return entity;
}

} // namespace JamSpook
