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

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.GP_PricingEspecifico;
import com.rjconsultores.ventaboletos.entidad.GP_PricingEspecificoCategoria;
import com.rjconsultores.ventaboletos.service.GP_PricingEspecificoService;
import com.rjconsultores.ventaboletos.service.GP_PricingHistoricoService;
import com.rjconsultores.ventaboletos.web.utilerias.MyGenericForwardComposer;
import com.rjconsultores.ventaboletos.web.utilerias.MyListbox;

@Controller("copiarGP_PricingEspecificoController")
@Scope("prototype")
public class CopiarGP_PricingEspecificoController extends MyGenericForwardComposer {

	private static final long serialVersionUID = 1L;

	@Autowired
	private GP_PricingEspecificoService pricingEspecificoService;
	@Autowired
	private GP_PricingHistoricoService pricingHistoricoService;

	private Textbox nombrePricing;
	private GP_PricingEspecifico pricingEspecifico;
	private List<GP_PricingEspecificoCategoria> pricingCategoriaList;
	private MyListbox pricingList;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		pricingEspecifico = (GP_PricingEspecifico) Executions.getCurrent().getArg().get("pricingEspecifico");
		pricingCategoriaList = (List<GP_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<GP_PricingEspecifico> lsPricing =	pricingEspecificoService.buscarPorNome(nombrePricing.getValue());

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

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

				if (clone != null) {

					pricingHistoricoService.compararESalvar(null, clone);
					
					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;
	}
}
