package com.fp.armas.portal.validator;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.html.HtmlInputText;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

import com.fp.armas.portal.util.validadores.ValidadorIdentificacion;
import com.fp.armas.portal.web.PortalWebResources;

@FacesValidator(value="identificacionValidator")
public class IdentificacionValidator implements Validator {

	@Override
	public void validate(FacesContext context, UIComponent component, Object value)
			throws ValidatorException {
		try {
			int maxLength = ((HtmlInputText)component).getMaxlength();
			switch (maxLength) {
			case 10:
				if(!ValidadorIdentificacion.cedula(value.toString())){
					throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, PortalWebResources.getString("error.msg.cedula"), PortalWebResources.getString("error.msg.cedula")));
				}
				break;
			case 13:
				if(!ValidadorIdentificacion.ruc(value.toString())){
					throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, PortalWebResources.getString("error.msg.ruc"), PortalWebResources.getString("error.msg.ruc")));
				}

			default:
				break;
			}
		} catch (Throwable e) {
			throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), e.getMessage()));
		}
	}

}
