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

AdminController::AdminController(Repository* repo){
    this->_repo = repo;
}
AdminController::~AdminController(){
    delete this->_repo;
}
void AdminController::add(int size,const char* colour,int price,int quantity,const char* photograph){
    if(size<=0){
        throw AdminControllerException("Size must be greater than 0");
    }
    if(strlen(colour)==0){
        throw AdminControllerException("Colour must not be empty");
    }
    if(price<=0){
        throw AdminControllerException("Price must be greater than 0");
    }
    if(quantity<0){
        throw AdminControllerException("Quantity must be greater than or equal to 0");
    }
    if(strlen(photograph)==0){
        throw AdminControllerException("Photograph must not be empty");
    }
    Trench* trench = new Trench(size,colour,price,quantity,photograph);
    try{
        this->_repo->add(trench);
    }
    catch(RepoException& e){
        delete trench;
        throw AdminControllerException(e.what());
    }
}
void AdminController::remove(int size,const char* colour,const char* photograph){
    if(size<=0){
        throw AdminControllerException("Size must be greater than 0");
    }
    if(strlen(colour)==0){
        throw AdminControllerException("Colour must not be empty");
    }
    if(strlen(photograph)==0){
        throw AdminControllerException("Photograph must not be empty");
    }
    Trench* trench = new Trench(size,colour,0,0,photograph);
    try{
        this->_repo->remove(trench);
    }
    catch(RepoException& e){
        delete trench;
        throw AdminControllerException(e.what());
    }
    delete trench;
}
void AdminController::remove(int index){
    this->_repo->remove(index);
}
void AdminController::update(int size,const char* colour,int price,int quantity,const char* photograph){
    if(size<=0){
        throw AdminControllerException("Size must be greater than 0");
    }
    if(strlen(colour)==0){
        throw AdminControllerException("Colour must not be empty");
    }
    if(price!=-1 && price<=0){
        throw AdminControllerException("Price must be greater than 0");
    }
    if(quantity!=-1 && quantity<0){
        throw AdminControllerException("Quantity must be greater than or equal to 0");
    }
    if(strlen(photograph)==0){
        throw AdminControllerException("Photograph must not be empty");
    }
    Trench* trench = new Trench(size,colour,price,quantity,photograph);
    int index = this->_repo->get(trench);
    delete trench;
    if(index==-1){
        throw AdminControllerException("Trench does not exist");
    }
    this->_repo->update(index,price,quantity);
    
}
int AdminController::get(int size,const char* colour,const char* photograph){
    if(size<=0){
        throw AdminControllerException("Size must be greater than 0");
    }
    if(strlen(colour)==0){
        throw AdminControllerException("Colour must not be empty");
    }
    if(strlen(photograph)==0){
        throw AdminControllerException("Photograph must not be empty");
    }
    Trench* trench = new Trench(size,colour,0,0,photograph);
    int index = this->_repo->get(trench);
    delete trench;
    return index;

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