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

import java.util.HashMap;
import java.util.List;
import java.util.Map;

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.zk.ui.event.EventListener;
import org.zkoss.zul.Button;
import org.zkoss.zul.Datebox;
import org.zkoss.zul.Longbox;
import org.zkoss.zul.Paging;

import com.rjconsultores.ventaboletos.entidad.Voucher;
import com.rjconsultores.ventaboletos.service.GrupoContratoService;
import com.rjconsultores.ventaboletos.service.ParadaService;
import com.rjconsultores.ventaboletos.service.TransportadoraService;
import com.rjconsultores.ventaboletos.service.VoucherService;
import com.rjconsultores.ventaboletos.web.utilerias.MyGenericForwardComposer;
import com.rjconsultores.ventaboletos.web.utilerias.MyListbox;
import com.rjconsultores.ventaboletos.web.utilerias.render.RenderFaturarVoucher;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Controller("faturarVoucherController")
@Scope("prototype")
public class FaturarVoucherController extends MyGenericForwardComposer {

	private static Logger log = LogManager.getLogger(FaturarVoucherController.class); 
	private static final long serialVersionUID = 5214942034025004656L;
	private static final String TITULO = "faturarVoucherController.window.title";
	
	@Autowired
	private VoucherService voucherService;
	
	@Autowired
	private ParadaService paradaService;
	
	@Autowired
	private TransportadoraService transportadoraService;
	
	@Autowired
	private GrupoContratoService grupoService;
	
	private MyListbox voucherList;
	private Paging pagingFaturar;
	private Longbox txtFatura;
	private Datebox datCorte;
	private Button btnFaturar;
	private Button btnFaturarFim;

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

		List<Voucher> listaVoucher = (List<Voucher>) Executions.getCurrent().getArg().get("listaVoucher");
		voucherList.setData(listaVoucher);
		voucherList.setItemRenderer(new RenderFaturarVoucher());
		voucherList.addEventListener("onDoubleClick", new EventListener() {

			@Override
			public void onEvent(Event event) throws Exception {
				Voucher cc = (Voucher) voucherList.getSelected();
				verVoucher(cc);
			}
		});
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void verVoucher(Voucher gc) {
		if (gc == null) {
			return;
		}

		Map args = new HashMap();
		args.put("voucher", gc);
		args.put("voucherList", voucherList);

		openWindow("/gui/configuraciones_comerciales/negcorporativos/editarVoucher.zul",
				Labels.getLabel("editarVoucherController.window.title"), args, MODAL);
	}
	
	public void onClick$btnSalvar(Event ev) throws InterruptedException {
		onClick$btnFaturar(ev);
	}	
	
	public void onClick$btnFaturar(Event ev) throws InterruptedException {
		try {
			if(validaCampos()) {
				executaFaturamento();
			}		
		} catch (Exception ex) {
			log.error(ex.getMessage());
			Messagebox.show(
						Labels.getLabel("MSG.Error"),
						Labels.getLabel(TITULO),
						Messagebox.OK, Messagebox.ERROR);
		}
		
	}
	
	@SuppressWarnings("unchecked")
	private void executaFaturamento() {
		
		voucherService.faturar( voucherList.getListData(), 
								txtFatura.getValue(), 
								datCorte.getValue() );

		try {
			Messagebox.show(
					Labels.getLabel("MSG.suscribirOK"),
					Labels.getLabel(TITULO),
					Messagebox.OK, Messagebox.INFORMATION);
		} catch (InterruptedException e) {
			log.error(e);
		}

		closeWindow();	
		
	}

	private boolean validaCampos() throws InterruptedException {
		
		if ( txtFatura.getValue() != null && datCorte.getValue() != null ){
			int resp = Messagebox.show(
					Labels.getLabel("faturarVoucherController.MSG.faturaCorteDefault"),
	                Labels.getLabel(TITULO),
					Messagebox.YES | Messagebox.NO, Messagebox.QUESTION);

			return resp == Messagebox.YES;
		}else if ( txtFatura.getValue() != null ){
			int resp = Messagebox.show(
					Labels.getLabel("faturarVoucherController.MSG.faturaDefault"),
	                Labels.getLabel(TITULO),
					Messagebox.YES | Messagebox.NO, Messagebox.QUESTION);

			return resp == Messagebox.YES;
		}else if ( datCorte.getValue() != null ){
			int resp = Messagebox.show(
					Labels.getLabel("faturarVoucherController.MSG.corteDefault"),
	                Labels.getLabel(TITULO),
					Messagebox.YES | Messagebox.NO, Messagebox.QUESTION);

			return resp == Messagebox.YES;
		}else {
			int resp = Messagebox.show(
					Labels.getLabel("faturarVoucherController.MSG.faturaCorteVazio"),
	                Labels.getLabel(TITULO),
					Messagebox.YES | Messagebox.NO, Messagebox.QUESTION);

			return resp == Messagebox.YES;
		}
		
	}	
		
}
