#include "../../repository/repository.h"
#include "../../domain/trench.h"
#include "../../vector/vector.h"
#include "user_controller.h"
#include <stdio.h>

UserController::UserController(Repository* repo,Repository* shoppingRepository) {
    this->_repo = repo;
    this->_shoppingRepository = shoppingRepository;
}

UserController::~UserController() {
    delete this->_shoppingRepository;
}

void UserController::addProductToShoppingList(Trench* trench) {
    int index = this->_shoppingRepository->get(trench);
    int repo_index = this->_repo->get(trench);
    if(index==-1) {
        Trench* trenchCopy = new Trench(trench->size(),trench->colour(),trench->price(),1,trench->photograph());
        this->_shoppingRepository->add(trenchCopy);
        trench->quantity(trench->quantity()-1);
        (*this->_repo)[repo_index]->quantity((*this->_repo)[repo_index]->quantity()-1);
        return;
    }
    (*this->_shoppingRepository)[index]->quantity((*this->_shoppingRepository)[index]->quantity()+1);
    trench->quantity(trench->quantity()-1);
    (*this->_repo)[repo_index]->quantity((*this->_repo)[repo_index]->quantity()-1);

}

void UserController::emptyShoppingList() {
    for(Trench* i : *this->_shoppingRepository) {
        int index = this->_repo->get(i);
        (*this->_repo)[index]->quantity((*this->_repo)[index]->quantity()+i->quantity());
    }
    while(this->_shoppingRepository->size()){
        this->_shoppingRepository->remove(0);
    }
}

void UserController::buyShoppingList() {
    while(this->_shoppingRepository->size()){
        this->_shoppingRepository->remove(0);
    }
}

int UserController::shoppingListTotal() {
    int total = 0;
    for(Trench* i : *this->_shoppingRepository) {
        total+=i->quantity()*i->price();
    }
    return total;
}


Vector<Trench*> UserController::getTrenchCarousel(int size) {
    Vector<Trench*> trenchCarousel;
    for(Trench* i : *this->_repo){
        if((size == -1||i->size()==size) && i->quantity()>0){
            Trench* copy = new Trench(*i);
            trenchCarousel.add(copy);
        }
    }
    return trenchCarousel;
}

Vector<Trench*> UserController::getShoppingListTrench(){
    Vector<Trench*> trenchCarousel;
    for(Trench* i : *this->_shoppingRepository){
        Trench* copy = new Trench(*i);
        trenchCarousel.add(copy);
    }
    return trenchCarousel;
}