package com.rjconsultores.ventaboletos.web.gui.controladores.seguridad;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.util.resource.Labels;
import org.zkoss.zhtml.Messagebox;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zul.Textbox;

import com.rjconsultores.ventaboletos.entidad.Usuario;
import com.rjconsultores.ventaboletos.exception.BusinessException;
import com.rjconsultores.ventaboletos.service.UsuarioService;
import com.rjconsultores.ventaboletos.web.utilerias.MyGenericForwardComposer;

@Controller("cambiaContrasenaController")
@Scope("prototype")
public class CambiaContrasenaController extends MyGenericForwardComposer {

	private static final long serialVersionUID = 1L;
	private static Logger log = Logger.getLogger(EditarUsuarioController.class);

	@Autowired
	private UsuarioService usuarioService;

	private Textbox txtUser;
	private Textbox txtPassword;
	private Textbox txtNewPassword;
	private Textbox txtConfirmPassword;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
	}

	public void onClick$btnSalvar(Event ev) throws InterruptedException {

		try {
			if (StringUtils.isBlank(txtUser.getValue()) || StringUtils.isBlank(txtPassword.getValue())
					|| StringUtils.isBlank(txtNewPassword.getValue()) || StringUtils.isBlank(txtConfirmPassword.getValue())) {
				Messagebox.show(Labels.getLabel("winCambiaContrasena.erro.camposVazios"),
						Labels.getLabel("winCambiaContrasena.title"), Messagebox.OK, Messagebox.INFORMATION);
				return;
			}

			Usuario usuario = usuarioService.buscarPeloNomeSenha(txtUser.getValue(), txtPassword.getValue());
			if (usuario == null) {
				Messagebox.show(Labels.getLabel("winCambiaContrasena.erro.usuarioSenha"),
						Labels.getLabel("winCambiaContrasena.title"), Messagebox.OK, Messagebox.INFORMATION);
				return;
			}

			if (txtPassword.getValue().equals(txtNewPassword.getValue())) {
				Messagebox.show(Labels.getLabel("winCambiaContrasena.erro.senhasIguais"),
						Labels.getLabel("winCambiaContrasena.title"), Messagebox.OK, Messagebox.INFORMATION);
				return;
			}

			if (!txtNewPassword.getValue().equals(txtConfirmPassword.getValue())) {
				Messagebox.show(Labels.getLabel("winCambiaContrasena.erro.senhasDiferentes"),
						Labels.getLabel("winCambiaContrasena.title"), Messagebox.OK, Messagebox.INFORMATION);
				return;
			}

			usuarioService.cambiarContrasena(usuario, txtNewPassword.getValue());

			Messagebox.show(Labels.getLabel("winCambiaContrasena.MSG.suscribirOK"),
					Labels.getLabel("winCambiaContrasena.title"), Messagebox.OK, Messagebox.INFORMATION);

			closeWindow();

		} catch (BusinessException ex) {
			log.error(ex.getLocalizedMessage());
			Clients.alert(ex.getLocalizedMessage(), Labels.getLabel("winCambiaContrasena.title"), Messagebox.INFORMATION);
		}

	}

}
