/* CustomFiltersCtrl.java

	Purpose:
		
	Description:
		
	History:
		Fri, May 13, 2016 03:06:12 PM, Created by Henri

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

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

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.zkoss.util.resource.Labels;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zssex.ui.dialog.impl.DialogCallbackEvent;
import org.zkoss.zssex.ui.dialog.impl.DialogCtrlBase;
import org.zkoss.zul.Button;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Spinner;
import org.zkoss.zul.Window;
import org.zkoss.zss.model.SAutoFilter.NFilterColumn;
import org.zkoss.zss.model.STop10Filter;

/**
 * 
 * @author Henri
 */
public class Top10FilterCtrl extends DialogCtrlBase {
	private static final long serialVersionUID = 2289298995088798124L;

	private final static String URI = "~./zssex/dlg/top10Filter.zul";
	
	public final static String ARG_TOPBOTTOM = "topbottom";
	public final static String ARG_VALUE = "value";
	public final static String ARG_ITEMSPERCENT = "itemspercent";

	private ListModelList<TopBottom> topbottomModel;
	private ListModelList<ItemsPercent> itemspercentModel;
	
	@Wire
	private Button okBtn;
	
	@Wire
	private Listbox topbottomListbox;
	
	@Wire
	private Listbox itemspercentListbox;
	
	@Wire
	private Spinner valueSpinner;
	
	public static String getLabel(Enum enum0) {
		return Labels.getLabel("zssex.top10dlg."+enum0.name());
	}
	
	public static void show(EventListener<DialogCallbackEvent> callback, NFilterColumn fc) {
		final STop10Filter top10Filter = fc == null ? null : fc.getTop10Filter();
		Boolean isTop = top10Filter == null ? Boolean.TRUE : top10Filter.isTop();
		Boolean isPercent = top10Filter == null ? Boolean.FALSE : top10Filter.isPercent();
		Double value = top10Filter == null ? 10.0 : top10Filter.getValue();
		
		Map arg = newArg(callback);
		arg.put(ARG_ITEMSPERCENT, isPercent);
		arg.put(ARG_TOPBOTTOM, isTop);
		arg.put(ARG_VALUE, value);
		
		Window comp = (Window) Executions.createComponents(URI, null, arg);
		comp.doModal();
		return;
	}

	private void init(boolean isTop, double value, boolean isPercent) {
		topbottomModel = new ListModelList<TopBottom>(2);
		topbottomModel.add(TopBottom.top);
		topbottomModel.add(TopBottom.bottom);
		
		itemspercentModel = new ListModelList<ItemsPercent>(2);
		itemspercentModel.add(ItemsPercent.items);
		itemspercentModel.add(ItemsPercent.percent);
		
		Set<TopBottom> topSet = new HashSet<TopBottom>();
		topSet.add(isTop ? TopBottom.top : TopBottom.bottom);
		topbottomModel.setSelection(topSet);		
		topbottomListbox.setModel(topbottomModel);

		Set<ItemsPercent> itemSet = new HashSet<ItemsPercent>();
		itemSet.add(isPercent ? ItemsPercent.percent : ItemsPercent.items);
		itemspercentModel.setSelection(itemSet);		
		itemspercentListbox.setModel(itemspercentModel);
		
		valueSpinner.setValue((int) value);
	}
		
	public void doAfterCompose(Window comp) throws Exception {
		super.doAfterCompose(comp);
		
		Map<?, ?> arg = Executions.getCurrent().getArg();
		final boolean isTop = (Boolean)arg.get(ARG_TOPBOTTOM);
		final boolean isPercent = (Boolean)arg.get(ARG_ITEMSPERCENT);
		final double val = (Double)arg.get(ARG_VALUE);

		init(isTop, val, isPercent);
	}

	@Listen("onOK=window; onClick=#okBtn;")
	public void onOK() {
		boolean isTop = topbottomModel.getSelection().iterator().next() == TopBottom.top;
		boolean isPercent = itemspercentModel.getSelection().iterator().next() == ItemsPercent.percent;
		double value = new Double(valueSpinner.getValue());
		
		postCallback(ON_OK, newMap(
				newEntry(ARG_TOPBOTTOM, isTop), 
				newEntry(ARG_ITEMSPERCENT, isPercent),
				newEntry(ARG_VALUE, value)));

		detach();
	}
	
	@Listen("onCancel = window; onClick = #cancelBtn")
	public void onCancel() {
		detach();
		postCallback(ON_CANCEL, null);
	}
	
	enum TopBottom {
		top, bottom;
	}
	
	enum ItemsPercent {
		items, percent;
	}
}


