#include "repository.h"
#include "../exceptions/exceptions.h"

Repository::Repository(){
    this->_data = new Vector<Trench*>();
}
Repository::~Repository(){
    delete this->_data;
}
void Repository::add(Trench* trench){
    if(this->get(trench)!=-1)
        throw RepoException("Trench already exists");
    this->_data->add(trench);

}
void Repository::remove(Trench* trench){
    int index = this->get(trench);
    if(index==-1)
        throw RepoException("Trench does not exists");
    this->_data->remove(index);
}
void Repository::remove(int index){
    if(index<0 || index > this->_data->size()-1)
        throw RepoException("Index out of bounds");
    this->_data->remove(index);
}

int Repository::get(Trench* trench){
    for(int i=0;i<this->_data->size();i++){
        if(*trench == *(*this->_data)[i])
            return i;
    }
    return -1;
    //return this->_data->get(trench);
}

Trench*& Repository::operator[](int index){
    if(index<0 || index > this->_data->size()-1)
        throw RepoException("Index out of bounds");
    return (*this->_data)[index];
}
void Repository::update(int index, int price, int quantity){
    if(index<0 || index > this->_data->size()-1)
        throw RepoException("Index out of bounds");
    Trench* trench = (*this->_data)[index];
    if(price!=-1 && price<=0)
        throw RepoException("Invalid price");
    if(quantity!=-1 && quantity<0)
        throw RepoException("Invalid quantity");
    if(price>0)
        trench->price(price);
    if(quantity>=0)
        trench->quantity(quantity);
}

int Repository::size(){
    return this->_data->size();
}

Trench** Repository::begin(){
    return this->_data->begin();
}
Trench** Repository::end(){
    return this->_data->end();
}