/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.arrebol.taxiservicios.validators;

import com.arrebol.taxiservicios.controller.system.UserController;
import java.util.ResourceBundle;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.faces.application.FacesMessage;
import javax.faces.validator.ValidatorException;
import org.primefaces.context.RequestContext;

/**
 *
 * @author Picasso
 */
public abstract class GenericValidator {

    /**
     * Validate email format.
     *
     * @param email
     */
    protected synchronized void emailValidator(String email) throws ValidatorException {
        pattern = Pattern.compile(EMAIL_PATTERN);
        matcher = pattern.matcher(email);

        if (!matcher.matches()) {
            errorSeverity(getBundlePropertyFile().getString("validators.email"));
        }
    }

    /**
     * Validate password length and not empety or blank spaces.
     *
     * @param password
     */
    protected synchronized void passwordValidator(String password) throws ValidatorException {
        pattern = Pattern.compile(PASSWORD_PATTERN);
        matcher = pattern.matcher(password);

        if (!matcher.matches()) {
            errorSeverity(getBundlePropertyFile().getString("validator.password"));
        }
    }

    /**
     * Validate password length and not empety or blank spaces.
     *
     * @param password
     */
    protected synchronized void temporalPasswordValidator(String password) throws ValidatorException {
        if (password.trim().length() <= 5) {
            errorSeverity(getBundlePropertyFile().getString("validator.password"));
        }
    }

    /**
     * Validate string length and not empety or blank spaces.
     *
     * @param patterRule Can be MIN_LENGTH_3, NUMBER_MIN_LENGTH_3,
     * NUMBER_MIN_LENGTH_6.
     * @param lengthMin Possible values 3 and 6 minimum lngths.
     * @param value To be evaluate.
     * @param msg Message show to the user if validation failure.
     * @throws ValidatorException
     */
    protected synchronized void notEmptyValidator(final String patterRule, int lengthMin, String value, String msg) throws ValidatorException {
        pattern = Pattern.compile(patterRule);
        matcher = pattern.matcher(value);

        if (value.trim().length() >= lengthMin) {
            if (!matcher.matches()) {
                errorSeverity(getBundlePropertyFile().getString(msg));
            }
        } else {
            errorSeverity(getBundlePropertyFile().getString(msg));
        }
    }

    /**
     * Validate string length and not empety or blank spaces.
     *
     * @param lengthMin Possible values 3 and 6 minimum lngths.
     * @param value To be evaluate.
     * @param msg Message show to the user if validation failure.
     *
     * @throws ValidatorException If validation fail return message to show in
     * growl to the user.
     */
    protected synchronized void notEmptyValidator(int lengthMin, String value, String msg) throws ValidatorException {
        if (!(value.trim().length() >= lengthMin)) {
            errorSeverity(getBundlePropertyFile().getString(msg));
        }
    }

    /**
     * Validate taht a value is a float positive bigger that 0 number.
     *
     * @param value
     * @param msg
     * @throws ValidatorException
     */
    protected synchronized void floatNumber(String value, String msg) throws ValidatorException {
        try {
            float myFloatnumber = Float.parseFloat(value);

            if (0 >= myFloatnumber) {
                errorSeverity(getBundlePropertyFile().getString(msg));
            }
        } catch (Exception e) {
            errorSeverity(getBundlePropertyFile().getString(msg));
        }
    }

    /**
     * Validate taht a value is a double positive bigger that 0 number.
     *
     * @param value
     * @param msg
     * @throws ValidatorException
     */
    protected synchronized void doubleNumber(Object value, String msg) throws ValidatorException {
        try {
            double myFloatnumber = Double.parseDouble(value.toString());

            if (0 > myFloatnumber) {
                errorSeverity(getBundlePropertyFile().getString(msg));
            }
        } catch (Exception e) {
            errorSeverity(getBundlePropertyFile().getString(msg));
        }
    }

