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

import org.zkoss.zk.ui.api.HtmlBasedComponent;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Checkbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.ListitemRenderer;

import com.rjconsultores.ventaboletos.vo.parada.ParadaVOConexionRuta;

/**
 * 
 * @author wallace
 */
public class RenderGeracaoConexionPorRuta implements ListitemRenderer {
	
	private static int grupo = 0;
	private boolean changeColor = true;
	private static int origemConexao = 0;
	private static int destinoConexao = 0;
	
	private String style1 = "Font-weight:bold;background-color: #DCDCDC";
	private String style2 = "Font-weight:bold;background-color: #C0C0C0";
	
	private void setStyle(HtmlBasedComponent lc) {
		lc.setStyle(changeColor ? style1 : style2);
	}
	
	public void render(Listitem lstm, Object o) throws Exception {
		
		ParadaVOConexionRuta conexion = (ParadaVOConexionRuta) o;
		
		Boolean isCtrlDiferente = grupo != conexion.getGrupo() || (origemConexao != conexion.getParadaOrigemTrecho().getParadaId() || destinoConexao != conexion.getParadaDestinoTrecho().getParadaId());
		
		if (isCtrlDiferente) {
			grupo = conexion.getGrupo();
			origemConexao = conexion.getParadaOrigemTrecho().getParadaId();
			destinoConexao = conexion.getParadaDestinoTrecho().getParadaId();
			changeColor = !changeColor;
		}
		
		Listcell lc = new Listcell(conexion.getParadaOrigemTrecho().getDescparada());
		setStyle(lstm);
		lc.setParent(lstm);
		
		lc = new Listcell(conexion.getParadaDestinoTrecho().getDescparada());
		lc.setParent(lstm);
		
		lc = new Listcell(conexion.getParadaOrigem().getDescparada());
		lc.setParent(lstm);

		lc = new Listcell(conexion.getParadaDestino().getDescparada());
		lc.setParent(lstm);

		lc = new Listcell(conexion.getNumRutaSentido());
		lc.setParent(lstm);
		
		lc = new Listcell(String.valueOf(conexion.getGrupo()));
		lc.setParent(lstm);
		
		lc = new Listcell(String.valueOf(conexion.getSecuencia()));
		lc.setParent(lstm);

		if(isCtrlDiferente) {
			Checkbox ckbindPricing = new Checkbox();
			ckbindPricing.setChecked(conexion.getConexionCtrl().getIndPricing());
			
			ckbindPricing.addEventListener("onClick", new EventListener() {
				@Override
				public void onEvent(Event event) throws Exception {
					conexion.getConexionCtrl().setIndPricing(ckbindPricing.isChecked());
				}
			});
			
			lc = new Listcell();
			ckbindPricing.setParent(lc);
			lc.setParent(lstm);
		} else {
			lc = new Listcell(" ");
			lc.setParent(lstm);
		}
		
		lstm.setValue(conexion);
	}
}
