/* CustomSortHandler.java

{{IS_NOTE
	Purpose:
		
	Description:
		
	History:
		2013/8/5 , Created by dennis
}}IS_NOTE

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

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

import java.util.List;

import org.zkoss.util.resource.Labels;
import org.zkoss.zk.ui.event.SerializableEventListener;
import org.zkoss.zss.api.Range;
import org.zkoss.zss.api.Ranges;
import org.zkoss.zss.api.AreaRef;
import org.zkoss.zss.api.model.Book;
import org.zkoss.zss.api.model.Sheet;
import org.zkoss.zss.model.CellRegion;
import org.zkoss.zss.range.SRange;
import org.zkoss.zss.range.impl.DataRegionHelper;
import org.zkoss.zss.ui.UserActionContext;
import org.zkoss.zss.ui.impl.ua.AbstractHandler;
import org.zkoss.zss.ui.impl.undo.SortCellAction;
import org.zkoss.zss.ui.sys.UndoableActionManager;
import org.zkoss.zssex.ui.dialog.CustomSortCtrl;
import org.zkoss.zssex.ui.dialog.CustomSortCtrl.SortRule;
import org.zkoss.zssex.ui.dialog.impl.DialogCallbackEvent;

/**
 * @author dennis
 *
 */
public class CustomSortHandler extends AbstractHandler {
	private static final long serialVersionUID = -8000940195023894982L;

	/* (non-Javadoc)
	 * @see org.zkoss.zss.ui.sys.ua.impl.AbstractHandler#processAction(org.zkoss.zss.ui.UserActionContext)
	 */
	@Override
	protected boolean processAction(final UserActionContext ctx) {
		final Sheet sheet = ctx.getSheet();
		AreaRef selection = ctx.getSelection();
		//ZSS-1261: select one cell == select all valid range
		SRange srcrng = Ranges.range(sheet, selection.getRow(), selection.getColumn(), selection.getLastRow(), selection.getLastColumn()).getInternalRange();
		CellRegion rng = new DataRegionHelper(srcrng).findCustomSortRegion();
		final AreaRef selection0 = new AreaRef(rng.getRow(),rng.getColumn(),rng.getLastRow(),rng.getLastColumn());

		final Range range = Ranges.range(sheet, selection0);
		if (range.isProtected()) {
			showProtectMessage();
			return true;
		}
		
		CustomSortCtrl.show(new SerializableEventListener<DialogCallbackEvent>(){
			private static final long serialVersionUID = -4841252864311531700L;

			@Override
			public void onEvent(DialogCallbackEvent event) throws Exception {
				if(CustomSortCtrl.ON_OK.equals(event.getName())){
					boolean caseSensitive = (Boolean)event.getData(CustomSortCtrl.ARG_CASE_SENSITIVE);
					boolean hasHeader = (Boolean)event.getData(CustomSortCtrl.ARG_HAS_HEADER);
					boolean byRow = "row".equals((String)event.getData(CustomSortCtrl.ARG_ORIENTATION));
					List<SortRule> rules = (List<SortRule>)event.getData(CustomSortCtrl.ARG_RULES);
					if(rules.size()<1){
						return;
					}
					Object[][] sortInfo = new Object[3][];
					
					int row = selection0.getRow();
					int col = selection0.getColumn();
					int lastrow = selection0.getLastRow();
					int lastcol = selection0.getLastColumn();
					
					//only support max to 3
					for(int i=0;i<3;i++){//init sort information
						Object[] info = new Object[3];
						if(i<rules.size()){
							SortRule rule = rules.get(i);
							if(byRow){
								info[0] = Ranges.range(sheet,row+rule.sortIndex,col,row+rule.sortIndex,lastcol);
							}else{
								info[0] = Ranges.range(sheet,row,col+rule.sortIndex,lastrow,col+rule.sortIndex);
							}
							info[1] = !rule.ascending;
						}else{
							info[0] = null;//range
							info[1] = false;//desc
						}
						sortInfo[i] = info;
					}
					UndoableActionManager uam = ctx.getSpreadsheet().getUndoableActionManager();
					uam.doAction(new SortCellAction(Labels.getLabel("zss.undo.sort"),sheet, selection0.getRow(), selection0.getColumn(),
						selection0.getLastRow(), selection0.getLastColumn(),
						(Range)sortInfo[0][0],(Boolean)sortInfo[0][1],null,
						(Range)sortInfo[1][0],(Boolean)sortInfo[1][1],null,
						(Range)sortInfo[2][0],(Boolean)sortInfo[2][1],null,
						hasHeader,caseSensitive,byRow));
				}
			}}, sheet, selection0, ctx.getSpreadsheet());
		return true;
	}

	@Override
	public boolean isEnabled(Book book, Sheet sheet) {
		return book != null && sheet != null && ( !sheet.isProtected() ||
				Ranges.range(sheet).getSheetProtection().isSortAllowed());
	}

}
