package it.mice.voila.runtime.springmvc.csrf;

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

public class FormField implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6539168343340594151L;

	private String parameterName;
	private String parameterType;
	private Collection<String> parameterValues;
	private Object actualValue;
	private boolean readonly;
	
	public FormField(String parameterName, String parameterType, boolean readonly) {
		super();
		this.parameterName = parameterName;
		
		this.parameterValues = new ArrayList<String>();
		
		this.parameterType = parameterType;
		this.readonly = readonly;
	}
	
	public String getParameterName() {
		return parameterName;
	}
	public void setParameterName(String parameterName) {
		this.parameterName = parameterName;
	}
	public Collection<String> getParameterValue() {
		return parameterValues;
	}
	public void setParameterValue(Collection<String> parameterValue) {
		this.parameterValues = parameterValue;
	}
	public String getParameterType() {
		return parameterType;
	}
	public void setParameterType(String parameterType) {
		this.parameterType = parameterType;
	}
	public boolean isReadonly() {
		return readonly;
	}
	public void setReadonly(boolean readonly) {
		this.readonly = readonly;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public Object getActualValue() {
		return actualValue;
	}

	public void setActualValue(Object actualValue) {
		this.actualValue = actualValue;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((parameterName == null) ? 0 : parameterName.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		FormField other = (FormField) obj;
		if (parameterName == null) {
			if (other.parameterName != null)
				return false;
		} else if (!parameterName.equals(other.parameterName))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "FormField [parameterName=" + parameterName + ", parameterType="
				+ parameterType + ", parameterValues=" + parameterValues
				+ ", readonly=" + readonly + "]";
	}

	public void leaveOnlyThisValue(String value) {
		if (getParameterValue().size() > 1) {
			Collection<String> newArray = new ArrayList<String>();
			newArray.add(value);
			setParameterValue(newArray);
		}
	}
}
