package com.rjconsultores.ventaboletos.web.utilerias;

import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.zkoss.util.resource.Labels;
import org.zkoss.zk.ui.WrongValueException;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Comboitem;
import org.zkoss.zul.Radio;
import org.zkoss.zul.Radiogroup;
import org.zkoss.zul.Window;


@SuppressWarnings("rawtypes")
public class MyGenericForwardComposer extends GenericForwardComposer {

	private static final long serialVersionUID = 1L;
	public static final int OVERLAPPED = PantallaUtileria.OVERLAPPED;
	public static final int MODAL = PantallaUtileria.MODAL;
	public static final String MSG_OK = "MSG.suscribirOK";
	private static Logger log = LogManager.getLogger(MyGenericForwardComposer.class);

	public void openWindow(String component, String title, Map args) {
		this.superArgs(args);
		PantallaUtileria.openWindow(component, title, args, OVERLAPPED, desktop);
	}

	public void openWindowPosi(String component, String title, Map args, int type, String pos) {
		this.superArgs(args);
		PantallaUtileria.openWindowPosi(component, title, args, type, pos, desktop);
	}

	public void openWindow(String component, String title, Map args, int type, Boolean closabled) {
		this.superArgs(args);
		PantallaUtileria.openWindow(component, title, args, type, closabled, desktop);
	}

	public void openWindow(String component, String title, Map args, int type) {
		this.superArgs(args);
		PantallaUtileria.openWindow(component, title, args, type, desktop);
	}

	public void openWindowPosi(String component, String title, Map args, String pos) {
		this.superArgs(args);
		PantallaUtileria.openWindowPosi(component, title, args, OVERLAPPED, pos, desktop);
	}

	public void closeWindow() {
		if (this.self instanceof Window) {
			((Window) this.self).detach();
		}
	}
	
	public boolean isRegistroNovo(Object id) {		
		return id == null;
	}
	
	public void selecionaRadio( Object campo, Radiogroup radioGroup ) {
		if( campo !=null ) {
			for (Object filho : radioGroup.getChildren()) {
				Radio rd = (Radio)filho;
				if(rd.getValue().equals( campo.toString())){
					radioGroup.setSelectedItem(rd);
					break;
				}
			}
		}
	}
	
	public void selecionaCombo( Object campo, Combobox combo ) {
		for(Object obj : combo.getItems()) {
			Comboitem item = (Comboitem)obj;
			if(item.getValue().equals(campo)) {
				combo.setSelectedItem(item);
				return;
			}
		}
		
		if( combo.getItems().isEmpty() ) {
			Comboitem item = new Comboitem(campo.toString());
			item.setAttribute("value", campo);
			item.setValue(campo);
			item.setParent(combo);
			combo.setSelectedItem(item);
		}
	}

	@Override
	public boolean doCatch(Throwable ex) throws Exception {

		if (ex instanceof WrongValueException) {
			throw (WrongValueException) ex;
		}

		log.error("Erro ao abrir a janela", ex);

		alert(Labels.getLabel("MSG.Error.Open.Windows"));

		this.closeWindow();

		return true;
	}
	
	@SuppressWarnings("unchecked")
	private void superArgs(Map args){
		if(!super.arg.isEmpty()){
			args.putAll(arg);
		}
	}

}
