#include "repo_tests.h"
#include "../../repository/repository.h"
#include "../../domain/trench.h"
#include "../../exceptions/exceptions.h"

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

void test_repo(void){
    Repository* repo = new Repository();

    Trench* t1 = new Trench(1,"Colour1",1,1,"Photo1");
    Trench* t2 = new Trench(2,"Colour2",2,2,"Photo2");
    Trench* t3 = new Trench(3,"Colour1",3,3,"Photo3");

    int check=0;

    repo->add(t1);
    repo->add(t2);
    repo->add(t3);
    assert(repo->size()==3);
    assert(repo->get(t1)==0);    
    assert(repo->get(t2)==1);    
    assert(repo->get(t3)==2);
    assert((*repo)[0]==t1);
    repo->remove(t2);
    repo->remove(1);
    assert(repo->size()==1);
    repo->update(0,2,-1);
    assert((*repo)[0]->price()==2);
    assert((*repo)[0]->quantity()==1);
    repo->update(0,-1,2);
    assert((*repo)[0]->price()==2);
    assert((*repo)[0]->quantity()==2);
    repo->update(0,3,3);
    assert((*repo)[0]->price()==3);
    assert((*repo)[0]->quantity()==3);
    assert(*(repo->begin())==t1);
    assert(*(repo->end()-1)==t1);
    
    Trench* t4 = new Trench(4,"Colour4",4,4,"Photo4");
    try{
        repo->add(t1);
    }
    catch(RepoException& e){
        assert(strcmp(e.what(),"Trench already exists")==0);
        check=1;
    }
    assert(check);
    check=0;
    try{
        repo->remove(t4);
    }
    catch(RepoException& e){
        assert(strcmp(e.what(),"Trench does not exists")==0);
        check=1;
    }
    assert(check);
    check=0;
    try{
        repo->remove(-6);
    }
    catch(RepoException& e){
        assert(strcmp(e.what(),"Index out of bounds")==0);
        check=1;
    }
    assert(check);
    check=0;
    try{
        (*repo)[54];
    }
    catch(RepoException& e){
        assert(strcmp(e.what(),"Index out of bounds")==0);
        check=1;
    }
    assert(check);
    check=0;
    try{
        repo->update(-6,1,1);
    }
    catch(RepoException& e){
        assert(strcmp(e.what(),"Index out of bounds")==0);
        check=1;
    }
    assert(check);
    check=0;
    try{
        repo->update(0,-3,1);
    }
    catch(RepoException& e){
        assert(strcmp(e.what(),"Invalid price")==0);
        check=1;
    }
    assert(check);
    check=0;
    try{
        repo->update(0,3,-5);
    }
    catch(RepoException& e){
        assert(strcmp(e.what(),"Invalid quantity")==0);
        check=1;
    }
    assert(check);
    delete t4;
    delete repo;

}
