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

import java.util.Calendar;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.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.zk.ui.util.Clients;
import org.zkoss.zul.Combobox;

import com.rjconsultores.ventaboletos.entidad.GP_Pricing;
import com.rjconsultores.ventaboletos.entidad.GP_PricingImporte;
import com.rjconsultores.ventaboletos.entidad.Moneda;
import com.rjconsultores.ventaboletos.service.GP_PricingImporteService;
import com.rjconsultores.ventaboletos.service.MonedaService;
import com.rjconsultores.ventaboletos.utilerias.UsuarioLogado;
import com.rjconsultores.ventaboletos.web.utilerias.MyGenericForwardComposer;
import com.rjconsultores.ventaboletos.web.utilerias.MyListbox;
import com.rjconsultores.ventaboletos.web.utilerias.MyTextboxDecimal;

@Controller("editarGP_PricingImporteController")
@Scope("prototype")
public class EditarGP_PricingImporteController extends MyGenericForwardComposer {

	private static final long serialVersionUID = 1L;

	@Autowired
	private GP_PricingImporteService pricingImporteService;
	@Autowired
	private MonedaService monedaService;
	private List<Moneda> lsMoneda;
	private GP_PricingImporte pricingImporte;
	private MyListbox pricingImporteList;
	private Combobox cmbMoneda;
	private MyTextboxDecimal importe;
	private static Logger log = LogManager.getLogger(EditarGP_PricingImporteController.class);

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

		lsMoneda = monedaService.obtenerTodos();

		pricingImporte = (GP_PricingImporte) Executions.getCurrent().getArg().get("pricingImporte");
		pricingImporteList = (MyListbox) Executions.getCurrent().getArg().get("pricingImporteList");

		super.doAfterCompose(comp);
	}

	public void onClick$btnAdicionarImporte(Event ev) throws Exception {
		importe.getValue();
		cmbMoneda.getValue();

		try {
			if (pricingImporte.getMoneda() == null) {
				Messagebox.show(
						Labels.getLabel("MSG.Error.combobox"),
						Labels.getLabel("editarPricingController.windowImporte.title"),
						Messagebox.OK, Messagebox.EXCLAMATION);
			} else {
				List<GP_PricingImporte> lsPi = pricingImporteService.buscarMoneda(pricingImporte, pricingImporte.getMoneda());

				if (lsPi.isEmpty()) {
					if (pricingImporte.getPricingimporteId() != null) {
						pricingImporteService.actualizacion(pricingImporte);
						pricingImporteList.updateItem(pricingImporte);
					} else {
						pricingImporteService.suscribir(pricingImporte);
						pricingImporteList.addItemNovo(pricingImporte);
					}

					closeWindow();
				} else if (lsPi.size() == 1) {
					if (lsPi.get(0).getPricingimporteId().equals(pricingImporte.getPricingimporteId())) {
						pricingImporteService.actualizacion(pricingImporte);
						pricingImporteList.updateItem(pricingImporte);

						closeWindow();
					} else {
						Messagebox.show(
								Labels.getLabel("MSG.Registro.Existe"),
								Labels.getLabel("editarPricingController.windowImporte.title"),
								Messagebox.OK, Messagebox.EXCLAMATION);
					}
				} else {
					Messagebox.show(
							Labels.getLabel("MSG.Registro.Existe"),
							Labels.getLabel("editarPricingController.windowImporte.title"),
							Messagebox.OK, Messagebox.EXCLAMATION);
				}
			}
		} catch (Exception ex) {
			log.error("",ex);
			Messagebox.show(
					Labels.getLabel("MSG.Error"),
					Labels.getLabel("editarPricingController.windowImporte.title"),
					Messagebox.OK, Messagebox.EXCLAMATION);
		}
	}

	public void onClick$btnRemoverImporte(Event ev) {
		try {
			GP_PricingImporte pImporte = (GP_PricingImporte) pricingImporteList.getSelected();
			if (pImporte != null) {
				int resp = Messagebox.show(
						Labels.getLabel("editarPricingController.MSG.borrarPerguntaImporte"),
						Labels.getLabel("editarPricingController.windowImporte.title"),
						Messagebox.YES | Messagebox.NO, Messagebox.QUESTION);

				if (resp == Messagebox.YES) {
					pricingImporte.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
					pricingImporte.setFecmodif(Calendar.getInstance().getTime());
					pricingImporte.setActivo(GP_Pricing.INATIVO);

					pricingImporteList.removeItem(pricingImporte);
					pricingImporteService.borrar(pricingImporte);
					closeWindow();
				}
			} else {
				Messagebox.show(
						Labels.getLabel("editarPricingController.MSG.selectItem"),
						Labels.getLabel("editarPricingController.windowImporte.title"),
						Messagebox.OK, Messagebox.EXCLAMATION);
			}
		} catch (Exception ex) {
			log.error(ex);
			Clients.alert(
					Labels.getLabel("MSG.Error"),
					Labels.getLabel("editarPricingController.windowImporte.title"),
					Messagebox.EXCLAMATION);
		}

	}

	public GP_PricingImporte getPricingImporte() {
		return pricingImporte;
	}

	public void setPricingImporte(GP_PricingImporte pricingImporte) {
		this.pricingImporte = pricingImporte;
	}

	public MyListbox getPricingImporteList() {
		return pricingImporteList;
	}

	public void setPricingImporteList(MyListbox pricingImporteList) {
		this.pricingImporteList = pricingImporteList;
	}

	public GP_PricingImporteService getPricingImporteService() {
		return pricingImporteService;
	}

	public void setPricingImporteService(GP_PricingImporteService pricingImporteService) {
		this.pricingImporteService = pricingImporteService;
	}

	public List<Moneda> getLsMoneda() {
		return lsMoneda;
	}

	public void setLsMoneda(List<Moneda> lsMoneda) {
		this.lsMoneda = lsMoneda;
	}

	public MonedaService getMonedaService() {
		return monedaService;
	}

	public void setMonedaService(MonedaService monedaService) {
		this.monedaService = monedaService;
	}
}
