package tutorial.core.services.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;



import tutorial.core.models.entities.ActiveIngredient;
import tutorial.core.models.entities.ActiveIngredientRecomendation;
import tutorial.core.repositories.ActiveIngredientRecomendationRepo;
import tutorial.core.repositories.ActiveIngredientRepo;
import tutorial.core.services.ActiveIngredientService;
import tutorial.core.services.exceptions.ActiveIngredientExistsException;
import tutorial.core.services.exceptions.ActiveIngredientNotFoundException;
import tutorial.core.services.util.ActiveIngredientList;
import tutorial.core.services.util.ActiveIngredientRecomendationList;

/**
 * 
 * @author Xavier
 */
@Service
@Transactional
public class ActiveIngredientServiceImpl implements ActiveIngredientService {

    @Autowired
    private ActiveIngredientRepo activeIngredientRepo;

    @Autowired
    private ActiveIngredientRecomendationRepo entryRepo;

    @Override
    public ActiveIngredientRecomendation createActiveIngredientRecomendation(Long activeIngredientId, ActiveIngredientRecomendation data) {
        ActiveIngredient activeIngredient = activeIngredientRepo.findActiveIngredient(activeIngredientId);
        if(activeIngredient == null)
        {
            throw new ActiveIngredientNotFoundException();
        }
        ActiveIngredientRecomendation entry = entryRepo.createActiveIngredientRecomendation(data);
        entry.setActiveIngredient(activeIngredient);
        return entry;
    }

    @Override
    public ActiveIngredientList findAllActiveIngredients() {
        return new ActiveIngredientList(activeIngredientRepo.findAllActiveIngredients());
    }

    @Override
    public ActiveIngredientRecomendationList findAllActiveIngredientRecomendations(Long activeIngredientId) {
        ActiveIngredient activeIngredient = activeIngredientRepo.findActiveIngredient(activeIngredientId);
        if(activeIngredient == null)
        {
            throw new ActiveIngredientNotFoundException();
        }
        return new ActiveIngredientRecomendationList(activeIngredientId, entryRepo.findByActiveIngredientId(activeIngredientId));
    }

    @Override
    public ActiveIngredient findActiveIngredient(Long id) {
        return activeIngredientRepo.findActiveIngredient(id);
    }
    
    @Override
    public ActiveIngredient findActiveIngredientByName(String name) {
        return activeIngredientRepo.findActiveIngredientByName(name);
    }

    @Override
    public ActiveIngredient createActiveIngredient(ActiveIngredient data) {
        ActiveIngredient activeIngredientSameName = activeIngredientRepo.findActiveIngredientByName(data.getName());

        if(activeIngredientSameName != null)
        {
            throw new ActiveIngredientExistsException();
        }

        ActiveIngredient createdActiveIngredient = activeIngredientRepo.createActiveIngredient(data);

        return createdActiveIngredient;
    }

    @Override
    public void deleteAllActiveIngredients() {
        activeIngredientRepo.deleteAllActiveIngredients();
    }
}
