#include "admin_controller_tests.h"
#include "../../controllers/admin_controller/admin_controller.h"
#include "../../repository/repository.h"
#include "../../domain/trench.h"
#include "../../exceptions/exceptions.h"

#include<assert.h>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>

void test_admin_controller(void){
    Repository* repo = new Repository();
    AdminController* controller = new AdminController(repo);
    int check = 0;

    try{
        controller->add(0,"1",1,1,"1");
    }
    catch(AdminControllerException& e){
        assert(strcmp(e.what(),"Size must be greater than 0")==0);
        check=1;
    }
    assert(check);
    check=0;
    try{
        controller->add(1,"",1,1,"1");
    }
    catch(AdminControllerException& e){
        assert(strcmp(e.what(),"Colour must not be empty")==0);
        check=1;
    }
    assert(check);
    check=0;
    try{
        controller->add(1,"1",0,1,"1");
    }
    catch(AdminControllerException& e){
        assert(strcmp(e.what(),"Price must be greater than 0")==0);
        check=1;
    }
    assert(check);
    check=0;
    try{
        controller->add(1,"1",1,-1,"1");
    }
    catch(AdminControllerException& e){
        assert(strcmp(e.what(),"Quantity must be greater than or equal to 0")==0);
        check=1;
    }
    assert(check);
    check=0;
    try{
        controller->add(1,"1",1,1,"");
    }
    catch(AdminControllerException& e){
        assert(strcmp(e.what(),"Photograph must not be empty")==0);
        check=1;
    }
    assert(check);
    check=0;
    controller->add(1,"1",1,1,"1");
    try{
        controller->add(1,"1",1,1,"1");
    }
    catch(AdminControllerException& e){
        assert(strcmp(e.what(),"Trench already exists")==0);
        check=1;
    }
    assert(check);
    check=0;
    try{
        controller->remove(0,"1","1");
    }
    catch(AdminControllerException& e){
        assert(strcmp(e.what(),"Size must be greater than 0")==0);
        check=1;
    }
    assert(check);
    check=0;
    try{
        controller->remove(1,"","1");
    }
    catch(AdminControllerException& e){
        assert(strcmp(e.what(),"Colour must not be empty")==0);
        check=1;
    }
    assert(check);
    check=0;
    try{
        controller->remove(1,"1","");
    }
    catch(AdminControllerException& e){
        assert(strcmp(e.what(),"Photograph must not be empty")==0);
        check=1;
    }
    assert(check);
    check=0;
    controller->remove(1,"1","1");
    try{
        controller->remove(1,"1","1");
    }
    catch(AdminControllerException& e){
        assert(strcmp(e.what(),"Trench does not exists")==0);
        check=1;
    }
    assert(check);
    check=0;
    controller->add(1,"1",1,1,"1");
    controller->remove(0);
    try{
        controller->remove(1);
    }
    catch(RepoException& e){
        assert(strcmp(e.what(),"Index out of bounds")==0);
        check=1;
    }
    assert(check);
    check=0;
    controller->add(1,"1",1,1,"1");


    try{
        controller->update(0,"1",1,1,"1");
    }
    catch(AdminControllerException& e){
        assert(strcmp(e.what(),"Size must be greater than 0")==0);
        check=1;
    }
    assert(check);
    check=0;
    try{
        controller->update(1,"",1,1,"1");
    }
    catch(AdminControllerException& e){
        assert(strcmp(e.what(),"Colour must not be empty")==0);
        check=1;
    }
    assert(check);
    check=0;
    try{
        controller->update(1,"1",0,1,"1");
    }
    catch(AdminControllerException& e){
        assert(strcmp(e.what(),"Price must be greater than 0")==0);
        check=1;
    }
    assert(check);
    check=0;
    try{
        controller->update(1,"1",1,-2,"1");
    }
    catch(AdminControllerException& e){
        assert(strcmp(e.what(),"Quantity must be greater than or equal to 0")==0);
        check=1;
    }
    assert(check);
    check=0;
    try{
        controller->update(1,"1",1,1,"");
    }
    catch(AdminControllerException& e){
        assert(strcmp(e.what(),"Photograph must not be empty")==0);
        check=1;
    }
    assert(check);
    check=0;
    controller->update(1,"1",2,2,"1");
    try{
        controller->update(2,"1",1,1,"1");
    }
    catch(AdminControllerException& e){
        assert(strcmp(e.what(),"Trench does not exist")==0);
        check=1;
    }
    assert(check);
    check=0;
    try{
        controller->get(0,"1","1");
    }
    catch(AdminControllerException& e){
        assert(strcmp(e.what(),"Size must be greater than 0")==0);
        check=1;
    }
    assert(check);
    check=0;
    try{
        controller->get(1,"","1");
    }
    catch(AdminControllerException& e){
        assert(strcmp(e.what(),"Colour must not be empty")==0);
        check=1;
    }
    assert(check);
    check=0;
    try{
        controller->get(1,"1","");
    }
    catch(AdminControllerException& e){
        assert(strcmp(e.what(),"Photograph must not be empty")==0);
        check=1;
    }
    assert(check);
    check=0;
    assert(controller->get(1,"1","1")==0);
    assert(controller->size()==1);
    assert((*controller)[0]->size()==1);
    assert(controller->getAll()==repo);

    delete controller;
}
