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

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
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.utilerias.ApplicationProperties;
import com.rjconsultores.ventaboletos.utilerias.CustomEnum;
import com.rjconsultores.ventaboletos.web.utilerias.MensagensUtils;
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 String CONTROLLER_WINDOW_TITLE = "copiarPricingController.window.title";

	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 (StringUtils.isBlank(nombrePricing.getText())) {
			MensagensUtils.showMessageExclamation("copiarPricingController.MSG.sem.nome",CONTROLLER_WINDOW_TITLE);
			return;
		}
		
		if (existePricingComMesmoNome()) {
			MensagensUtils.showMessageExclamation("copiarPricingController.MSG.Registro.Existe", CONTROLLER_WINDOW_TITLE);
			return;
		}
		
		Boolean clonou = pricingService.clonarPricing(pricing.getPricingId(), nombrePricing.getText(), 
				ApplicationProperties.getInstance().isCustomHabilitado(CustomEnum.USA_PADRAO_PRICING_TIPO_PASSAGEM_PET.getDescricao()));

		if (clonou) {
			closeWindow();
			MensagensUtils.showMessageInformation("copiarPricingController.MSG.suscribirOK", CONTROLLER_WINDOW_TITLE);
		} else {
			MensagensUtils.showMessageInformation("copiarPricingController.MSG.ERRO", CONTROLLER_WINDOW_TITLE);
		}

	}

	protected boolean existePricingComMesmoNome() {
		return pricingService.buscarPorNombre(nombrePricing.getText()).size() > 0;
	}

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

	public MyListbox getPricingList() {
		return pricingList;
	}

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