package com.tandbergtv.workflow.web.page;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import com.tandbergtv.workflow.driver.search.SearchOperator;
import com.tandbergtv.workflow.driver.search.SearchType;

/**
 * This class is an object wrapper for the html form control, like TextBox,
 * Select, etc.,
 * 
 * @author rao
 * 
 */
public class Field implements Serializable {

	private static final long serialVersionUID = 7600402103239677060L;

	private Map<String, String> attributesMap = new HashMap<String, String>();

	private Map<String, String> valuesMap = new HashMap<String, String>();

	public Map<String, String> getAttributesMap() {
		return attributesMap;
	}

	public void setAttributesMap(Map<String, String> attributesMap) {
		this.attributesMap = attributesMap;
	}

	public String getAttribute(String name) {
		return attributesMap.get(name);
	}

	public Map<String, String> getValuesMap() {
		return valuesMap;
	}

	public void setValuesMap(Map<String, String> valuesMap) {
		this.valuesMap = valuesMap;
	}
	
	/**
	 * Returns the name of the entity
	 * 
	 * @return
	 */
	public String getEntity() {
		return getAttribute("entity");
	}
	
	/**
	 * Returns the property of the entity that needs to be used to create the search criterion,
	 * defaults to the name of the field if not explicitly specified
	 * 
	 * @return
	 */
	public String getProperty() {
		String property = getAttribute("property");
		
		if (property != null)
			return property;
		
		return getName();
	}
	
	/**
	 * Return the name. This may be used to create the search parameter
	 * 
	 * @return
	 */
	public String getName() {
		return getAttribute("name");
	}
	
	/**
	 * Returns the display name or localization key
	 * @return
	 */
	public String getLabel() {
		return getAttribute("label");
	}
	
	/**
	 * Returns the column number in which this field should be rendered
	 * 
	 * @return
	 */
	public String getColumn() {
		return getAttribute("column");
	}
	
	/**
	 * Returns the display widget - text field, single select etc
	 * 
	 * @return
	 */
	public String getType() {
		return getAttribute("type");
	}
	
	public boolean getIsTextbox() {
		return getType().equals("textbox");
	}
	
	public boolean getIsSingleSelect() {
		return getType().equals("dropdown");
	}
	
	public boolean getIsMultiSelect() {
		return getType().equals("list");
	}
	
	public boolean getIsDateRange() {
		return getType().equals("dateRange");
	}
	
	public String getStyle() {
		return getAttribute("style");
	}

	public boolean getIsResource() {
		String value = getAttribute("isResource");
		boolean isResource = true;
		if (value != null && value.trim().length() > 0)
			isResource = Boolean.parseBoolean(value);
		return isResource; 
	}
	
	/**
	 * Returns the data type of this field to be used for creating the search parameter
	 * 
	 * @return
	 */
	public SearchType getSearchType() {
		String type = getAttribute("dataType");
		
		if (type == null || type.length() == 0)
			return SearchType.STRING;
		
		return SearchType.valueOf(type);
	}
	
	/**
	 * Returns the search operator of this field
	 * 
	 * @return
	 */
	public SearchOperator getSearchOperator() {
		String operator = getAttribute("operator");
		
		if (operator == null || operator.length() == 0)
			return SearchOperator.EQUAL;
		
		return SearchOperator.valueOf(operator);
	}
	
	public String getParameterType() {
		return getAttribute("paramType");
	}
	
	public boolean getIsValueParameter() {
		return "value".equals(getParameterType());
	}
	
	public boolean getIsListParameter() {
		return "list".equals(getParameterType());
	}
	
	public boolean getIsRangeParameter() {
		return "range".equals(getParameterType());
	}
	
	/**
	 * Determines whether this field has any values or not. The rules are simple - 
	 * a value parameter must have a non-empty string,
	 * a range parameter must have at least one of the ranges non-empty,
	 * a list parameter must have at least one value
	 * 
	 * @param values
	 * @return
	 */
	public boolean hasValues(String... values) {
		if (values == null)
			return false;
		
		if (getIsValueParameter() && values[0].length() == 0)
			return false;
		
		if (getIsRangeParameter()) {
			if (values.length < 2)
				return false;
			
			String from = values[0];
			String to = values[1];
			
			return (from.length() > 0 || to.length() > 0);
		}
		
		if (getIsListParameter())
			return values.length > 0;
		
		return true;
	}
	
	/**
	 * Returns the default style depending on the field type. Currently
	 * only list and drop down have default styles. 
	 * 
	 * @return
	 */
	public String getDefaultStyle() {
		String type = this.getType();
		if (type.equals("dropdown")) {
			return "width: 280px; max-height: 140px;";
		} else if (type.equals("list")) {
			return "width: 280px; max-height: 140px;";
		} else if (type.equals("textbox")) {
			return "";
		} else if (type.equals("dateRange")) {
			return "";
		}
		return "";
	}
}
