/*
	GameEngine and Editor
	by John Ryland
	Copyright (c) 2023
*/

////////////////////////////////////////////////////////////////////////////////////
//	Game State

#include "GameState.h"

namespace GameEngine {

GameState::GameState(GameRuntime::Schema* _schema)
{
    //GameRuntime::StructDescription fields{   };
    //ComponentType sceneObjectType{ lastUsedComponentTypeId, "Object", fields, GameRuntime::CalculateComponentSize(schema, fields), .enabled = true };
    //components[lastUsedComponentTypeId] = sceneObjectType;
    //entities
    schema = _schema;
}

void GameState::RegisterComponentType()
{
}

StringList GameState::AllComponentTypeNames()
{
    StringList compTypeNames;
    for (auto& compType : components)
    {
        if (compType.second.enabled)
            compTypeNames.emplace_back(compType.second.name);
    }
    return compTypeNames;
}

ComponentTypeId GameState::FindComponentTypeByName(String componentName)
{
    for (auto& compType : components)
    {
        if (compType.second.enabled && compType.second.name == componentName)
            return compType.second.id;
    }
    return INVALID_COMPONENT_TYPE_ID;
}

void GameState::NewEntity()
{
}

void GameState::NewComponent()
{
}

GameRuntime::Schema* GameState::Schema()
{
    return schema;
}

void GameState::RemoveStaleComponentReferences()
{
    for (auto& compType : components)
    {
        if (!compType.second.enabled)
        {
            for (auto& ent : entities)
            {
                if (ent.second.components.count(compType.second.id))
                {
                    free(ent.second.components[compType.second.id]);
                    ent.second.components.erase(compType.second.id);
                }
            }
        }
    }
}

} // GameEngine namespace
