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

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

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.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Comboitem;
import org.zkoss.zul.Intbox;
import org.zkoss.zul.Timebox;

import com.rjconsultores.ventaboletos.entidad.ClaseServicio;
import com.rjconsultores.ventaboletos.entidad.Corrida;
import com.rjconsultores.ventaboletos.entidad.CorridaCtrl;
import com.rjconsultores.ventaboletos.entidad.Parada;
import com.rjconsultores.ventaboletos.entidad.Pricing;
import com.rjconsultores.ventaboletos.entidad.PricingCorrida;
import com.rjconsultores.ventaboletos.service.ClaseServicioService;
import com.rjconsultores.ventaboletos.service.CorridaCtrlService;
import com.rjconsultores.ventaboletos.service.CorridaService;
import com.rjconsultores.ventaboletos.service.PricingCorridaService;
import com.rjconsultores.ventaboletos.utilerias.UsuarioLogado;
import com.rjconsultores.ventaboletos.web.utilerias.MyGenericForwardComposer;
import com.rjconsultores.ventaboletos.web.utilerias.MyListbox;
import com.rjconsultores.ventaboletos.web.utilerias.render.RenderEditarPricingCorrida;

/**
 * 
 * @author Rafius
 */
@Controller("editarPricingCorridaController")
@Scope("prototype")
public class EditarPricingCorridaController extends MyGenericForwardComposer {

	private static final long serialVersionUID = 1L;
	@Autowired
	private PricingCorridaService pricingCorridaService;
	@Autowired
	private CorridaCtrlService corridaCtrlService;
	@Autowired
	private CorridaService corridaService;
	@Autowired
	private ClaseServicioService claseServicioService;
	private List<ClaseServicio> lsClaseServicio;
	private Pricing pricing;
	private MyListbox pricingCorridaList;
	private MyListbox corridaList;
	private Intbox idCorrida;
	private Combobox cmbParadaOrigem;
	private Combobox cmbParadaDestino;
	private Combobox cmbTipoServicio;
	private Timebox timeInicio;
	private Timebox timeFin;
	private static Logger log = Logger.getLogger(EditarPricingCorridaController.class);

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		lsClaseServicio = claseServicioService.buscarTodosExceto(-1);

		super.doAfterCompose(comp);

		pricing = (Pricing) Executions.getCurrent().getArg().get("pricing");
		try{
			pricing.getPricingCorridaList();
		}catch(Exception e){
			pricing.setPricingCorridaList(new ArrayList<PricingCorrida>());
		}
		pricingCorridaList = (MyListbox) Executions.getCurrent().getArg().get("pricingCorridaList");

