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

import java.util.List;

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

import com.rjconsultores.ventaboletos.entidad.Pricing;
import com.rjconsultores.ventaboletos.service.PricingService;
import com.rjconsultores.ventaboletos.web.utilerias.MyGenericForwardComposer;
import com.rjconsultores.ventaboletos.web.utilerias.MyListbox;

@Controller("copiarPricingController")
@Scope("prototype")
public class CopiarPricingController extends MyGenericForwardComposer {

	private static final long serialVersionUID = 1L;

	@Autowired
	private PricingService pricingService;

	private Textbox nombrePricing;
	private Pricing pricing;
	private MyListbox pricingList;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		pricing = (Pricing) Executions.getCurrent().getArg().get("pricing");

		super.doAfterCompose(comp);
	}

	public void onClick$btnSalvarCopia(Event ev) throws InterruptedException {
		if (!nombrePricing.getText().equals("") && nombrePricing.getText() != null) {

			List<Pricing> lsPricing =
					pricingService.buscarPorNombre(nombrePricing.getText());

			if (lsPricing.isEmpty()) {

				Boolean resp = pricingService.clonarPricing(pricing.getPricingId(), nombrePricing.getText());

				if (resp) {

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

				} else {
					Messagebox.show(
							Labels.getLabel("copiarPricingController.MSG.ERRO"),
							Labels.getLabel("copiarPricingController.window.title"),
							Messagebox.OK, Messagebox.INFORMATION);
				}

			} else {
				Messagebox.show(
						Labels.getLabel("copiarPricingController.MSG.Registro.Existe"),
						Labels.getLabel("copiarPricingController.window.title"),
						Messagebox.OK, Messagebox.EXCLAMATION);
			}
		} else {
			Messagebox.show(
					Labels.getLabel("copiarPricingController.MSG.sem.nome"),
					Labels.getLabel("copiarPricingController.window.title"),
					Messagebox.OK, Messagebox.EXCLAMATION);
		}
	}

	public void onClose$winCopiarPricing(Event ev) {
		closeWindow();
	}

	public MyListbox getPricingList() {
		return pricingList;
	}

	public void setPricingList(MyListbox pricingList) {
		this.pricingList = pricingList;
	}
}
