/* 
	Purpose:
		
	Description:
		
	History:
		2013/7/10, Created by dennis

Copyright (C) 2013 Potix Corporation. All Rights Reserved.

*/
package org.zkoss.zssex.ui.dialog.impl;

import java.util.HashMap;
import java.util.Map;

import org.zkoss.lang.Objects;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.UiException;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.event.SerializableEventListener;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zss.api.AreaRef;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Window;
import org.zkoss.zul.ext.Selectable;
/**
 * 
 * @author dennis
 *
 */
public class DialogCtrlBase extends SelectorComposer<Window>{
	private static final long serialVersionUID = 8725038301961603253L;
	
	private final static String ARG_CALLBACK = "callback";
	protected EventListener callback;
	
	public static final String ON_OK = "onOK";
	public static final String ON_CANCEL = "onCancel";
	
	@Override
	public void doAfterCompose(Window comp) throws Exception {
		super.doAfterCompose(comp);
		
		callback = (EventListener)Executions.getCurrent().getArg().get(ARG_CALLBACK);
		if(callback==null){
			throw new UiException("callback for dialog not found");
		}
		
		comp.addEventListener("onCallback", new SerializableEventListener<Event>() {
			private static final long serialVersionUID = -25694149725985620L;

			public void onEvent(Event event) throws Exception {
				Object[] data = (Object[])event.getData();
				DialogCallbackEvent evt = new DialogCallbackEvent((String)data[0],(Map<String,Object>)data[1]);
				callback.onEvent(evt);
			}
		});
	}
	
	
	protected Component getFellow(String compId){
		return getSelf().getFellow(compId);
	}
	
	protected static Map newArg(EventListener<DialogCallbackEvent> callback) {
		Map arg = new HashMap();
		arg.put(ARG_CALLBACK, callback);
		return arg;
	}
	
	protected void postCallback(String eventName,Map<String,Object> data){
		Events.postEvent("onCallback",getSelf(),new Object[]{eventName,data});
		
	}
	protected void sendCallback(String eventName,Map<String,Object> data){
		Events.sendEvent("onCallback",getSelf(),new Object[]{eventName,data});
	}
	
	protected void detach(){
		getSelf().detach();
	}
	
	
	static public class Entry {
		final String name;
		final Object value;
		public Entry(String name,Object value){
			this.name = name;
			this.value = value;
		}
	}
	
	static protected Entry newEntry(String name,Object value){
		Entry arg = new Entry(name,value);
		return arg;
	}
	
	static protected Map<String,Object> newMap(Entry... args){
		Map<String,Object> argm = new HashMap<String,Object>();
		for(Entry arg:args){
			argm.put(arg.name,arg.value);
		}
		return argm;
	}
	
	static protected Object getSingleSelection(ListModel model){
		Selectable sel = model instanceof Selectable?(Selectable)model:null;
		if(sel!=null && sel.getSelection().size()>0){
			return sel.getSelection().iterator().next();
		}
		return null;
	}
	
	static protected int getSingleSelectionIndex(ListModel model){
		Selectable sel = model instanceof Selectable?(Selectable)model:null;
		if(sel!=null && sel.getSelection().size()>0){
			Object obj = sel.getSelection().iterator().next();
			if(model instanceof ListModelList){
				return ((ListModelList)model).indexOf(obj);
			}
			
			int s = model.getSize();
			for(int i =0;i<s;i++){
				if(Objects.equals(obj, model.getElementAt(i))){
					return i;
				}
			}
		}
		return -1;
	}
}
