/* DataValidationAction.java

{{IS_NOTE
	Purpose:
		
	Description:
		
	History:
		Wed, March 27, 2015 10:27:56 AM, Created by JerryChen
}}IS_NOTE

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

{{IS_RIGHT
	This program is distributed under GPL Version 2.0 in the hope that
	it will be useful, but WITHOUT ANY WARRANTY.
}}IS_RIGHT
*/
package org.zkoss.zssex.ui.impl.undo;

import java.util.List;

import org.zkoss.zss.api.Range;
import org.zkoss.zss.api.Ranges;
import org.zkoss.zss.api.model.Sheet;
import org.zkoss.zss.api.model.Validation;
import org.zkoss.zss.api.model.Validation.AlertStyle;
import org.zkoss.zss.api.model.Validation.OperatorType;
import org.zkoss.zss.api.model.Validation.ValidationType;
import org.zkoss.zss.ui.impl.undo.AbstractUndoableAction;
import org.zkoss.zssex.ui.dialog.DataValidationCtrl;
/**
 * 
 * @author JerryChen
 * @since 3.8.0
 */
public class DataValidationAction extends AbstractUndoableAction {
	private static final long serialVersionUID = -1030348652602783447L;
	
	private ValidationType _validationType;
	private OperatorType _operatorType;
	private boolean _ignoreBlank;
	private boolean _inCellDropDown;
	private String _param1;
	private String _param2;
	
	private boolean _isShowInput;
	private String _inputTitle;
	private String _inputMessage;
	
	private boolean _isShowError;
	private AlertStyle _alertStyle;
	private String _errorTitle;
	private String _errorMessage;
	
	
	private ValidationType _oldValidationType;
	private OperatorType _oldOperatorType;
	private boolean _oldIgnoreBlank;
	private boolean _oldInCellDropDown;
	private String _oldParam1;
	private String _oldParam2;
	
	private boolean _oldIsShowInput;
	private String _oldInputTitle;
	private String _oldInputMessage;
	
	private boolean _oldIsShowError;
	private AlertStyle _oldAlertStyle;
	private String _oldErrorTitle;
	private String _oldErrorMessage;
	
	private boolean _doFlag;
	private boolean _isNew;
	
	public DataValidationAction(
		String label,
		Sheet sheet,
		int row,
		int column,
		int lastRow,
		int lastColumn,
		ValidationType validationType,
		boolean ignoreBlank,
		OperatorType operatorType,
		boolean inCellDropDown,
		String param1,
		String param2,
		
		boolean showInput,
		String inputTitle,
		String inputMessage,
		
		boolean showError,
		AlertStyle alertStyle,
		String errorTitle,
		String errorMessage	
	) {
		super(label, sheet, row, column, lastRow, lastColumn);
		_validationType = validationType; 
		_operatorType = operatorType;
		_ignoreBlank = ignoreBlank;
		_inCellDropDown = inCellDropDown;
		_param1 = param1;
		_param2 = param2;
		
		_isShowInput = showInput;
		_inputTitle = inputTitle;
		_inputMessage = inputMessage;
		
		_isShowError = showError;
		_alertStyle = alertStyle;
		_errorTitle = errorTitle;
		_errorMessage = errorMessage;
	}

	@Override
	public void doAction() {
		if(!isSheetProtected()) {
			Range r = Ranges.range(_sheet, _row, _column, _lastRow, _lastColumn);
			List<Validation> validations = r.getValidations();
			if(validations.size() > 0) {
				Validation v = validations.get(0);
				_oldValidationType = v.getValidationType();
				_oldOperatorType = v.getOperatorType();
				_oldIgnoreBlank = v.isIgnoreBlank();
				_oldInCellDropDown = v.isInCellDropdown();
				_oldParam1 = v.getFormula1();
				_oldParam2 = v.getFormula2();
				
				_oldIsShowInput = v.isShowInput();
				_oldInputTitle = v.getInputTitle();
				_oldInputMessage = v.getInputMessage();
				
				_oldIsShowError = v.isShowError();
				_oldAlertStyle = v.getAlertStyle();
				_oldErrorTitle = v.getErrorTitle();
				_oldErrorMessage = v.getErrorMessage();
				_isNew = false;
			} else {
				_isNew = true;
			}
			
			r.setValidation(
					_validationType, 
					_ignoreBlank, 
					_operatorType, 
					_inCellDropDown, 
					_param1, 
					_param2, 
					_isShowInput, 
					_inputTitle, 
					_inputMessage, 
					_isShowError, 
					_alertStyle, 
					_errorTitle, 
					_errorMessage);
			
			_doFlag = true;
		}
	}

	@Override
	public boolean isUndoable() {
		return _doFlag && isSheetAvailable() && !isSheetProtected();
	}

	@Override
	public boolean isRedoable() {
		return !_doFlag && isSheetAvailable() && !isSheetProtected();
	}

	@Override
	public void undoAction() {
		Range r = Ranges.range(_sheet, _row, _column, _lastRow, _lastColumn);
		if(_isNew) {
			r.deleteValidation();
		} else {
			r.setValidation(
				_oldValidationType, 
				_oldIgnoreBlank, 
				_oldOperatorType, 
				_oldInCellDropDown, 
				_oldParam1, 
				_oldParam2, 
				_oldIsShowInput, 
				_oldInputTitle, 
				_oldInputMessage, 
				_oldIsShowError, 
				_oldAlertStyle, 
				_oldErrorTitle, 
				_oldErrorMessage);
		}
		_doFlag = false;
	}
	
	@Override
	protected boolean isSheetProtected(){
		final Range range = Ranges.range(_sheet);
		return super.isSheetProtected();
	}
	
	
}