		corridaList.setItemRenderer(new RenderEditarPricingCorrida());
	}

	private void refreshLista() throws InterruptedException {

		Comboitem cbiOrigem = cmbParadaOrigem.getSelectedItem();
		Parada origem = null;
		if (cbiOrigem != null) {
			origem = (Parada) cbiOrigem.getValue();
		}

		Comboitem cbiDestino = cmbParadaDestino.getSelectedItem();
		Parada destino = null;
		if (cbiDestino != null) {
			destino = (Parada) cbiDestino.getValue();
		}

		Comboitem cbiTipoServicio = cmbTipoServicio.getSelectedItem();
		ClaseServicio claseServicio = null;
		if (cbiTipoServicio != null) {
			claseServicio = (ClaseServicio) cbiTipoServicio.getValue();
		}

		corridaList.setData(corridaService.buscarDistinct(origem, destino, timeInicio.getValue(), timeFin.getValue(), claseServicio, idCorrida.getValue()));

		if (corridaList.getData().length == 0) {

			Messagebox.show(Labels.getLabel("MSG.ningunRegistro"), Labels.getLabel("busquedaPricingController.window.title"), Messagebox.OK, Messagebox.INFORMATION);

		}
	}

	public void onClick$btnPesquisa(Event ev) throws InterruptedException {
		cmbParadaOrigem.clearErrorMessage();
		cmbParadaDestino.clearErrorMessage();
		cmbTipoServicio.clearErrorMessage();

		if (idCorrida.getValue() == null) {
			cmbParadaOrigem.setConstraint("no empty");
			cmbParadaDestino.setConstraint("no empty");
			cmbTipoServicio.setConstraint("no empty");

			cmbParadaOrigem.getValue();
			cmbParadaDestino.getValue();
			cmbTipoServicio.getValue();
		}

		refreshLista();
	}

	public void onClick$btnAdicionarCorrida(Event ev) throws Exception {
		List<PricingCorrida> lsPricingViagem = (pricing.getPricingCorridaList() == null) ? new ArrayList<PricingCorrida>() : pricing.getPricingCorridaList();
		List<Object> ls = corridaList.getSelectedsItens();
		if (!ls.isEmpty()) {
			for (Object o : ls) {
				Boolean achou = Boolean.FALSE;
				Corrida corrida = (Corrida) o;
				if (corrida.getCorridaCtrl() == null) {
					CorridaCtrl cc = corridaCtrlService.obtenerID(corrida.getId().getCorridaId());
					if (cc != null) {
						corrida.setCorridaCtrl(cc);
					}
				}
				for (PricingCorrida pc : lsPricingViagem) {
					if (pc.getCorridaCtrl().getCorridaId().equals(corrida.getCorridaCtrl().getCorridaId())) {
						achou = Boolean.TRUE;
					}
				}

				if (!achou) {
					PricingCorrida p = new PricingCorrida();
					p.setActivo(Pricing.ATIVO);

					p.setCorridaCtrl(corrida.getCorridaCtrl());

					p.setFecmodif(Calendar.getInstance().getTime());
					p.setPricing(pricing);
					p.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());

					lsPricingViagem.add(p);
				}
			}

			pricing.setPricingCorridaList(lsPricingViagem);
			pricingCorridaList.setData(lsPricingViagem);

			closeWindow();
		}
	}

	public MyListbox getPricingCorridaList() {
		return pricingCorridaList;
	}

	public void setPricingCorridaList(MyListbox pricingCorridaList) {
		this.pricingCorridaList = pricingCorridaList;
	}

	public PricingCorridaService getPricingCorridaService() {
		return pricingCorridaService;
	}

	public void setPricingCorridaService(PricingCorridaService pricingCorridaService) {
		this.pricingCorridaService = pricingCorridaService;
	}

	public CorridaCtrlService getCorridaCtrlService() {
		return corridaCtrlService;
	}

	public void setCorridaCtrlService(CorridaCtrlService corridaCtrlService) {
		this.corridaCtrlService = corridaCtrlService;
	}

	public Intbox getIdCorrida() {
		return idCorrida;
	}

	public void setIdCorrida(Intbox idCorrida) {
		this.idCorrida = idCorrida;
	}

	public Combobox getCmbParadaDestino() {
		return cmbParadaDestino;
	}

	public void setCmbParadaDestino(Combobox cmbParadaDestino) {
		this.cmbParadaDestino = cmbParadaDestino;
	}

	public Combobox getCmbParadaOrigem() {
		return cmbParadaOrigem;
	}

	public void setCmbParadaOrigem(Combobox cmbParadaOrigem) {
		this.cmbParadaOrigem = cmbParadaOrigem;
	}

	public MyListbox getCorridaList() {
		return corridaList;
	}

	public void setCorridaList(MyListbox corridaList) {
		this.corridaList = corridaList;
	}

	public List<ClaseServicio> getLsClaseServicio() {
		return lsClaseServicio;
	}

	public void setLsClaseServicio(List<ClaseServicio> lsClaseServicio) {
		this.lsClaseServicio = lsClaseServicio;
	}

	public Combobox getCmbTipoServicio() {
		return cmbTipoServicio;
	}

	public void setCmbTipoServicio(Combobox cmbTipoServicio) {
		this.cmbTipoServicio = cmbTipoServicio;
	}

	public Timebox getTimeFin() {
		return timeFin;
	}

	public void setTimeFin(Timebox timeFin) {
		this.timeFin = timeFin;
	}

	public Timebox getTimeInicio() {
		return timeInicio;
	}

	public void setTimeInicio(Timebox timeInicio) {
		this.timeInicio = timeInicio;
	}
}
