#include "SimCore/SimCore.hpp"
#include <SimCore/IdentifierMaker.hpp>
#include <memory>
#include <loguru.hpp>


namespace SimCore {

IdentifierMaker::IdentifierMaker()
{}

std::shared_ptr<SimCore::Identifier> IdentifierMaker::getNewIdentifier(std::uint32_t parent,SimCore::ObjectSource ObjectSource ){
    
    std::lock_guard<std::mutex> lock(mx_);

    long size = IDList_.size();

    int lastNumber = 0;
    if (size > 0) {

        for (std::list<std::shared_ptr<SimCore::Identifier>>::iterator it=IDList_.begin(); it != IDList_.end(); ++it){
            if (ObjectSource == it->get()->isExternal()) {
                lastNumber = it->get()->getNumber();        
            }
        }

        int newNumber = lastNumber+1;

        auto id_ = std::make_shared<SimCore::Identifier>(parent,newNumber,ObjectSource);
        IDList_.push_back(id_);
        return id_;

    }else{
        auto id_ = std::make_shared<SimCore::Identifier>(parent,1,ObjectSource);
        IDList_.push_back(id_);
        return id_;
    }

}

bool IdentifierMaker::isInList(std::shared_ptr<SimCore::Identifier> ID){
std::lock_guard<std::mutex> lock(mx_);

 for (std::list<std::shared_ptr<SimCore::Identifier>>::iterator it=IDList_.begin(); it != IDList_.end(); ++it){
      if (ID.get() == it->get()) {
        return true;
      }
    }
    return false;
}

size_t IdentifierMaker::size(){
    std::lock_guard<std::mutex> lock(mx_);

    return IDList_.size();
}


}