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

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.event.EventListener;
import org.zkoss.zk.ui.event.InputEvent;
import org.zkoss.zk.ui.event.KeyEvent;
import org.zkoss.zul.Textbox;

import com.rjconsultores.ventaboletos.service.ConferenciaComissaoService;
import com.rjconsultores.ventaboletos.vo.comissao.EtiquetaMalote;
import com.rjconsultores.ventaboletos.web.utilerias.MyGenericForwardComposer;

@Scope("prototype")
@Controller("recebimentoMaloteController")
public class RecebimentoMaloteController extends MyGenericForwardComposer {
	
	private static Logger log = Logger.getLogger(RecebimentoMaloteController.class);

	private static final long serialVersionUID = 1L;
	
	@Autowired
	private ConferenciaComissaoService conferenciaComissaoService;

	private EtiquetaMalote etiquetaMalote;
	private Textbox txtCodigoBarras;
	private Textbox txtEmpresa;
	private Textbox txtPuntoVenta;
	private Textbox txtDatasMalote;
	private Textbox txtNumeroMalote;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		
		txtCodigoBarras.setCtrlKeys("#f10#f12#f3");
		txtCodigoBarras.addEventListener("onChanging", new EventListener() {

			@Override
			public void onEvent(Event event) throws Exception {
				if(event instanceof InputEvent) {
					InputEvent inputEvent = (InputEvent) event;
					if(inputEvent.getValue().length() == 35) {
						pesquisa(inputEvent.getValue());
					}
				}
			}
			
		});
		txtCodigoBarras.addEventListener("onCtrlKey", new EventListener() {
			
			@Override
			public void onEvent(Event event) throws Exception {
				if(event instanceof KeyEvent) {
					KeyEvent keyEvent = (KeyEvent) event;
					if(keyEvent.getKeyCode() == KeyEvent.F10) {
						onClick$btnPesquisa(event);
					} else if(keyEvent.getKeyCode() == KeyEvent.F12) {
						onClick$btnConfirmar(event);
					} else if(keyEvent.getKeyCode() == KeyEvent.F3) {
						onClick$btnLimpar(event);
					}
				}
			}
		});
		
		txtCodigoBarras.focus();
	}

	public void onClick$btnPesquisa(Event ev) throws InterruptedException {
		txtCodigoBarras.getValue();
		pesquisa(txtCodigoBarras.getValue());
	}
	
	private void pesquisa(String codigoBarras) throws InterruptedException {
		try {
			if(verificarCampos(codigoBarras)) {
				etiquetaMalote = conferenciaComissaoService.decodificarEtiquetaMalote(codigoBarras);
				txtEmpresa.setValue(etiquetaMalote.getNombempresa());
				txtPuntoVenta.setValue(etiquetaMalote.getNombpuntoventa());
				txtDatasMalote.setValue(etiquetaMalote.getDatasMalote());
				txtNumeroMalote.setValue(etiquetaMalote.getNumeroMalote());
			}
			
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			Messagebox.show(Labels.getLabel("MSG.Error"),
					Labels.getLabel("recebimentoMaloteController.window.title"),
					Messagebox.OK,Messagebox.ERROR);			
		}
		txtCodigoBarras.focus();
	}

	private boolean verificarCampos(String codigoBarras) throws InterruptedException {
		if(codigoBarras == null || codigoBarras.length() != 35) {
			Messagebox.show(Labels.getLabel("recebimentoMaloteController.msg.erro.codigoBarrasInvalido"),
					Labels.getLabel("recebimentoMaloteController.window.title"),
					Messagebox.OK,Messagebox.ERROR);
			return false;
		}
		return true;
	}

	public void onClick$btnLimpar(Event ev) {
		try {
			limpar();
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
	}
	
	private void limpar() {
		etiquetaMalote = new EtiquetaMalote();
		txtCodigoBarras.setValue("");
		txtDatasMalote.setValue("");
		txtEmpresa.setValue("");
		txtPuntoVenta.setValue("");
		txtNumeroMalote.setValue("");
		txtCodigoBarras.focus();
	}

	public void onClick$btnConfirmar(Event ev) throws InterruptedException {
		try {
			if(getEtiquetaMalote() != null) {
				conferenciaComissaoService.confirmarChegadaMalote(getEtiquetaMalote());
				Messagebox.show(Labels.getLabel("recebimentoMaloteController.msg.info.maloteRecebido"),
						Labels.getLabel("recebimentoMaloteController.window.title"),
						Messagebox.OK,Messagebox.INFORMATION);
				limpar();
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			Messagebox.show(Labels.getLabel("MSG.Error"),
					Labels.getLabel("recebimentoMaloteController.window.title"),
					Messagebox.OK,Messagebox.ERROR);			
		}
		
		txtCodigoBarras.focus();
	}

	public EtiquetaMalote getEtiquetaMalote() {
		return etiquetaMalote;
	}

	public void setEtiquetaMalote(EtiquetaMalote etiquetaMalote) {
		this.etiquetaMalote = etiquetaMalote;
	}
	
}
