/* ProtectSheetCtrl.java

	Purpose:
		
	Description:
		
	History:
		Wed, Apr 30, 2014 12:12:41 PM, Created by RaymondChao

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

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

import java.util.Map;

import org.zkoss.util.resource.Labels;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.SerializableEventListener;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zss.api.model.SheetProtection;
import org.zkoss.zssex.ui.dialog.impl.DialogCallbackEvent;
import org.zkoss.zssex.ui.dialog.impl.DialogCtrlBase;
import org.zkoss.zul.Button;
import org.zkoss.zul.Checkbox;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

/**
 * 
 * @author RaymondChao
 */
public class ProtectSheetCtrl extends DialogCtrlBase {
	private static final long serialVersionUID = -4600082937506137037L;

	private final static String URI = "~./zssex/dlg/protectSheet.zul";
	
	public final static String ARG_PASSWORD = "password";
	public final static String ARG_SHEET_PROTECTION = "sheetProtection";
	public final static String ARG_ALLOW_OPTIONS = "allowedOptions";
	
	@Wire
	private Button okBtn;
	
	@Wire
	private Checkbox checkbox;

	@Wire
	private Textbox passwordBox;
	
	@Wire
	private Listbox allowedOptions;
	
	@Wire
	private Listitem lockCells, unlockCells,
		formatCells, formatColumns, formatRows,
		insertColumns, insertRows, insertHyperlinks,
		deleteColumns, deleteRows, sort, autoFilter,
		//pivotTable, 
		editObjects;
		//editScenarios; 
	
	public static void show(EventListener<DialogCallbackEvent> callback, SheetProtection sheetProtection) {
		Map arg = newArg(callback);
		arg.put(ARG_SHEET_PROTECTION, sheetProtection);
		
		Window comp = (Window) Executions.createComponents(URI, null, arg);
		comp.doModal();
		return;
	}
	
	public void doAfterCompose(Window comp) throws Exception {
		super.doAfterCompose(comp);
		
		Map arg = Executions.getCurrent().getArg();
		initialOptions((SheetProtection) arg.get(ARG_SHEET_PROTECTION));
	}
	@Listen("onOK=window; onClick=#okBtn")
	public void onOK() {
		final String password = passwordBox.getValue();
		if (password == null || password.isEmpty()) {
			postData();
		} else {
			final String title = Labels.getLabel("zssex.dlg.protectSheet.confirm_passowrd"),
					hint = Labels.getLabel("zssex.dlg.protectSheet.confirm_passowrd_hint"),
					caution = Labels.getLabel("zssex.dlg.protectSheet.confirm_passowrd_caution");
			
			PasswordCtrl.show(new SerializableEventListener<DialogCallbackEvent>(){
				private static final long serialVersionUID = 420272954779448143L;

				@Override
				public void onEvent(DialogCallbackEvent event) throws Exception {
					if (ProtectSheetCtrl.ON_OK.equals(event.getName())) {
						if (password.equals((String) event.getData("password"))) {
							postData();
						} else {
							Messagebox.show(Labels.getLabel("zssex.dlg.protectSheet.confirm_passowrd_error"), "ZK Spreadsheet",
									Messagebox.OK, Messagebox.EXCLAMATION, new SerializableEventListener() {
										private static final long serialVersionUID = -7358057033654454683L;

										public void onEvent(Event evt) {
											passwordBox.setFocus(true);
											passwordBox.select();
										}
									});
						}
					}
				}}, title, hint, caution);
		}
	}
	
	@Listen("onCheck = #checkbox")
	public void onCheck() {
		okBtn.setDisabled(!checkbox.isChecked());
	}
	
	@Listen("onCancel = window; onClick = #cancelBtn")
	public void onCancel() {
		detach();
		postCallback(ON_CANCEL, null);
	}
	
	@Listen("onClick = #lockCells")
	public void selectUnlockCells() {
		if (lockCells.isSelected()) {
			unlockCells.setSelected(true);
		}
	}
	
	@Listen("onClick = #unlockCells")
	public void unselectLockCells() {
		if (!unlockCells.isSelected()) {
			lockCells.setSelected(false);
		}
	}
	
	private void postData() {
		detach();
		int result = 0;
		for (Listitem listitem: allowedOptions.getSelectedItems()) {
			result = result + Integer.parseInt((String) listitem.getValue(), 16);
		}
		
		postCallback(ON_OK, newMap(
			newEntry(ARG_PASSWORD, passwordBox.getValue()),
			newEntry(ARG_ALLOW_OPTIONS, result)));
	}
	
	private void initialOptions(SheetProtection sheetProtection) {
		if (sheetProtection.isSelectLockedCellsAllowed()) {
			lockCells.setSelected(true);
		}
		if (sheetProtection.isSelectUnlockedCellsAllowed()) {
			unlockCells.setSelected(true);
		}
		if (sheetProtection.isFormatCellsAllowed()) {
			formatCells.setSelected(true);
		}
		if (sheetProtection.isFormatColumnsAllowed()) {
			formatColumns.setSelected(true);
		}
		if (sheetProtection.isFormatRowsAllowed()) {
			formatRows.setSelected(true);
		}
		if (sheetProtection.isInsertColumnsAllowed()) {
			insertColumns.setSelected(true);
		}
		if (sheetProtection.isInsertRowsAllowed()) {
			insertRows.setSelected(true);
		}
		if (sheetProtection.isInsertHyperlinksAllowed()) {
			insertHyperlinks.setSelected(true);
		}
		if (sheetProtection.isDeleteColumnsAllowed()) {
			deleteColumns.setSelected(true);
		}
		if (sheetProtection.isDeleteRowsAllowed()) {
			deleteRows.setSelected(true);
		}
		if (sheetProtection.isSortAllowed()) {
			sort.setSelected(true);
		}
		if (sheetProtection.isAutoFilterAllowed()) {
			autoFilter.setSelected(true);
		}
		if (sheetProtection.isObjectsEditable()) {
			editObjects.setSelected(true);
		}
	}
}
