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.zul.Combobox;

import com.rjconsultores.ventaboletos.entidad.GP_Pricing;
import com.rjconsultores.ventaboletos.entidad.GP_PricingMarca;
import com.rjconsultores.ventaboletos.entidad.Marca;
import com.rjconsultores.ventaboletos.service.GP_PricingMarcaService;
import com.rjconsultores.ventaboletos.service.MarcaService;
import com.rjconsultores.ventaboletos.utilerias.UsuarioLogado;
import com.rjconsultores.ventaboletos.web.utilerias.MyGenericForwardComposer;
import com.rjconsultores.ventaboletos.web.utilerias.MyListbox;

@Controller("editarGP_PricingMarcaController")
@Scope("prototype")
public class EditarGP_PricingMarcaController extends MyGenericForwardComposer {

	private static final long serialVersionUID = 1L;
	@Autowired
	private GP_PricingMarcaService pricingMarcaService;
	@Autowired
	private MarcaService marcaService;
	private List<Marca> lsMarca;
	private GP_PricingMarca pricingMarca;
	private MyListbox pricingMarcaList;
	private Combobox cmbMarca;
	private static Logger log = LogManager.getLogger(EditarGP_PricingMarcaController.class);

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		lsMarca = marcaService.buscarMarcaPorEmpresa(UsuarioLogado.getUsuarioLogado().getEmpresa());

		GP_PricingMarca pm = (GP_PricingMarca) Executions.getCurrent().getArg().get("pricingMarca");
		pricingMarca = (pm.getUsuarioId() == null) ? pm : pricingMarcaService.obtenerID(pm.getUsuarioId());
		pricingMarcaList = (MyListbox) Executions.getCurrent().getArg().get("pricingMarcaList");

		super.doAfterCompose(comp);

		cmbMarca.focus();
	}

	public void onClick$btnAdicionarMarca(Event ev) throws Exception {
		cmbMarca.getValue();

		try {
			if (pricingMarca.getMarca() == null) {
				Messagebox.show(
						Labels.getLabel("MSG.Error.combobox"),
						Labels.getLabel("editarPricingController.windowMarca.title"),
						Messagebox.OK, Messagebox.EXCLAMATION);
			} else {
				Boolean existe = pricingMarcaService.obtenerPricingMarca(pricingMarca.getPricing(), (Marca) cmbMarca.getSelectedItem().getValue());
				if (existe) {
					Messagebox.show(
							Labels.getLabel("MSG.Registro.Existe"),
							Labels.getLabel("editarPricingController.windowMarca.title"),
							Messagebox.OK, Messagebox.EXCLAMATION);
				} else {
					if (pricingMarca.getPricingmarcaId() != null) {
						pricingMarcaService.actualizacion(pricingMarca);
						pricingMarcaList.updateItem(pricingMarca);
					} else {
						pricingMarcaService.suscribir(pricingMarca);
						pricingMarcaList.addItemNovo(pricingMarca);
					}
					closeWindow();
				}
			}
		} catch (Exception ex) {
			log.error(ex);
		}
	}

	public void onClick$btnRemoverMarca(Event ev) {
		try {

			int resp = Messagebox.show(
					Labels.getLabel("editarPricingController.MSG.borrarPerguntaMarca"),
					Labels.getLabel("editarPricingController.windowMarca.title"),
					Messagebox.YES | Messagebox.NO, Messagebox.QUESTION);

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

				pricingMarcaList.removeItem(pricingMarca);
				pricingMarcaService.borrar(pricingMarca);
				closeWindow();
			}
		} catch (Exception ex) {
			log.error(ex);
		}

	}

	public GP_PricingMarca getPricingMarca() {
		return pricingMarca;
	}

	public void setPricingMarca(GP_PricingMarca pricingMarca) {
		this.pricingMarca = pricingMarca;
	}

	public MyListbox getPricingMarcaList() {
		return pricingMarcaList;
	}

	public void setPricingMarcaList(MyListbox pricingMarcaList) {
		this.pricingMarcaList = pricingMarcaList;
	}

	public GP_PricingMarcaService getPricingMarcaService() {
		return pricingMarcaService;
	}

	public void setPricingMarcaService(GP_PricingMarcaService pricingMarcaService) {
		this.pricingMarcaService = pricingMarcaService;
	}

	public List<Marca> getLsMarca() {
		return lsMarca;
	}

	public void setLsMarca(List<Marca> lsMarca) {
		this.lsMarca = lsMarca;
	}

	public MarcaService getMarcaService() {
		return marcaService;
	}

	public void setMarcaService(MarcaService marcaService) {
		this.marcaService = marcaService;
	}
}
