#include "Scene.h"

#include "RootNode.h"
#include "FontsManager.h"
#include "SceneItem.h"

#include <iostream>

Scene::Scene()
    :   mBackGroundColor(Color(255, 255, 255)),
        mRootNode(std::make_unique<RootNode>()),
        mSceneInfo(std::make_unique<SceneInfo>())

{
    mRootNode->setName("Scene_RootNode");
}

Scene::~Scene()
{

}

void Scene::addNode(AbstractVisualNode* node)
{
    mRootNode->addChild(node);
}

bool Scene::isEmpty() const
{
    return mRootNode->getChildren().empty();
}

const Color& Scene::getBackgroundColor() const
{
    return mBackGroundColor;
}

void Scene::setBackgroundColor(const Color& color)
{
    mBackGroundColor = color;
}

void Scene::update()
{
    mSceneItems.clear();
    updateNode(mRootNode.get(), Transform());
}

void Scene::updateNode(AbstractVisualNode* node, const Transform& transform)
{
    node->update(mSceneInfo.get());

    auto node_transform = node->getTransform();
    node_transform.applyPre(transform);

    for (auto child : node->getChildren())
    {
        if (child->getIsVisible())
        {
            updateNode(child, node_transform);
        }
    }

    for (std::size_t idx=0; idx< node->getNumSceneItems(); idx++)
    {
        if (auto item = node->getSceneItem(idx))
        {
            item->updateTransform(node_transform);
            mSceneItems.push_back(item);
        }
    }
}

const std::vector<SceneItem*>& Scene::getItems() const
{
    return mSceneItems;
}

bool Scene::shouldShowMeshOutline() const
{
    return mSceneInfo->mShowMeshOutline;
}

void Scene::setShowMeshOutline(bool shouldShow)
{
    mSceneInfo->mShowMeshOutline = shouldShow;
}

void Scene::setSupportsGeometryPrimitives(bool)
{
    mSceneInfo->mSupportsGeometryPrimitives = true;
}

void Scene::setFontsManager(FontsManager* fontsManager)
{
    mSceneInfo->mFontsManager = fontsManager;
}
