/* XSSFCustomFilter.java

	Purpose:
		
	Description:
		
	History:
		May 11, 2016 9:56:50 AM, Created by henrichen

	Copyright (C) 2016 Potix Corporation. All Rights Reserved.
*/

package org.zkoss.poi.xssf.usermodel;

import java.io.Serializable;

import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCustomFilter;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STFilterOperator;
import org.zkoss.poi.ss.usermodel.CustomFilter;

//ZSS-1224
/**
 * @author henri
 * @since 3.9.11
 */
public class XSSFCustomFilter implements CustomFilter, Serializable {
	private CTCustomFilter _ctFilter;
	
	/*package*/ XSSFCustomFilter(CTCustomFilter ctFilter) {
		_ctFilter = ctFilter;
	}
	
	@Override
	public void setProperties(Operator operator, String value) {
		switch(operator) {
		case greaterThan:
		case greaterThanOrEqual:
		case lessThan:
		case lessThanOrEqual:
		case notEqual:
			_ctFilter.setVal(value);
			_ctFilter.setOperator(STFilterOperator.Enum.forString(operator.name()));
			break;
			
		case notBeginWith:
			_ctFilter.setVal(value + "*");
			_ctFilter.setOperator(STFilterOperator.Enum.forString(Operator.notEqual.name()));
			break;
			
		case notEndWith:
			_ctFilter.setVal("*" + value);
			_ctFilter.setOperator(STFilterOperator.Enum.forString(Operator.notEqual.name()));
			break;
			
		case notContains:
			_ctFilter.setVal("*" + value + "*");
			_ctFilter.setOperator(STFilterOperator.Enum.forString(Operator.notEqual.name()));
			break;
			
		case beginWith:
			_ctFilter.setVal(value + "*");
			if (_ctFilter.isSetOperator()) {
				_ctFilter.unsetOperator();
			}
			break;
			
		case endWith:
			_ctFilter.setVal("*" + value);
			if (_ctFilter.isSetOperator()) {
				_ctFilter.unsetOperator();
			}
			break;
			
		case contains:
			_ctFilter.setVal("*" + value + "*");
			if (_ctFilter.isSetOperator()) {
				_ctFilter.unsetOperator();
			}
			break;
			
		case equal:
		default:
			_ctFilter.setVal(value);
			if (_ctFilter.isSetOperator()) {
				_ctFilter.unsetOperator();
			}
			break;
		}
	}

	@Override
	public String getValue() {
		final String val = _ctFilter.getVal();
		boolean endWith = false;
		boolean beginWith = false;
		if (val.startsWith("*")) {
			endWith = true;
		}
		if (val.endsWith("*")) {
			beginWith = true;
		}
		boolean contains = beginWith && endWith;
		
		if (contains) {
			return val.substring(1, val.length()-1);
		} else if (beginWith) {
			return val.substring(0, val.length()-1);
		} else if (endWith) {
			return val.substring(1);
		} else {
			return val;
		}
	}

	@Override
	public Operator getOperator() {
		final String val = _ctFilter.getVal();
		boolean endWith = false;
		boolean beginWith = false;
		if (val.startsWith("*")) {
			endWith = true;
		}
		if (val.endsWith("*")) {
			beginWith = true;
		}
		boolean contains = beginWith && endWith;
		
		switch(_ctFilter.getOperator().intValue()) {
		case STFilterOperator.INT_GREATER_THAN:
			return CustomFilter.Operator.greaterThan;
			
		case STFilterOperator.INT_GREATER_THAN_OR_EQUAL:
			return CustomFilter.Operator.greaterThanOrEqual;
			
		case STFilterOperator.INT_LESS_THAN:
			return CustomFilter.Operator.lessThan;
			
		case STFilterOperator.INT_LESS_THAN_OR_EQUAL:
			return CustomFilter.Operator.lessThanOrEqual;
			
		case STFilterOperator.INT_NOT_EQUAL:
			if (contains) {
				return CustomFilter.Operator.notContains;
			} else if (beginWith) {
				return CustomFilter.Operator.notBeginWith;
			} else if (endWith) {
				return CustomFilter.Operator.notEndWith;
			} else { //notEqual
				return CustomFilter.Operator.notEqual;
			}
		
		//equal
		default:
			if (contains) {
				return CustomFilter.Operator.contains;
			} else if (beginWith) {
				return CustomFilter.Operator.beginWith;
			} else if (endWith) {
				return CustomFilter.Operator.endWith;
			} else { //equal
				return CustomFilter.Operator.equal;
			}
		}
	}

}
