/**
 * 
 */
package cm.bao.ui.web.gwt.widgets.client.forms.smartgwt;

import cm.bao.ui.web.gwt.widgets.client.utils.StringUtils;

import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.types.DSDataFormat;
import com.smartgwt.client.widgets.form.fields.ComboBoxItem;
import com.smartgwt.client.widgets.grid.HoverCustomizer;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridRecord;

/**
 * @author rlagoue
 * 
 */
public class SuggestBoxItem extends ComboBoxItem {
	private DataSource dataSource;
	private ListGrid pickListProperties = new ListGrid();
	private String criteriaFieldName;
	private String hoverFieldName;
	private String hoverNotFoundMessage = "";
	private String fieldId;
	private String jsonDataURL;

	/**
	 * 
	 * @param name
	 * @param title
	 * @param criteriaFieldName
	 * @param fieldId
	 * @param jsonDataURL
	 */
	public SuggestBoxItem(String name, String title, String criteriaFieldName,
			String fieldId, String jsonDataURL) {
		this(name, title, criteriaFieldName, fieldId, jsonDataURL, null);
	}

	/**
	 * 
	 * @param name
	 * @param title
	 * @param criteriaFieldName
	 * @param fieldId
	 * @param jsonDataURL
	 * @param hoverFieldName
	 */
	public SuggestBoxItem(String name, String title, String criteriaFieldName,
			String fieldId, String jsonDataURL, String hoverFieldName) {
		super(name, title);
		if (StringUtils.isEmptyOrNull(criteriaFieldName)
				|| StringUtils.isEmptyOrNull(fieldId)
				|| StringUtils.isEmptyOrNull(jsonDataURL)) {
			throw new IllegalArgumentException(
					"No parameter may be null or empty");
		}
		this.criteriaFieldName = criteriaFieldName;
		this.fieldId = fieldId;
		this.jsonDataURL = jsonDataURL;
		init();
	}

	private void init() {
		initDataSource();
		initPickListProperties();
		setOptionDataSource(dataSource);
		setDisplayField(criteriaFieldName);
		setValueField(fieldId);
		setPickListProperties(pickListProperties);
	}

	private void initPickListProperties() {
		ListGrid pickListProperties = new ListGrid();
		pickListProperties.setCanHover(true);
		pickListProperties.setShowHover(true);
		initHoverProperty();
	}

	private void initHoverProperty() {
		if (StringUtils.isEmptyOrNull(hoverFieldName)) {
			return;
		}
		pickListProperties.setHoverCustomizer(new HoverCustomizer() {
			@Override
			public String hoverHTML(Object value, ListGridRecord record,
					int rowNum, int colNum) {
				String hoverValueStr = record.getAttribute(hoverFieldName);
				if (hoverValueStr == null)
					hoverValueStr = hoverNotFoundMessage;
				return hoverValueStr;
			}
		});
	}

	private void initDataSource() {
		dataSource = new DataSource(jsonDataURL);
		dataSource.setDataFormat(DSDataFormat.JSON);
		DataSourceTextField id = new DataSourceTextField(fieldId, fieldId);
		id.setHidden(true);
		id.setPrimaryKey(true);
		DataSourceTextField label = new DataSourceTextField(criteriaFieldName,
				criteriaFieldName);
		dataSource.setFields(id, label);
	}

	public Long getValueAsLong() {
		String valueStr = (String) getValue();
		return StringUtils.isEmptyOrNull(valueStr) ? 0L : Long
				.valueOf(valueStr);
	}
	
	public DataSource getDataSource() {
		return dataSource;
	}

}
