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

#include "KartFactory.h"

namespace JamSpook {

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

KartFactory::~KartFactory()
{}

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

    // Create transform
    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;

    // Add physics component
    ColliderFactory* colliderFactory = new ColliderFactory(mPhysicsSystem, mAssetSystem);
    shared_ptr<KartPhysicsComponent> physicsComponent =
            make_shared<KartPhysicsComponent>(
                    transform,
                    entity,
                    getShared(mPhysicsSystem),
                    getShared(mPhysicsSystem),
                    getShared(mPhysicsSystem),
                    colliderFactory->createConvexHullCollider(position,
                                                              quat_cast(rotation),
                                                              "kart-hulls.bcosp",  // filePath
                                                              "kart-hulls-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(1,1,1));  // allow all positional movement movement
    physicsComponent->getCollider()->getRigidBody()->setAngularFactor(btVector3(1,1,1)); // allow all rotational movement movement
    physicsComponent->getCollider()->getRigidBody()->setRestitution(0.1f);
    physicsComponent->getCollider()->getRigidBody()->setFriction(0.01f);
//    physicsComponent->getCollider()->getRigidBody()->setGravity(btVector3(0, 0, 0));       // no gravity, length of zero
    physicsComponent->getCollider()->getRigidBody()->setActivationState(DISABLE_DEACTIVATION); // RigidBody may never sleep. (Note: Or else it will not respond to setLinearVelocity when standing still for too long(a couple of seconds).)

    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> modelRenderableKart = modelFactory->createModel("kart-vehicle.bcosm",
                                                                                "kart-vehicle-diffuse.png",
                                                                                "kart-vehicle-model");
    graphicsComponent->addRenderable(modelRenderableKart);

    shared_ptr<ModelRenderable> modelRenderableDriver = modelFactory->createModel("kart-driver.bcosm",
                                                                                  "kart-driver-diffuse.png",
                                                                                  "kart-driver-model");
    graphicsComponent->addRenderable(modelRenderableDriver);

    delete modelFactory;

    // Add internal pointlight
    LightSourceFactory* lightSourceFactory = new LightSourceFactory(mAssetSystem, mGraphicsSystem);
    shared_ptr<LightSource> lightSource = lightSourceFactory->createPointLight(vec3(1,1,1),
                                                                               0.02f,
                                                                               0.03f,
                                                                               transform);
    graphicsComponent->setLightSource(lightSource);
    delete lightSourceFactory;

    // Return instance
    return entity;
}

} // namespace JamSpook
