/* ProtectSheetAction.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 org.zkoss.util.resource.Labels;
import org.zkoss.zk.ui.event.SerializableEventListener;
import org.zkoss.zss.api.IllegalOpArgumentException;
import org.zkoss.zss.api.Range;
import org.zkoss.zss.api.Ranges;
import org.zkoss.zss.api.SheetOperationUtil;
import org.zkoss.zss.api.model.Sheet;
import org.zkoss.zss.model.InvalidModelOpException;
import org.zkoss.zss.ui.UserActionContext;
import org.zkoss.zss.ui.impl.ua.AbstractSheetHandler;
import org.zkoss.zssex.ui.dialog.PasswordCtrl;
import org.zkoss.zssex.ui.dialog.ProtectSheetCtrl;
import org.zkoss.zssex.ui.dialog.impl.DialogCallbackEvent;

/**
 * @author dennis
 *
 */
public class ProtectSheetAction extends AbstractSheetHandler {
	private static final long serialVersionUID = 3461410585095810994L;

	/* (non-Javadoc)
	 * @see org.zkoss.zss.ui.sys.ua.impl.AbstractHandler#processAction(org.zkoss.zss.ui.UserActionContext)
	 */
	@Override
	protected boolean processAction(final UserActionContext ctx) {
		Sheet sheet = ctx.getSheet();
		
		final Range range = Ranges.range(sheet);
		
		if (range.isSheetProtected()) {
			if (!SheetOperationUtil.unprotectSheet(range, null)) {
			PasswordCtrl.show (new SerializableEventListener<DialogCallbackEvent>() {
				private static final long serialVersionUID = -5447189387875752055L;

				@Override
				public void onEvent(DialogCallbackEvent event) throws Exception {
					if(ProtectSheetCtrl.ON_OK.equals(event.getName())){
						try {
							if (!SheetOperationUtil.unprotectSheet(range, (String) event.getData(ProtectSheetCtrl.ARG_PASSWORD))) {
								showInfoMessage(Labels.getLabel ("zss.actionhandler.msg.password_not_correct"));
							}
						} catch (IllegalOpArgumentException x) {
							showInfoMessage(Labels.getLabel ("zss.actionhandler.msg.illegal_range_operation")+" : " + x.getMessage());
						} catch (InvalidModelOpException x) {
							showInfoMessage(Labels.getLabel("zss.actionhandler.msg.illegal_range_operation")+" : " + x.getMessage());
						}
					}
				}}, Labels.getLabel("zssex.dlg.unprotectSheet.title"),
				Labels.getLabel("zssex.dlg.unprotectSheet.hint"), "");
			}
		} else {
			ProtectSheetCtrl.show(new SerializableEventListener<DialogCallbackEvent>(){
				private static final long serialVersionUID = 2491792382934665898L;

				@Override
				public void onEvent(DialogCallbackEvent event) throws Exception {
					if(ProtectSheetCtrl.ON_OK.equals(event.getName())){
						final Integer options = (Integer) event.getData(ProtectSheetCtrl.ARG_ALLOW_OPTIONS);
						SheetOperationUtil.protectSheet(range, (String) event.getData(ProtectSheetCtrl.ARG_PASSWORD),
								isAllowed(options, 0x400), isAllowed(options, 0x4000), isAllowed(options, 0x4),
								isAllowed(options, 0x8), isAllowed(options, 0x10), isAllowed(options, 0x20),
								isAllowed(options, 0x40), isAllowed(options, 0x80), isAllowed(options, 0x100),
								isAllowed(options, 0x200), isAllowed(options, 0x800), isAllowed(options, 0x1000),
								isAllowed(options, 0x2000), isAllowed(options, 0x1), isAllowed(options, 0x2));
					}
				}}, range.getSheetProtection());
		}
		return true;
	}

	private boolean isAllowed(int options, int mask) {
		return (options & mask) != 0;
	}

}
