#include "admin_controller.h"
#include "../../exceptions/exceptions.h"
#include <string>
#include <algorithm>


AdminController::AdminController(std::shared_ptr<FileRepository> repo){
    this->_repo = repo;
}

AdminController::~AdminController(){
    
}
void AdminController::add(int size,std::string colour,int price,int quantity,std::string photograph){
    Trench trench(size,colour,price,quantity,photograph);
    this->_repo->add(trench);
}
void AdminController::remove(int size,std::string colour,std::string photograph){
    Trench trench(size,colour,0,0,photograph);
    this->_repo->remove(trench);
}
void AdminController::remove(int index){
    this->_repo->remove(index);
}
void AdminController::update(int size,std::string colour,int price,int quantity,std::string photograph){
    Trench trench(size,colour,price,quantity,photograph);
    int index = this->_repo->get(trench);
    if(index==-1){
        throw AdminControllerException("Trench does not exist");
    }
    this->_repo->update(index,price,quantity);
    
}
int AdminController::get(int size,std::string colour,std::string photograph){
    if(size<=0){
        throw AdminControllerException("Size must be greater than 0");
    }
    if(colour.length()==0){
        throw AdminControllerException("Colour must not be empty");
    }
    if(photograph.length()==0){
        throw AdminControllerException("Photograph must not be empty");
    }
    Trench trench(size,colour,0,0,photograph);
    int index = this->_repo->get(trench);
    return index;

}
int AdminController::size(){
    return this->_repo->size();
}
Trench& AdminController::operator[](int index){
    return (*this->_repo)[index];
}
std::shared_ptr<FileRepository> AdminController::getAll(){
    return this->_repo;
}