#include "repository.h"
#include "../vector/vector.h"
#include <string.h>
#include<time.h>
#include <stdlib.h>

Repository* _Repository_Constructor(){
    Repository* repository = (Repository*)malloc(sizeof(Repository));
    if (repository==NULL)
        return NULL;
    repository->elements=_Vector_Constructor(10);
    return repository;
}

void _Repository_Destructor(Repository* repository){
    if (repository==NULL)
        return;
    _Vector_Destructor(repository->elements);
    repository->elements=NULL;
    free(repository);

}

int _Repository_add(Repository* repository, Material* material){
    if (repository==NULL)
        return 1;
    if (material==NULL)
        return 1;
    for(int i=0;i<_Vector_getLength(repository->elements);i++){
        Material* item =_Vector_getItem(repository->elements,i);
        if(strcmp(item->name,material->name)==0&&strcmp(item->name,material->name)==0&&item->expiration_date.tm_mday==material->expiration_date.tm_mday&&item->expiration_date.tm_mon==material->expiration_date.tm_mon&&item->expiration_date.tm_year==material->expiration_date.tm_year){
            _Material_SetQuantity(item,_Material_GetQuantity(item)+_Material_GetQuantity(material));
            return 2;
        }
    }
    _Vector_add(repository->elements,material);
    return 0;
}

int _Repository_delete(Repository* repository, Material* material){
    if (repository==NULL)
        return 1;
    if (material==NULL)
        return 1;
    for(int i=0;i<_Vector_getLength(repository->elements);i++){
        Material* item =_Vector_getItem(repository->elements,i);
        if(strcmp(item->name,material->name)==0&&strcmp(item->name,material->name)==0&&item->expiration_date.tm_mday==material->expiration_date.tm_mday&&item->expiration_date.tm_mon==material->expiration_date.tm_mon&&item->expiration_date.tm_year==material->expiration_date.tm_year){
            int quantity = _Material_GetQuantity(item);
            _Vector_delete(repository->elements,i); 
            _Material_SetQuantity(material,quantity);   
            return 0;
        }
    }
    return 1;
}

int _Repository_update(Repository* repository, Material* material){
    if (repository==NULL)
            return 1;
        if (material==NULL)
            return 1;
        for(int i=0;i<_Vector_getLength(repository->elements);i++){
            Material* item =_Vector_getItem(repository->elements,i);
            if(strcmp(item->name,material->name)==0&&strcmp(item->name,material->name)==0&&item->expiration_date.tm_mday==material->expiration_date.tm_mday&&item->expiration_date.tm_mon==material->expiration_date.tm_mon&&item->expiration_date.tm_year==material->expiration_date.tm_year){
                int quantity = _Material_GetQuantity(item);
                _Material_SetQuantity(item,_Material_GetQuantity(material));
                _Material_SetQuantity(material,quantity);    
                return 0;
            }
        }
    return 1;
}

int _Repository_getLenght(Repository* repository){
    return _Vector_getLength(repository->elements);
}

Material* _Repository_getItem(Repository* repository,int index){
    return (Material*)_Vector_getItem(repository->elements,index);
}