/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rjconsultores.ventaboletos.web.gui.controladores.catalogos;

import java.util.ArrayList;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.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.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zul.Button;

import com.rjconsultores.ventaboletos.entidad.Parada;
import com.rjconsultores.ventaboletos.entidad.RegionMetropolitana;
import com.rjconsultores.ventaboletos.service.ParadaService;
import com.rjconsultores.ventaboletos.service.RegionMetropolitanaService;
import com.rjconsultores.ventaboletos.web.utilerias.MyComboboxParada;
import com.rjconsultores.ventaboletos.web.utilerias.MyComboboxParadaCve;
import com.rjconsultores.ventaboletos.web.utilerias.MyGenericForwardComposer;
import com.rjconsultores.ventaboletos.web.utilerias.MyListbox;
import com.rjconsultores.ventaboletos.web.utilerias.MyTextbox;
import com.rjconsultores.ventaboletos.web.utilerias.render.RenderParadaRegionMetropolitana;

/**
 *
 * @author Administrador
 */
@Controller("editarRegionMetropolitanaController")
@Scope("prototype")
public class EditarRegionMetropolitanaController extends MyGenericForwardComposer {

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

	@Autowired
	private ParadaService paradaService;
	@Autowired
	private RegionMetropolitanaService regionMetropolitanaService;
	private RegionMetropolitana regionMetropolitana;
	private MyListbox regionMetropolitanaList;
	private Button btnApagar;
	private MyTextbox txtDesc;

	private MyComboboxParada cmbParada;
	private MyComboboxParadaCve cmbParadaCve;

	private MyListbox paradaList;
	private List<Parada> paradaListDeletada;

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

		regionMetropolitana = (RegionMetropolitana) Executions.getCurrent().getArg().get("regionMetropolitana");
		regionMetropolitanaList = (MyListbox) Executions.getCurrent().getArg().get("regionMetropolitanaList");

		if (regionMetropolitana.getRegionMetropolitanaId() == null) {
			btnApagar.setVisible(Boolean.FALSE);
		} else {

			List<Parada> paradas = paradaService.buscaParadaRegionMetropolitana(regionMetropolitana);
			paradaList.setData(paradas);
		}

		paradaList.setItemRenderer(new RenderParadaRegionMetropolitana());
		txtDesc.focus();
		txtDesc.setValue(regionMetropolitana.getDescRegion() == null ? "" : regionMetropolitana.getDescRegion());
	}

	public void onSelect$cmbParadaCve(Event ev) {
		if (cmbParadaCve.getSelectedItem() != null) {
			cmbParada.setComboItemByParada((Parada) cmbParadaCve.getSelectedItem().getValue());
		}
	}

	public void onSelect$cmbParada(Event ev) {
		if (cmbParada.getSelectedItem() != null)
			cmbParadaCve.setComboItemByParada((Parada) cmbParada.getSelectedItem().getValue());
	}

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

	public void salvarRegionMetropolitana() throws InterruptedException {

		regionMetropolitana.setDescRegion(txtDesc.getValue());

		try {

			if (paradaList == null || paradaList.getListModel() == null || paradaList.getListModel().getSize() == 0) {
				Messagebox.show(
						Labels.getLabel("editarRegionMetropolitanaController.MSG.vazio"),
						Labels.getLabel("editarRegionMetropolitanaController.window.title"),
						Messagebox.OK, Messagebox.ERROR);
				return;
			}

			if (regionMetropolitana.getRegionMetropolitanaId() == null) {
				regionMetropolitanaService.suscribir(regionMetropolitana);
				regionMetropolitanaList.addItemNovo(regionMetropolitana);
			} else {
				regionMetropolitanaService.actualizacion(regionMetropolitana);
				regionMetropolitanaList.updateItem(regionMetropolitana);
			}
			
			if(paradaList.getListData() != null) {
				for (Object parada : paradaList.getListData()) {
					paradaService.suscribirActualizar((Parada) parada);
				}
			}
			
			if (paradaListDeletada != null) {
				for (Parada parada : paradaListDeletada) {
					paradaService.suscribirActualizar(parada);
				}
			}

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

			closeWindow();
		} catch (Exception ex) {
			log.error("editarRegionMetropolitanaController: " , ex);
			Messagebox.show(
					Labels.getLabel("MSG.Error"),
					Labels.getLabel("editarRegionMetropolitanaController.window.title"),
					Messagebox.OK, Messagebox.ERROR);
		}
	}

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

		int resp = Messagebox.show(
				Labels.getLabel("editarRegionMetropolitanaController.MSG.borrarPergunta"),
				Labels.getLabel("editarRegionMetropolitanaController.window.title"),
				Messagebox.YES | Messagebox.NO, Messagebox.QUESTION);

		if (resp == Messagebox.YES) {

			regionMetropolitanaService.borrar(regionMetropolitana);

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

			regionMetropolitanaList.removeItem(regionMetropolitana);

			closeWindow();
		}
	}

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


		Parada parada = (Parada) cmbParada.getSelectedItem().getValue();
		parada.setRegionMetropolitana(regionMetropolitana);
		paradaList.addItemNovo(parada);

	}

	public void onClick$btnApagarParada(Event ev) throws InterruptedException {
		Parada parada = (Parada) paradaList.getSelected();
		parada.setRegionMetropolitana(null);

		if(paradaListDeletada == null) {
			paradaListDeletada = new ArrayList<Parada>();
		}
		paradaListDeletada.add(parada);

		paradaList.removeItem(parada);

	}

	public MyTextbox getTxtDesc() {
		return txtDesc;
	}

	public void setTxtDesc(MyTextbox txtDesc) {
		this.txtDesc = txtDesc;
	}

}
