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

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

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 = TagUtils.getPageStatus(getNestedPath());
		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 (!evaluateBoolean("forceEdit", getForceEdit())) {
			setDisabled("true");
		}
	}

	@Override
	public int doEndTag() throws JspException {
		super.doEndTag();
		if (!evaluateBoolean("disableDojo", getDisableDojo()) && !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 : " + evaluateBoolean("required", getRequired()));
		script.append("}}));");

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

}
