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.PricingEspecifico;
import com.rjconsultores.ventaboletos.entidad.PricingEspecificoCategoria;
import com.rjconsultores.ventaboletos.service.PricingEspecificoService;
import com.rjconsultores.ventaboletos.web.utilerias.MyGenericForwardComposer;
import com.rjconsultores.ventaboletos.web.utilerias.MyListbox;

@Controller("copiarPricingEspecificoController")
@Scope("prototype")
public class CopiarPricingEspecificoController extends MyGenericForwardComposer {

	private static final long serialVersionUID = 1L;

	@Autowired
	private PricingEspecificoService pricingEspecificoService;

	private Textbox nombrePricing;
	private PricingEspecifico pricingEspecifico;
	private List<PricingEspecificoCategoria> pricingCategoriaList;
	private MyListbox pricingList;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		pricingEspecifico = (PricingEspecifico) Executions.getCurrent().getArg().get("pricingEspecifico");
		pricingCategoriaList = (List<PricingEspecificoCategoria>) Executions.getCurrent().getArg().get("pricingCategoriaList");
		super.doAfterCompose(comp);
	}

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

			List<PricingEspecifico> lsPricing =	pricingEspecificoService.buscarPorNome(nombrePricing.getValue());

			if (lsPricing == null || 
					lsPricing.isEmpty()) {

				PricingEspecifico clone = pricingEspecificoService.clonarPricing(pricingEspecifico.getPricingespecificoId(), nombrePricing.getText(), pricingCategoriaList);

				if (clone != null) {

					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;
	}
}
