/*
 * SpriteFactory.cpp
 *
 *  Created on: Aug 24, 2020
 *      Author: fredrick
 */

#include "SpriteFactory.h"

namespace JamSpook {

SpriteFactory::SpriteFactory(weak_ptr<AssetSystem>    assetSystem,
                             weak_ptr<GraphicsSystem> graphicsSystem,
                             weak_ptr<RenderLayer>    renderLayer):
        mAssetSystem(assetSystem),
        mGraphicsSystem(graphicsSystem),
        mRenderLayer(renderLayer)
{}

SpriteFactory::~SpriteFactory()
{}

shared_ptr<Entity> SpriteFactory::createPlane(const string name,
                                              const string filePath,
                                              const vec3 position,
                                              const vec3 rotation,
                                              const vec3 scaleFactor)
{
    // Create instance
    shared_ptr<Entity> entity = make_shared<Entity>();
    entity->setEntityTag(name);
    SceneGraph::addEntity(entity);
    mat4 transform = scale(mat4(1), scaleFactor) * translate(mat4(1), position);

    // 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("plane-1x1.bcosm",
                                                                            filePath,
                                                                            name+"_model");
    delete modelFactory;
    graphicsComponent->addRenderable(modelRenderable);

    // Return instance
    return entity;
}

shared_ptr<Entity> SpriteFactory::createBackground(const string name,
                                                   const string filePath)
{
    // Create instance
    shared_ptr<Entity> entity = make_shared<Entity>();
    entity->setEntityTag(name);
    SceneGraph::addEntity(entity);
    mat4 transform = scale(mat4(1), vec3(1, 1, 1)) * translate(mat4(1), vec3(-1, -1, -1));

    // 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("plane-1x1.bcosm",
                                                                            filePath,
                                                                            name+"_model");
    delete modelFactory;
    graphicsComponent->addRenderable(modelRenderable);

    // Return instance
    return entity;
}

} // namespace JamSpook
