/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rjconsultores.ventaboletos.web.gui.controladores.catalogos;

import java.util.Arrays;
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.Button;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.api.Comboitem;

import com.rjconsultores.ventaboletos.entidad.Custom;
import com.rjconsultores.ventaboletos.enums.CustomTipo;
import com.rjconsultores.ventaboletos.enums.SistemaEnum;
import com.rjconsultores.ventaboletos.service.CustomService;
import com.rjconsultores.ventaboletos.utilerias.UsuarioLogado;
import com.rjconsultores.ventaboletos.web.utilerias.MyGenericForwardComposer;
import com.rjconsultores.ventaboletos.web.utilerias.MyListbox;

/**
 * 
 * @author Lucas
 */
@Controller("editarCustomController")
@Scope("prototype")
public class EditarCustomController extends MyGenericForwardComposer {

	private static final long serialVersionUID = 1L;
	@Autowired
	private CustomService customService;
	private Custom custom;
	private MyListbox customList;
	private Button btnApagar;
	private Textbox txtChave;
	private Textbox txtValor;
	private Combobox cmbSistema;
	private Combobox cmbTipo;
	private List<SistemaEnum> lsSistemaEnum;
	private List<CustomTipo> lsTipoEnum;

	private static Logger log = LogManager.getLogger(EditarCustomController.class);

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

		custom = (Custom) Executions.getCurrent().getArg().get("custom");
		customList = (MyListbox) Executions.getCurrent().getArg().get("customList");

		lsSistemaEnum = Arrays.asList(SistemaEnum.values());
		lsTipoEnum = Arrays.asList(CustomTipo.values());
		
		if (custom.getCustomId() == null) {
			btnApagar.setVisible(Boolean.FALSE);
		}else{
			txtChave.setDisabled(Boolean.TRUE);
			cmbSistema.setDisabled(Boolean.TRUE);
			cmbTipo.setDisabled(Boolean.TRUE);
			
			cmbSistema.setValue(SistemaEnum.getSistema(custom.getSistema()).toString());
			if (custom.getTipo() != null) {
				cmbTipo.setValue(CustomTipo.getCustomTipo(custom.getTipo()).toString());
			}
			
		}

		txtValor.focus();
	}

	public void onClick$btnSalvar(Event ev) throws InterruptedException {
		txtValor.getValue();

		try {
			
			Comboitem cbSistema = cmbSistema.getSelectedItem();
			if (cbSistema != null) {
				SistemaEnum sistemaEnum = (SistemaEnum) cbSistema.getValue();
				custom.setSistema(sistemaEnum.getValor());
			}
			
			Comboitem cbTipoCustom = cmbTipo.getSelectedItem();
			if(cbTipoCustom != null) {
				CustomTipo customTipo = (CustomTipo) cbTipoCustom.getValue();
				custom.setTipo(customTipo.getValor());
			}
			custom.setOrdem(0);
			custom.setActivo(Boolean.TRUE);
			custom.setFecmodif(Calendar.getInstance().getTime());
			custom.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());

			Custom tempCustom = customService.buscar(custom.getChave());

			if (custom.getCustomId() == null && tempCustom != null) {
				Messagebox.show(
						Labels.getLabel("MSG.Registro.Existe"),
						Labels.getLabel("customController.window.title"),
						Messagebox.OK, Messagebox.EXCLAMATION);
			} else {
				if (custom.getCustomId() == null) {
					customService.suscribir(custom);
					customList.addItem(custom);
				} else {
					customService.actualizacion(custom);
					customList.updateItem(custom);
				}

				Messagebox.show(
						Labels.getLabel("customController.MSG.suscribirOK"),
						Labels.getLabel("customController.window.title"),
						Messagebox.OK, Messagebox.INFORMATION);

				closeWindow();
			}
		} catch (Exception ex) {
			log.error("", ex);
			Messagebox.show(
					Labels.getLabel("MSG.Error"),
					Labels.getLabel("customController.window.title"),
					Messagebox.OK, Messagebox.ERROR);
		}
	}

	public void onClick$btnApagar(Event ev) throws InterruptedException {

		int resp = Messagebox.show(
				Labels.getLabel("customController.MSG.borrarPergunta"),
				Labels.getLabel("customController.window.title"),
				Messagebox.YES | Messagebox.NO, Messagebox.QUESTION);

		if (resp == Messagebox.YES) {

			customService.borrar(custom);

			Messagebox.show(
					Labels.getLabel("customController.MSG.borrarOK"),
					Labels.getLabel("customController.window.title"),
					Messagebox.OK, Messagebox.INFORMATION);

			customList.removeItem(custom);

			closeWindow();
		}
	}

	public CustomService getCustomService() {
		return customService;
	}

	public void setCustomService(CustomService customService) {
		this.customService = customService;
	}

	public Custom getCustom() {
		return custom;
	}

	public void setCustom(Custom custom) {
		this.custom = custom;
	}

	public MyListbox getCustomList() {
		return customList;
	}

	public void setCustomList(MyListbox customList) {
		this.customList = customList;
	}

	public Button getBtnApagar() {
		return btnApagar;
	}

	public void setBtnApagar(Button btnApagar) {
		this.btnApagar = btnApagar;
	}

	public Textbox getTxtChave() {
		return txtChave;
	}

	public void setTxtChave(Textbox txtChave) {
		this.txtChave = txtChave;
	}

	public Textbox getTxtValor() {
		return txtValor;
	}

	public void setTxtValor(Textbox txtValor) {
		this.txtValor = txtValor;
	}

	public List<SistemaEnum> getLsSistemaEnum() {
		return lsSistemaEnum;
	}

	public void setLsSistemaEnum(List<SistemaEnum> lsSistemaEnum) {
		this.lsSistemaEnum = lsSistemaEnum;
	}

	public List<CustomTipo> getLsTipoEnum() {
		return lsTipoEnum;
	}

	public void setLsTipoEnum(List<CustomTipo> lsTipoEnum) {
		this.lsTipoEnum = lsTipoEnum;
	}

	public Combobox getCmbSistema() {
		return cmbSistema;
	}

	public void setCmbSistema(Combobox cmbSistema) {
		this.cmbSistema = cmbSistema;
	}

	public Combobox getCmbTipo() {
		return cmbTipo;
	}

	public void setCmbTipo(Combobox cmbTipo) {
		this.cmbTipo = cmbTipo;
	}
}
