#include "pbio/DynamicTemplateIndex.h"
#import <Foundation/Foundation.h>
#import "DynamicTemplateIndex.h"


#import "TryCatch.mm"
#include <fstream>


@implementation DynamicTemplateIndex

-(nonnull instancetype) init: (void*) templ_index{
    _ptr = templ_index;
    return self;
}

-(void) dealloc{
    delete static_cast<pbio::DynamicTemplateIndex::Ptr*>(_ptr);
}

-(void* _Nonnull) ptr{
    return _ptr;
}

-(NSString *_Nonnull) getMethodName{
    pbio::DynamicTemplateIndex::Ptr& templ_index = *(static_cast<pbio::DynamicTemplateIndex::Ptr*>(_ptr));
    std::string value = tryCatch(templ_index->getMethodName());
    return [NSString stringWithUTF8String:value.c_str()];
}


-(size_t) size {
    pbio::DynamicTemplateIndex::Ptr& templ_index = *(static_cast<pbio::DynamicTemplateIndex::Ptr*>(_ptr));
    return tryCatch(templ_index->size());
}

-(size_t) capacity {
    pbio::DynamicTemplateIndex::Ptr& templ_index = *(static_cast<pbio::DynamicTemplateIndex::Ptr*>(_ptr));
    return tryCatch(templ_index->capacity());
}

-(ContextTemplate* _Nonnull) get: (int64_t) index{
    pbio::DynamicTemplateIndex::Ptr& templ_index = *(static_cast<pbio::DynamicTemplateIndex::Ptr*>(_ptr));
    pbio::ContextTemplate::Ptr templ = tryCatch(templ_index->get(index));
    return [[ContextTemplate alloc] init: new pbio::ContextTemplate::Ptr(templ)];
}

-(ContextTemplate* _Nonnull) at: (NSString *_Nonnull) key{
    pbio::DynamicTemplateIndex::Ptr& templ_index = *(static_cast<pbio::DynamicTemplateIndex::Ptr*>(_ptr));
    pbio::ContextTemplate::Ptr templ = tryCatch(templ_index->at([key UTF8String]));
    return [[ContextTemplate alloc] init: new pbio::ContextTemplate::Ptr(templ)];
}

-(void) add: (ContextTemplate* _Nonnull) templ : (NSString *_Nonnull) uuid{
    pbio::DynamicTemplateIndex::Ptr& templ_index = *(static_cast<pbio::DynamicTemplateIndex::Ptr*>(_ptr));
    pbio::ContextTemplate::Ptr& tmp_templ = *(static_cast<pbio::ContextTemplate::Ptr*>(templ.ptr));
    tryCatch(templ_index->add(tmp_templ, [uuid UTF8String]));
}

-(void) remove: (NSString *_Nonnull) uuid{
    pbio::DynamicTemplateIndex::Ptr& templ_index = *(static_cast<pbio::DynamicTemplateIndex::Ptr*>(_ptr));
    tryCatch(templ_index->remove([uuid UTF8String]));
}

-(void) concat: (DynamicTemplateIndex *_Nonnull) other{
    pbio::DynamicTemplateIndex::Ptr& templ_index = *(static_cast<pbio::DynamicTemplateIndex::Ptr*>(_ptr));
    pbio::DynamicTemplateIndex::Ptr& other_index = *(static_cast<pbio::DynamicTemplateIndex::Ptr*>(other.ptr));
    tryCatch(templ_index->concat(other_index));
}

@end
