/*
 * Arrebol Consultancy copyright.
 * 
 * This code belongs to Arrebol Consultancy
 * its use, redistribution or modification are prohibited 
 * without written authorization from Arrebol Consultancy.
 */
package com.arrebol.taxiservicios.convert;

import com.arrebol.taxiservicios.model.enums.FeeType;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;

/**
 *
 * @author Carlos Janitzio Zavala Lopez <janitzio.zavala@arrebol.com.mx>
 */
@FacesConverter("feeTypeConverter")
public class FeeTypeConverter implements Converter<FeeType> {

    @Override
    public FeeType getAsObject(FacesContext fc, UIComponent uic, String type) {
        try {
            for (FeeType feeType : FeeType.values()) {
                if (feeType.getValue().equals(type)) {
                    return feeType;
                }
            }
            throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error en búsqueda", "No se encontro el valor del tipo de multa"));
        } catch (Exception e) {
            throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error en búsqueda", "No se pudo obtener los datos del tipo de multa"));
        }
    }

    @Override
    public String getAsString(FacesContext fc, UIComponent uic, FeeType feeType) {
        if (feeType != null) {
            return feeType.getValue();
        } else {
            return null;
        }
    }

}
