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.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.tags.form.TagWriter;

public class CheckBoxTag extends
		org.springframework.web.servlet.tags.form.CheckboxTag {

	/**
	 * 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();
        }

		return super.writeTagContent(tagWriter);
	}

	private void protectField() throws JspException {
		if (!ExpressionEvaluationUtils.evaluateBoolean("forceEdit", getForceEdit(), this.pageContext)) {
			setReadonly(true);
			setDisabled(true);
			setTabindex("-1");
		    if (getCssClass() == null || getCssClass().indexOf("protected") == -1) {
		        setCssClass(StringUtils.defaultString(getCssClass()) + " protected");
		    }
		}
	}

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

	private void writeDojoScript(TagWriter tagWriter) throws JspException {
		StringBuffer script = new StringBuffer();
		script.append("Spring.addDecoration(new Spring.ElementDecoration({");
		script.append("elementId : '" + getId() + "',");
		script.append("widgetType : 'dijit.form.CheckBox',");
		script.append("widgetAttrs : {");

		script.append("required : " + ExpressionEvaluationUtils.evaluateBoolean("required", getRequired(), this.pageContext));

		script.append("}}));");
		TagUtils.getCurrentForm(pageContext).addScript(script);
	}
	
	@Override
	protected String resolveId() throws JspException {
		String result = super.resolveId();
		setId(result);
		return result;
	}

	@Override
	public void doFinally() {
		setReadonly(false);
		setRequired("false");
		setTabindex(null);
		setCssClass(null);
		setDisabled(false);
		setForceEdit(null);
		setDisableDojo(null);
		setProtectIfPageStatusIs(null);
		setProtectIfPageStatusIsNot(null);
		setId(null);
		super.doFinally();
	}

}