    /**
     * Raise a message with SEVERITY_ERROR.s
     *
     * @param message
     * @throws ValidatorException
     */
    protected void errorSeverity(String message) throws ValidatorException {
        FacesMessage msg = new FacesMessage(message);

        msg.setSeverity(FacesMessage.SEVERITY_ERROR);

        throw new ValidatorException(msg);
    }

    /**
     * Verify if user is already exist.
     *
     * @param userName
     * @param msg
     */
    protected void existUser(String userName, String msg) {
        UserController controller = new UserController();
        try {
            if (!controller.availableUserName(userName)) {
                errorSeverity(getBundlePropertyFile().getString(msg));
            }
        } catch (Exception e) {
            if (e instanceof ValidatorException) {
                throw e;
            } else {
                errorSeverity(getBundlePropertyFile().getString("validator.username.error"));
            }
        } finally {
            controller = null;
        }
    }

    protected RequestContext request;
    //\u00E0-\u00FC acentos PTODAR QUE ONDA CON ESTO
    
    protected final String EMAIL_PATTERN_MIN_LENGTH_3 = "([A-ZÑa-z0-9\\u0020\\u00E0-\\u00FC\\.\\,\\']{3,})|"
            + "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
    
    protected final String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\."
            + "[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*"
            + "(\\.[A-Za-z]{2,})$";
    protected final String PASSWORD_PATTERN = "[A-Za-z0-9][/[`~!@#$%^&*()_°¬|+\\-=?;:'\",.<>\\{\\}\\[\\]\\\\\\/]]{4,15}";
    protected final String MIN_LENGTH_3 = "[A-ZÑa-z0-9\\u0020\\u00E0-\\u00FC\\.\\,\\']{3,}";
    protected final String MIN_LENGTH_3_1 = "[A-ZÑa-z0-9\\u0020\\u00E0-\\u00FC\\.\\,\\']{3,}";
    protected final String MAX_LENGTH_20 = "[A-ZÑa-z0-9\\u0020\\u00E0-\\u00FC\\.\\,\\']{5,20}";
    protected final String MAX_LENGTH_50 = "[A-ZÑa-z0-9\\u0020\\u00E0-\\u00FC\\.\\,\\']{5,50}";
    protected final String NUMBER_MIN_LENGTH_3 = "[0-9]{3,}";
    protected final String NUMBER_MIN_LENGTH_6 = "[0-9]{6,}";
    protected final String NUMBER_MAX_LENGTH_12 = "[0-9]{5,12}";
    protected final String NUMBER_MAX_LENGTH_4 = "[0-9]{4}";
    protected final String TEXT_MAX_LENGTH_4 = "[A-Za-z0-9]{4}";
    protected final String CURP = "^([A-Z][AEIOUX][A-Z]{2}\\d{2}(?:0[1-9]|1[0-2])(?:0[1-9]|[12]\\d|3[01])[HM](?:AS|B[CS]|C[CLMSH]|D[FG]|G[TR]|HG|JC|M[CNS]|N[ETL]|OC|PL|Q[TR]|S[PLR]|T[CSL]|VZ|YN|ZS)[B-DF-HJ-NP-TV-Z]{3}[A-Z\\d])(\\d)$";
    protected final String RFC = "^([A-ZÑ&]{3,4}) ?(?:- ?)?(\\d{2}(?:0[1-9]|1[0-2])(?:0[1-9]|[12]\\d|3[01])) ?(?:- ?)?([A-Z\\d]{2})([A\\d])$";

    protected Pattern pattern;
    protected Matcher matcher;

    protected ResourceBundle bundlePropertyFile;
    private final String PROPERTY_FILE = "com.arrebol.taxiservicios.i18n.taxi_background";

    public RequestContext getRequest() {
        return request;
    }

    public void setRequest(RequestContext request) {
        this.request = request;
    }

    public ResourceBundle getBundlePropertyFile() {
        return bundlePropertyFile;
    }

    public synchronized void loadBundlePropertyFile() {
        this.bundlePropertyFile = ResourceBundle.getBundle(PROPERTY_FILE);
    }
}
