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

#include "Jump180Factory.h"

namespace JamSpook {

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

Jump180Factory::~Jump180Factory()
{}

shared_ptr<Entity> Jump180Factory::createJump180(const vec3 position,
                                                 const vec3 eulerRotationXYZDegrees)
{
    // Create instance
    shared_ptr<Entity> entity = make_shared<Entity>();
    entity->setEntityTag("jump180");
    SceneGraph::addEntity(entity);

    // Create transform
//    mat4 rotateToCenter = eulerAngleXYZ(radians(static_cast<float>(-90)),
//                                        radians(static_cast<float>(0)),
//                                        radians(static_cast<float>(0)));

    mat4 rotation = eulerAngleXYZ(
            radians(static_cast<float>(eulerRotationXYZDegrees.x)),
            radians(static_cast<float>(eulerRotationXYZDegrees.y)),
            radians(static_cast<float>(eulerRotationXYZDegrees.z)));

    mat4 translation = translate(mat4(1.0f), position);

    mat4 transform = rotation * translation;// * rotateToCenter;


//    mat4 transform = translate(mat4(1), position);

    // Add physics component
    ColliderFactory* colliderFactory = new ColliderFactory(mPhysicsSystem, mAssetSystem);
    shared_ptr<Jump180PhysicsComponent> physicsComponent =
            make_shared<Jump180PhysicsComponent>(
                    transform,
                    entity,
                    getShared(mPhysicsSystem),
                    getShared(mPhysicsSystem),
                    getShared(mPhysicsSystem),
                    colliderFactory->createConvexHullCollider(position,
                                                              quat_cast(rotation),
                                                              "jump180.bcosp",  // filePath
                                                              "jump180-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 of a ball
    ModelRenderableFactory* modelFactory = new ModelRenderableFactory(mAssetSystem);
    shared_ptr<ModelRenderable> modelRenderable = modelFactory->createModel("jump180.bcosm",
                                                                            "jump180.png",
                                                                            "jump180-model");
    delete modelFactory;
    graphicsComponent->addRenderable(modelRenderable);

    // Return instance
    return entity;
}

} // namespace JamSpook
