#include "Model3DList.h"

#include "Model3D.h"
#include "Text.h"

using namespace Framework;

const char* Standart3DTypes::cube = "f_cube";
const char* Standart3DTypes::texturModel = "f_texturModel";

// Contents of the Model3DList class
// Constructor
Model3DList::Model3DList()
    : ReferenceCounter()
{
    models = new RCArray<Model3DData>();
    names = new RCArray<Text>();
}

// Destructor
Model3DList::~Model3DList()
{
    models->release();
    names->release();
}

// Adds a model to the list
//  mdl: The model
//  name: The name under which the model is stored in the list
bool Model3DList::addModel(Model3DData* mdl, const char* name)
{
    for (auto i : *names)
    {
        if (i->isEqual(name))
        {
            mdl->release();
            return 0;
        }
    }
    models->add(mdl);
    names->add(new Text(name));
    return 1;
}

// Removes a model from the list
//  name: The name of the model
void Model3DList::removeModel(const char* name)
{
    int index = 0;
    for (auto i : *names)
    {
        if (i->isEqual(name))
        {
            names->remove(index);
            models->remove(index);
            return;
        }
        index++;
    }
}

// Checks whether a model has been stored under a specific name
//  name: The name
//  return: true if a model with the name exists
bool Model3DList::hasModel(const char* name) const
{
    for (auto i : *names)
    {
        if (i->isEqual(name))
        {
            return 1;
        }
    }
    return 0;
}

// Returns a specific model
//  name: The name of the model
Model3DData* Model3DList::getModel(const char* name) const
{
    int index = 0;
    for (auto i : *names)
    {
        if (i->isEqual(name))
        {
            return models->get(index);
        }
        index++;
    }
    return 0;
}

// Returns a specific model without increased reference counter
//  name: The name of the model
Model3DData* Model3DList::zModel(const char* name) const
{
    int index = 0;
    for (auto i : *names)
    {
        if (i->isEqual(name))
        {
            return models->z(index);
        }
        index++;
    }
    return 0;
}

//! remove All models
DLLEXPORT void Model3DList::removeAll()
{
    models->clear();
    names->clear();
}