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

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.Radio;

import com.rjconsultores.ventaboletos.entidad.FormaPago;
import com.rjconsultores.ventaboletos.entidad.Pricing;
import com.rjconsultores.ventaboletos.entidad.PricingFormapago;
import com.rjconsultores.ventaboletos.service.FormaPagoService;
import com.rjconsultores.ventaboletos.service.PricingFormapagoService;
import com.rjconsultores.ventaboletos.web.utilerias.MyGenericForwardComposer;
import com.rjconsultores.ventaboletos.web.utilerias.MyListbox;

@Controller("editarPricingFormapagoController")
@Scope("prototype")
public class EditarPricingFormapagoController extends MyGenericForwardComposer {

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

	@Autowired
	private FormaPagoService formaPagoService;

	@Autowired
	private PricingFormapagoService pricingFormapagoService;

	private List<FormaPago> lsFormaPago;
	private PricingFormapago pricingFormapago;
	private MyListbox pricingFormapagoList;

	private Radio si;
	private Radio no;

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

		this.setPricingFormapago((PricingFormapago) Executions.getCurrent().getArg().get("pricingFormapago"));
		this.setPricingFormapagoList((MyListbox) Executions.getCurrent().getArg().get("pricingFormapagoList"));
		this.setLsFormaPago(formaPagoService.obtenerTodos());

		super.doAfterCompose(comp);

		if (pricingFormapago.getPricingformapagoId() != null) {
			if (pricingFormapago.getIndexcepcion()) {
				si.setChecked(true);
			} else {
				no.setChecked(true);
			}
		} else {
			no.setChecked(true);
		}
	}

	public void onClick$btnAdicionarFormaPago(Event ev) throws InterruptedException {
		try {
			if (pricingFormapago.getFormaPago() == null) {
				Messagebox.show(
						Labels.getLabel("MSG.Error.combobox"),
						Labels.getLabel("editarPricingController.windowFormaPago.title"),
						Messagebox.OK, Messagebox.EXCLAMATION);
			} else {

				if (no.isChecked()) {
					pricingFormapago.setIndexcepcion(Boolean.FALSE);
				} else if (si.isChecked()) {
					pricingFormapago.setIndexcepcion(Boolean.TRUE);
				}

				if (pricingFormapago.getPricingformapagoId() != null) {
					pricingFormapagoService.actualizacion(pricingFormapago);
					pricingFormapagoList.updateItem(pricingFormapago);

					pricingFormapagoList.setData(pricingFormapago.getPricing().getPricingFormapagoList());

				} else {
					pricingFormapagoService.suscribir(pricingFormapago);
					pricingFormapagoList.addItem(pricingFormapago);
				}
				closeWindow();

			}
		} catch (Exception ex) {
			log.error(ex);
		}
	}

	public List<FormaPago> getLsFormaPago() {
		return lsFormaPago;
	}

	public void setLsFormaPago(List<FormaPago> lsFormaPago) {
		this.lsFormaPago = lsFormaPago;
	}

	public PricingFormapago getPricingFormapago() {
		return pricingFormapago;
	}

	public void setPricingFormapago(PricingFormapago pricingFormapago) {
		this.pricingFormapago = pricingFormapago;
	}

	public MyListbox getPricingFormapagoList() {
		return pricingFormapagoList;
	}

	public void setPricingFormapagoList(MyListbox pricingFormapagoList) {
		this.pricingFormapagoList = pricingFormapagoList;
	}

}
