/* CustomFiltersHandler.java

{{IS_NOTE
	Purpose:
		
	Description:
		
	History:
		2016/05/13 , Created by Henri
}}IS_NOTE

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

{{IS_RIGHT
}}IS_RIGHT
*/
package org.zkoss.zssex.ui.impl.ua;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.SerializableEventListener;
import org.zkoss.zss.api.AreaRef;
import org.zkoss.zss.api.Ranges;
import org.zkoss.zss.api.Range.AutoFilterOperation;
import org.zkoss.zss.api.Range.SheetVisible;
import org.zkoss.zss.api.model.Book;
import org.zkoss.zss.api.model.Sheet;
import org.zkoss.zss.model.SAutoFilter;
import org.zkoss.zss.model.SAutoFilter.FilterOp;
import org.zkoss.zss.model.SAutoFilter.NFilterColumn;
import org.zkoss.zss.model.SCustomFilter;
import org.zkoss.zss.model.SCustomFilters;
import org.zkoss.zss.ui.UserActionContext;
import org.zkoss.zss.ui.impl.ua.AbstractBookHandler;
import org.zkoss.zssex.ui.dialog.CustomFiltersCtrl;
import org.zkoss.zssex.ui.dialog.UnhideSheetCtrl;
import org.zkoss.zssex.ui.dialog.impl.DialogCallbackEvent;
import org.zkoss.zul.Messagebox;

/**
 * @author Henri
 * @since 3.9.0
 */
public class CustomFiltersHandler extends AbstractBookHandler{
	private static final long serialVersionUID = -6809662212020255159L;

//	@Override
//	public boolean isEnabled(Book book, Sheet sheet) {
//		if(book == null)
//			return false;
//		
//		for(int i = 0, length = book.getNumberOfSheets(); i < length; i++) {
//			if(book.getSheetAt(i).isHidden())
//				return true;
//		}
//		return false;
//	}

	@Override
	protected boolean processAction(final UserActionContext ctx) {
		final Sheet sheet = ctx.getSheet();
		
		//TODO: check if a table filter by check ref
		final SAutoFilter filter = sheet.getInternalSheet().getAutoFilter();
		final int index = (Integer) ctx.getData("field") - 1;
		final FilterOp op = FilterOp.valueOf((String)ctx.getData("op"));
		
		CustomFiltersCtrl.show(new SerializableEventListener<DialogCallbackEvent>() {
			private static final long serialVersionUID = 1L;

			// When press the OK button
			@Override
			public void onEvent(DialogCallbackEvent event) throws Exception {
				Map<String, Object> data = event.getData();
				if(data != null) {	
					boolean isAnd = (Boolean) data.get(CustomFiltersCtrl.ARG_ANDOR);
					FilterOp f1op = (FilterOp)data.get(CustomFiltersCtrl.ARG_F1_OP);
					FilterOp f2op = (FilterOp)data.get(CustomFiltersCtrl.ARG_F2_OP);
					Object f1val = data.get(CustomFiltersCtrl.ARG_F1_VAL); //ZSS-1193
					Object f2val = data.get(CustomFiltersCtrl.ARG_F2_VAL); //ZSS-1193
					if (f1op == FilterOp.none) {
						Messagebox.show("Error in parse line", "ZK Spreadsheet", Messagebox.OK, Messagebox.EXCLAMATION);
						return;
					}
					f1op = convertToNormalOp(f1op); //ZSS-1234
					f2op = convertToNormalOp(f2op); //ZSS-1234
					String[] criteria1 = new String[] {f1op.name(), f1val == null ? "" : f1val.toString()};
					String[] criteria2 = f2op == FilterOp.none ? null : new String[] {f2op.name(), f2val == null ? "" : f2val.toString()};
					Ranges.range(sheet).enableAutoFilter(index+1, 
							isAnd ? AutoFilterOperation.AND : AutoFilterOperation.OR, 
							criteria1, criteria2, true);
				}
			}
		}, filter, index, op); //ZSS-1233
				
		return true;
	}

	//ZSS-1234
	private FilterOp convertToNormalOp(FilterOp op) {
		if (op == null) {
			return null;
		}
	
		switch(op) {
		case after:
			return FilterOp.greaterThan;
		case afterEq:
			return FilterOp.greaterThanOrEqual;
		case before:
			return FilterOp.lessThan;
		case beforeEq:
			return FilterOp.lessThanOrEqual;
		default:
			return op;
		}
	}
}