/* PasteSpecialWindowCtrl.java

{{IS_NOTE
	Purpose:
		
	Description:
		
	History:
		Sep 1, 2010 6:05:07 PM , Created by Sam
}}IS_NOTE

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

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

import java.util.Map;

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.zss.api.Range.PasteOperation;
import org.zkoss.zss.api.Range.PasteType;
import org.zkoss.zss.api.model.Book;
import org.zkoss.zss.api.model.Book.BookType;
import org.zkoss.zss.ui.Spreadsheet;
import org.zkoss.zssex.ui.dialog.impl.DialogCallbackEvent;
import org.zkoss.zssex.ui.dialog.impl.DialogCtrlBase;
import org.zkoss.zssex.ui.dialog.impl.RefocusDialogCtrlBase;
import org.zkoss.zul.Checkbox;
import org.zkoss.zul.Radio;
import org.zkoss.zul.Radiogroup;
import org.zkoss.zul.Window;
/**
 * @author Sam
 *
 */
public class PasteSpecialCtrl extends RefocusDialogCtrlBase {
	private static final long serialVersionUID = 1691798403088433067L;

	private final static String URI = "~./zssex/dlg/pasteSpecial.zul";
	
	public final static String ARG_PASTE_OP = "pasteOp";
	public final static String ARG_SKIP_BLANK = "skipBlank";
	public final static String ARG_PASTE_TYPE = "pasteType";
	public final static String ARG_TRANSPOSE = "transport";
	
	public final static String ARG_BOOK_TYPE = "bookType";
	
	@Wire
	private Radiogroup pasteTypeRadiogroup;
	@Wire
	private Radiogroup operationRadiogroup;
	@Wire
	private Checkbox skipBlankBox;
	@Wire
	private Checkbox transposeBox;
	
	@Wire
	private Radio comment;
	@Wire
	private Radio validation;

	public static void show(EventListener<DialogCallbackEvent> callback,Book.BookType type, Spreadsheet ss) {
		Map arg = newArg(callback);
		arg.put(ARG_BOOK_TYPE, type);
		arg.put(ARG_SPREADSHEET, ss);
		
		Window comp = (Window)Executions.createComponents(URI, null, arg);
		comp.doModal();
		return;
	}
	
	public PasteSpecialCtrl () {
	}
	
	public void doAfterCompose(Window w) throws Exception{
		super.doAfterCompose(w);
		
		Book.BookType type = (Book.BookType)Executions.getCurrent().getArg().get(ARG_BOOK_TYPE);
		if(type==BookType.XLS){
			comment.setDisabled(true);
			validation.setDisabled(true);
		}
	}

	@Listen("onOK=window; onClick=#okBtn")
	public void onClick$okBtn() {
		
		detach();
		postCallback(ON_OK, newMap(
			newEntry(ARG_PASTE_TYPE, getPasteType()),
			newEntry(ARG_PASTE_OP, PasteOperation.NONE/*getPasteOperation()*/),
			newEntry(ARG_SKIP_BLANK, skipBlankBox.isChecked()),
			newEntry(ARG_TRANSPOSE, transposeBox.isChecked())));
	}
	
	private PasteOperation getPasteOperation() {
		Radio sel = operationRadiogroup.getSelectedItem();
		String operation = sel==null?null:(String)sel.getValue();
		if (operation == null || "none".equals(operation) )
			return PasteOperation.NONE;
		if ( "add".equals(operation) ) {
			return PasteOperation.ADD;
		} else if ( "sub".equals(operation) ) {
			return PasteOperation.SUB;
		} else if ( "mul".equals(operation) ) {
			return PasteOperation.MUL;
		} else if ( "divide".equals(operation) ) {
			return PasteOperation.DIV;
		}
		//for log, should remove after test
		new Exception("unknow paste operaton " +operation).printStackTrace();
		return PasteOperation.NONE;
	}
	
	private PasteType getPasteType() {
		Radio sel = pasteTypeRadiogroup.getSelectedItem();
		String type = sel==null?null:(String)sel.getValue();
		if (type == null 
				|| "paste".equals(type)
				|| "all".equals(type) )
			return PasteType.ALL;
		
		if ( "allExcpetBorder".equals(type) ) {
			return PasteType.ALL_EXCEPT_BORDERS;
		} else if ( "columnWidth".equals(type) ) {
			return PasteType.COLUMN_WIDTHS;
		} else if ( "comment".equals(type) ) {
			return PasteType.COMMENTS;
		} else if ( "formula".equals(type) ) {
			return PasteType.FORMULAS;
		} else if ( "formulaWithNumFmt".equals(type) ) {
			return PasteType.FORMULAS_AND_NUMBER_FORMATS;
		} else if ( "value".equals(type) ) {
			return PasteType.VALUES;
		} else if ( "valueWithNumFmt".equals(type) ) {
			return PasteType.VALUES_AND_NUMBER_FORMATS;
		} else if ( "format".equals(type) ) {
			return PasteType.FORMATS;
		} else if ( "validation".equals(type) ) {
			return PasteType.VALIDATAION;
		}
		//for log, should remove after test
		new Exception("unknow paste operaton " +type).printStackTrace();
		return PasteType.ALL;
	}
	
	@Listen("onCancel=window; onClick=#cancelBtn")
	public void onCancel(){
		detach();
		postCallback(ON_CANCEL, null);
	}
}