package com.arrebol.taxiservicios.beans.topbar;

import com.arrebol.car.driver.security.CarDriverSecurity;
import com.arrebol.taxiservicios.beans.TaxiGenericBean;
import com.arrebol.taxiservicios.controller.top.PrivacyService;
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.inject.Inject;
import javax.inject.Named;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 *
 * @author Carlos Janitzio Zavala Lopez
 */
@Named
@RequestScoped
public class PrivacyBean extends TaxiGenericBean implements Serializable {

    /**
     * Update password.
     */
    public void updatePassword() {
        logger.info("updatePassword");

        try {
            if (validatePasswordRules(getPassword(), getConfirmPassword())) {
                if (privacyService.updatePassword(
                        getLoggedUser().getId(),
                        new CarDriverSecurity(APP, getPassword()).getPassword())) {
                    showMessage(FacesMessage.SEVERITY_INFO,
                            getBundlePropertyFile().getString("privacy"),
                            getBundlePropertyFile().getString("privacy.password.confirmed.details"));
                    showMessage(FacesMessage.SEVERITY_INFO,
                            getBundlePropertyFile().getString("privacy"),
                            getBundlePropertyFile().getString("privacy.password.restart"));
                } else {
                    showMessage(FacesMessage.SEVERITY_ERROR,
                            getBundlePropertyFile().getString("privacy"),
                            getBundlePropertyFile().getString("privacy.password.confirmed.details.error"));
                }
            } else {
                showMessage(FacesMessage.SEVERITY_WARN,
                        getBundlePropertyFile().getString("privacy"),
                        getBundlePropertyFile().getString("privacy.min.details"));
            }
        } catch (Exception e) {
            logger.error("selfUpdate: " + e);
            showMessage(FacesMessage.SEVERITY_FATAL,
                    getBundlePropertyFile().getString("privacy"),
                    getBundlePropertyFile().getString("privacy.password.exception.desc"));
        }
    }

    private static final long serialVersionUID = 875427975258155311L;

    final Logger logger = LogManager.getLogger(PrivacyBean.class);

    @Inject
    private PrivacyService privacyService;

    private String password;
    private String confirmPassword;

    @PostConstruct
    public void init() {
        logger.info("init, Privact " + getLoggedUser().getPerson().getId());

        loadBundlePropertyFile();
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getConfirmPassword() {
        return confirmPassword;
    }

    public void setConfirmPassword(String confirmPassword) {
        this.confirmPassword = confirmPassword;
    }

}
