/* XSSFTop10Filter.java

	Purpose:
		
	Description:
		
	History:
		May 17, 2016 3:20:23 PM, 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.CTCustomFilters;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFilterColumn;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTop10;
import org.zkoss.poi.ss.usermodel.Top10Filter;
import org.zkoss.poi.xssf.usermodel.XSSFAutoFilter.XSSFFilterColumn;

/**
 * @author henri
 * @since 3.9.11
 */
public class XSSFTop10Filter implements Top10Filter, Serializable {
	private CTTop10 _cttop10;

	/*package*/ XSSFTop10Filter(CTTop10 cttop10) {
		_cttop10 = cttop10;
	}
	
	public XSSFTop10Filter(XSSFFilterColumn  fc) {
		CTFilterColumn ctfc = fc.getCTFilterColumn();
		if (ctfc.isSetTop10()) {
			ctfc.unsetTop10();
		}
		_cttop10 = ctfc.addNewTop10();
	}
	
	@Override
	public boolean isTop() {
		return !_cttop10.isSetTop() || _cttop10.getTop(); //default true
	}

	@Override
	public boolean isPercent() {
		return _cttop10.isSetPercent() && _cttop10.getPercent(); //default false
	}

	@Override
	public double getValue() {
		return _cttop10.getVal();
	}

	@Override
	public double getFilterValue() {
		return _cttop10.getFilterVal();
	}
	
	@Override
	public void setProperties(boolean isTop, double value, boolean isPercent, double filterValue) {
		if (isTop) {
			if (_cttop10.isSetTop()) {
				_cttop10.unsetTop();
			}
		} else {
			_cttop10.setTop(false);
		}
		
		_cttop10.setVal(value);
		_cttop10.setFilterVal(filterValue);
		
		if (isPercent) {
			_cttop10.setPercent(true);
		} else {
			if (_cttop10.isSetPercent()) {
				_cttop10.unsetTop();
			}
		}
	}
}
