/* FormatCellHandler.java

{{IS_NOTE
	Purpose:
		
	Description:
		
	History:
		2014/12/22 , Created by JerryChen
}}IS_NOTE

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

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

import org.zkoss.util.resource.Labels;
import org.zkoss.zk.ui.event.SerializableEventListener;
import org.zkoss.zss.api.CellOperationUtil.CellStyleApplierEx;
import org.zkoss.zss.api.Range.CellAttribute;
import org.zkoss.zss.api.Range;
import org.zkoss.zss.api.Ranges;
import org.zkoss.zss.api.AreaRef;
import org.zkoss.zss.api.model.CellStyle.FillPattern;
import org.zkoss.zss.api.model.Color;
import org.zkoss.zss.api.model.Sheet;
import org.zkoss.zss.api.model.impl.EnumUtil;
import org.zkoss.zss.model.SSheet;
import org.zkoss.zss.range.impl.StyleUtil;
import org.zkoss.zss.range.impl.WholeStyleUtil;
import org.zkoss.zss.ui.UserActionContext;
import org.zkoss.zss.ui.impl.ua.AbstractCellHandler;
import org.zkoss.zss.ui.impl.undo.CellStyleAction;
import org.zkoss.zss.ui.sys.UndoableActionManager;
import org.zkoss.zssex.ui.dialog.FormatCellCtrl;
import org.zkoss.zssex.ui.dialog.impl.DialogCallbackEvent;

/**
 * @author JerryChen
 *
 */
public class FormatCellHandler extends AbstractCellHandler {
	private static final long serialVersionUID = -7929818996569565554L;

	/* (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();
		final AreaRef selection = ctx.getSelection();
		Range range = Ranges.range(sheet, selection.getRow(), selection.getColumn());
		String format = range.getCellDataFormat();
		Color backColor = range.getCellStyle().getBackColor();
		Color fillColor = range.getCellStyle().getFillColor();
		FillPattern pattern = range.getCellStyle().getFillPattern();
		int rotation = range.getInternalRange().getCellStyle().getRotation();
		
		
		FormatCellCtrl.show(new SerializableEventListener<DialogCallbackEvent>(){
			private static final long serialVersionUID = -7538418046002248163L;

			@Override
			public void onEvent(DialogCallbackEvent event) throws Exception {
				if(FormatCellCtrl.ON_OK.equals(event.getName())){
					final String format = (String)event.getData(FormatCellCtrl.ARG_FORMAT_CODE);
					final String backColor = (String)event.getData(FormatCellCtrl.ARG_BACKGROUND_CODE);
					final String fillColor = (String)event.getData(FormatCellCtrl.ARG_FILL_COLOR_CODE);
					final FillPattern pattern = (FillPattern)event.getData(FormatCellCtrl.ARG_FILL_PATTERN_CODE);
					final int rotation = (Integer)event.getData(FormatCellCtrl.ARG_ROTATION_CODE);
					CellStyleApplierEx applier = new CellStyleApplierEx() {
						
						@Override
						public void apply(Range range) {
							SSheet sheet = range.getSheet().getInternalSheet();
							if(format != null) {
								StyleUtil.setDataFormat(sheet.getBook(),sheet.getCell(range.getRow(),range.getColumn()),format);
							}
							StyleUtil.setFillOptions(sheet.getBook(), sheet.getCell(range.getRow(), range.getColumn()), 
									backColor, fillColor, EnumUtil.toStyleFillPattern(pattern));
							StyleUtil.setTextRotation(sheet.getBook(), sheet.getCell(range.getRow(), range.getColumn()), rotation);
						}
						
						@Override
						public void applyWhole(Range wholeRange) {
							if(format != null) {
								WholeStyleUtil.setDataFormat(wholeRange.getInternalRange(), format);
							}
							WholeStyleUtil.setFillOptions(wholeRange.getInternalRange(), backColor, fillColor, EnumUtil.toStyleFillPattern(pattern));
							WholeStyleUtil.setTextRotation(wholeRange.getInternalRange(), rotation);
						}
						@Override
						public void notifyChange(Range range) {
							range.notifyChange(CellAttribute.ALL);
						}
					};
					
					UndoableActionManager uam = ctx.getSpreadsheet().getUndoableActionManager();
					uam.doAction(new CellStyleAction(Labels.getLabel("zss.undo.cellFormat"),sheet, selection.getRow(), selection.getColumn(), 
						selection.getLastRow(), selection.getLastColumn(), applier));
				}
				
			}}, format, backColor, fillColor, pattern, rotation, ctx.getSpreadsheet());
		return true;
	}

}
