package fyloz.trial.ColorRecipesExplorer.services;

import fyloz.trial.ColorRecipesExplorer.dao.MixTypeDao;
import fyloz.trial.ColorRecipesExplorer.model.Material;
import fyloz.trial.ColorRecipesExplorer.model.MixType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MixTypeService extends GenericService<MixType> {

    private MixTypeDao mixTypeDao;

    @Autowired
    public MixTypeService(MixTypeDao mixTypeDao) {
        super(mixTypeDao);
        this.mixTypeDao = mixTypeDao;
    }

    public MixType getByName(String name) {
        return mixTypeDao.findByTypeName(name);
    }

    public MixType getByMaterial(Material material) {
        return mixTypeDao.findByMaterial(material);
    }

    public MixType createByName(String name) {
        Material mixTypeMaterial = new Material(name, 0, true, null);
        MixType mixType = new MixType(name, mixTypeMaterial);

        mixType = save(mixType);
        if (mixType == null) {
            return getByName(name);
        }

        return mixType;
    }
}
