package com.udapsoft.modular.web.tags;

import java.util.Map;

import com.udapsoft.modular.core.util.StringUtils;

public class ControlTag extends TemplateBasedRequestContextAwareTag {

	private String id;
	private String searchId;
	private String tableId;
	private String command;
	private String url;
	private String eventHandler;
	private String chainControl;
	private String event = "click";
	private String successMessage;

	public String getId() {
		return this.id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getTableId() {
		return this.tableId;
	}

	public void setTableId(String targetViewId) {
		this.tableId = targetViewId;
	}

	public String getCommand() {
		return this.command;
	}

	public void setCommand(String command) {
		this.command = command;
	}

	public String getUrl() {
		return this.url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getEventHandler() {
		return this.eventHandler;
	}

	public void setEventHandler(String eventHandler) {
		this.eventHandler = eventHandler;
	}

	public String getSearchId() {
		return this.searchId;
	}

	public void setSearchId(String searchId) {
		this.searchId = searchId;
	}

	public String getChainControl() {
		return this.chainControl;
	}

	public void setChainControl(String searchButtonId) {
		this.chainControl = searchButtonId;
	}

	public String getEvent() {
		return this.event;
	}

	public void setEvent(String event) {
		this.event = event;
	}

	public String getSuccessMessage() {
		return this.successMessage;
	}

	public void setSuccessMessage(String successMessage) {
		this.successMessage = successMessage;
	}

	protected int doBuildModelForStartTag(Map model) throws Exception {
		evaluateStringAndSetModel(model, "id", this.pageContext);
		evaluateStringAndSetModel(model, "searchId", this.pageContext);
		evaluateStringAndSetModel(model, "tableId", this.pageContext);
		evaluateStringAndSetModel(model, "chainControl", this.pageContext);
		evaluateStringAndSetModel(model, "successMessage", this.pageContext);

		evaluateStringAndSetModel(model, "command", this.pageContext);
		evaluateStringAndSetModel(model, "url", this.pageContext);
		evaluateStringAndSetModel(model, "eventHandler", this.pageContext);
		evaluateStringAndSetModel(model, "event", this.pageContext);

		String command = (String) model.get("command");
		if ("search".equalsIgnoreCase(command)) {
			String output = (String) model.get("tableId");
			String[] outputs = null;
			if (StringUtils.hasText(output)) {
				outputs = output.split(",");
			} else {
				outputs = new String[0];
			}
			model.put("output", outputs);
		}
		return 1;
	}

	protected int doBuildModelForEndTag(Map model) throws Exception {
		evaluateStringAndSetModel(model, "id", this.pageContext);
		evaluateStringAndSetModel(model, "searchId", this.pageContext);
		evaluateStringAndSetModel(model, "tableId", this.pageContext);
		evaluateStringAndSetModel(model, "chainControl", this.pageContext);
		evaluateStringAndSetModel(model, "successMessage", this.pageContext);

		evaluateStringAndSetModel(model, "command", this.pageContext);
		evaluateStringAndSetModel(model, "url", this.pageContext);
		evaluateStringAndSetModel(model, "eventHandler", this.pageContext);
		evaluateStringAndSetModel(model, "event", this.pageContext);

		return 6;
	}

}
