package com.udapsoft.modular.web.tags;

import java.util.Map;

public class SortTag extends TemplateBasedRequestContextAwareTag {

	private static final long serialVersionUID = -7665975447992822704L;
	private String id;
	private String searchId;
	private String label;
	private String order = "ascending";
	private String mark = "▲,▼";
	private static final String ASC = "ascending";
	private static final String DESC = "descending";

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

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

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

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

	public String getLabel() {
		return this.label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public String getOrder() {
		return this.order;
	}

	public void setOrder(String order) {
		this.order = order;
	}

	public String getMark() {
		return this.mark;
	}

	public void setMark(String mark) {
		this.mark = mark;
	}

	protected int doBuildModelForStartTag(Map model) throws Exception {
		evaluateStringAndSetModel(model, "id", this.pageContext);
		evaluateStringAndSetModel(model, "searchId", this.pageContext);
		evaluateStringAndSetModel(model, "label", this.pageContext);
		evaluateStringAndSetModel(model, "order", this.pageContext);
		evaluateStringAndSetModel(model, "mark", this.pageContext);

		String[] marks = this.mark.split(",");
		String markDesc;
		String markAsc;
		if (marks.length == 1) {
			markAsc = marks[0];
			markDesc = marks[0];
		} else {
			markAsc = marks[0];
			markDesc = marks[1];
		}
		Object obj = this.pageContext.getRequest().getAttribute(
				"modular.dao.order.key_" + this.id);
		if ((obj != null) && ((obj instanceof Boolean))) {
			Boolean isAsc = (Boolean) obj;
			model.put("mark", isAsc.booleanValue() ? markAsc : markDesc);
			model.put("order", isAsc.booleanValue() ? "descending"
					: "ascending");
		} else {
			boolean b = "ascending".equals(this.order);
			model.remove("mark");
			model.put("order", b ? "ascending" : "descending");
		}
		return 1;
	}

	protected int doBuildModelForEndTag(Map model) throws Exception {
		evaluateStringAndSetModel(model, "id", this.pageContext);
		evaluateStringAndSetModel(model, "searchId", this.pageContext);
		evaluateStringAndSetModel(model, "label", this.pageContext);
		evaluateStringAndSetModel(model, "order", this.pageContext);
		evaluateStringAndSetModel(model, "mark", this.pageContext);

		String[] marks = this.mark.split(",");
		String markDesc;
		String markAsc;
		if (marks.length == 1) {
			markAsc = marks[0];
			markDesc = marks[0];
		} else {
			markAsc = marks[0];
			markDesc = marks[1];
		}
		Object obj = this.pageContext.getRequest().getAttribute(
				"modular.dao.order.key_" + this.id);
		if ((obj != null) && ((obj instanceof Boolean))) {
			Boolean isAsc = (Boolean) obj;
			model.put("mark", isAsc.booleanValue() ? markAsc : markDesc);
			model.put("order", isAsc.booleanValue() ? "descending"
					: "ascending");
		} else {
			boolean b = "ascending".equals(this.order);
			model.remove("mark");
			model.put("order", b ? "ascending" : "descending");
		}
		return 6;
	}

}
