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

import it.mice.voila.runtime.springmvc.bean.PageStatus;
import it.mice.voila.runtime.springmvc.util.ExpressionEvaluationUtils;

import javax.servlet.jsp.JspException;

import org.springframework.web.servlet.tags.form.TagWriter;

public class SelectTag extends
		org.springframework.web.servlet.tags.form.SelectTag {
	/**
	 * serial version UID
	 */
	private static final long serialVersionUID = 3232323232L;

	private TagWriter tagWriter;
	private String required = "false";
	private String disableDojo = null;
	private String forceEdit = null;

	private String protectIfPageStatusIs = null;
	private String protectIfPageStatusIsNot = null;
	
	public String getProtectIfPageStatusIs() {
		return protectIfPageStatusIs;
	}

	public void setProtectIfPageStatusIs(String protectIfPageStatusIs) {
		this.protectIfPageStatusIs = protectIfPageStatusIs;
	}

	public String getProtectIfPageStatusIsNot() {
		return protectIfPageStatusIsNot;
	}

	public void setProtectIfPageStatusIsNot(String protectIfPageStatusIsNot) {
		this.protectIfPageStatusIsNot = protectIfPageStatusIsNot;
	}
	
	public String getRequired() {
		return required;
	}

	public void setRequired(String required) {
		this.required = required;
	}

	public String getDisableDojo() {
		return disableDojo;
	}

	public void setDisableDojo(String disableDojo) {
		this.disableDojo = disableDojo;
	}

	public String getForceEdit() {
		return forceEdit;
	}

	public void setForceEdit(String forceEdit) {
		this.forceEdit = forceEdit;
	}

	@Override
	protected int writeTagContent(TagWriter tagWriter) throws JspException {
		this.tagWriter = tagWriter;
		PageStatus pageStatus;
		try {
			pageStatus = TagUtils.getPageStatus(getNestedPath());
		} catch (javax.servlet.jsp.JspException e) {
			e.printStackTrace();
			throw new JspException(e);
		}
		if (pageStatus.isReadOnly()) {
			protectField();
		}
        if (protectIfPageStatusIs != null && pageStatus.equals(protectIfPageStatusIs)) {
            protectField();
        } else if (protectIfPageStatusIsNot != null && !pageStatus.equals(protectIfPageStatusIsNot)) {
            protectField();
        }
        
		int result = super.writeTagContent(tagWriter);
		return result;
	}

	private void protectField() throws JspException {
		if (!ExpressionEvaluationUtils.evaluateBoolean("forceEdit", getForceEdit(), this.pageContext)) {
			setDisabled(true);
		}
	}

	@Override
	public int doEndTag() throws JspException {
		super.doEndTag();
		if (!ExpressionEvaluationUtils.evaluateBoolean("disableDojo", getDisableDojo(), this.pageContext) && !TagUtils.isBootstrapTheme()) {
			writeDojoScript(tagWriter);
		}

		String value = getDisplayString(getBoundValue(), getPropertyEditor());
		TagUtils.getSecuritySessionBean().setReadOnly(getName(), isReadonly() || isDisabled(), value);
		
		return EVAL_PAGE;
	}

	private void writeDojoScript(TagWriter tagWriter) throws JspException {
		StringBuffer script = new StringBuffer();
		script.append("Spring.addDecoration(new Spring.ElementDecoration({");
		script.append("elementId : '" + resolveId() + "',");
		//script.append("widgetType : 'voila.form.FilteringSelect',");
		script.append("widgetType : 'dijit.form.FilteringSelect',");
		
		script.append("widgetAttrs : {");
		script.append("required : " + ExpressionEvaluationUtils.evaluateBoolean("required", getRequired(), this.pageContext));
		script.append("}}));");

		TagUtils.getCurrentForm(pageContext).addScript(script);
	}
	
	@Override
	public void doFinally() {
		setDisabled(false);
		setRequired(null);
		setForceEdit(null);
		setDisableDojo(null);
		setProtectIfPageStatusIs(null);
		setProtectIfPageStatusIsNot(null);
		super.doFinally();
	}

}
