package com.rjconsultores.ventaboletos.web.utilerias.menu.item.esquemaoperacional;

import java.util.List;

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.Executions;
import org.zkoss.zk.ui.event.Event;

import com.rjconsultores.ventaboletos.entidad.AliasServico;
import com.rjconsultores.ventaboletos.entidad.Ruta;
import com.rjconsultores.ventaboletos.exception.BusinessException;
import com.rjconsultores.ventaboletos.service.AliasServicoService;
import com.rjconsultores.ventaboletos.service.RutaService;
import com.rjconsultores.ventaboletos.web.utilerias.MyGenericForwardComposer;
import com.rjconsultores.ventaboletos.web.utilerias.MyListbox;

@Controller("editarAliasServicoController")
@Scope("prototype")
public class EditarAliasServicoController extends MyGenericForwardComposer {

	private static final long serialVersionUID = 1L;
	@Autowired
	private AliasServicoService aliasServicoService;
	@Autowired
	private RutaService rutaService;
	private List<Ruta> lsRutas;
	private MyListbox aliasServicoList;
	private AliasServico aliasServico;

	public AliasServico getAliasServico() {
		return aliasServico;
	}

	public void setAliasServico(AliasServico aliasServico) {
		this.aliasServico = aliasServico;
	}

	public List<Ruta> getLsRutas() {
		return lsRutas;
	}

	public void setLsRutas(List<Ruta> lsRutas) {
		this.lsRutas = lsRutas;
	}

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		lsRutas = rutaService.obtenerTodos();
		super.doAfterCompose(comp);

		aliasServico = (AliasServico) Executions.getCurrent().getArg().get("aliasServico");
		aliasServicoList = (MyListbox) Executions.getCurrent().getArg().get("aliasServicoList");
	}

	public void onClick$btnSalvar(Event ev) throws InterruptedException {
		
		try {
			
			aliasServicoService.suscribirActualizar(aliasServico);
			aliasServicoList.updateItem(aliasServico);

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

			closeWindow();
		} catch (BusinessException ex) {
			Messagebox.show(
					ex.getMessage(),
					Labels.getLabel("editarAliasServicoController.window.title"),
					Messagebox.OK, Messagebox.ERROR);
		} catch (Exception ex) {
			Messagebox.show(
						Labels.getLabel("MSG.Error"),
						Labels.getLabel("editarAliasServicoController.window.title"),
						Messagebox.OK, Messagebox.ERROR);
		}
	}

	public void onClick$btnApagar(Event ev) throws InterruptedException {
		Integer resp = Messagebox.show(
					Labels.getLabel("editarAliasServicoController.MSG.borrarPergunta"),
					Labels.getLabel("editarAliasServicoController.window.title"),
					Messagebox.YES | Messagebox.NO, Messagebox.QUESTION);

		if (resp == Messagebox.YES) {

			aliasServicoService.borrar(aliasServico);

			Messagebox.show(
						Labels.getLabel("editarAliasServicoController.MSG.borrarOK"),
						Labels.getLabel("editarAliasServicoController.window.title"),
						Messagebox.OK, Messagebox.INFORMATION);

			aliasServicoList.removeItem(aliasServico);

			closeWindow();
		}
	}
}
