/* XSSFDynamicFilter.java

	Purpose:
		
	Description:
		
	History:
		May 16, 2016 6:59:25 PM, Created by henrichen

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

package org.zkoss.poi.xssf.usermodel;

import java.io.Serializable;
import java.util.ArrayList;

import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDynamicFilter;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFilterColumn;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STDynamicFilterType;
import org.zkoss.poi.ss.usermodel.DynamicFilter;
import org.zkoss.poi.xssf.usermodel.XSSFAutoFilter.XSSFFilterColumn;

/**
 * @author henri
 * @since 3.9.11
 */
public class XSSFDynamicFilter implements DynamicFilter, Serializable {
	private static final long serialVersionUID = -8123574382145472060L;
	private CTDynamicFilter _ctdf;
	
	/*package*/ XSSFDynamicFilter(CTDynamicFilter ctdf) {
		this._ctdf = ctdf;
	}
	
	public XSSFDynamicFilter(XSSFFilterColumn  fc) {
		CTFilterColumn ctfc = fc.getCTFilterColumn();
		if (ctfc.isSetDynamicFilter()) {
			ctfc.unsetDynamicFilter();
		}
		_ctdf = ctfc.addNewDynamicFilter();
	}
	
	@Override
	public Double getMaxValue() {
		return _ctdf.isSetMaxVal() ? _ctdf.getMaxVal() : null;
	}
	
	@Override
	public Double getValue() {
		return _ctdf.isSetVal() ? _ctdf.getVal() : null;
	}

	//ZSS-1234
	@Override
	public String getType() {
		final String t = _ctdf.getType().toString();
		return "null".equals(t) ? null : t;
	}

	@Override
	public void setProperties(Double maxValue, Double value, String type) {
		if (maxValue != null) {
			_ctdf.setMaxVal(maxValue);
		} else if (_ctdf.isSetMaxVal()) {
			_ctdf.unsetMaxVal();
		}
		if (value != null) {
			_ctdf.setVal(value);
		} else if (_ctdf.isSetVal()) {
			_ctdf.unsetVal();
		}
		//ZSS-1234
		_ctdf.setType(STDynamicFilterType.Enum.forString(type == null ? "null" : type));
	}

}
