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

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class PageStatus implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3306868792504809971L;
	
	public static final PageStatus SEARCH_PAGE_STATUS = new PageStatus("SEARCH", false);
	public static final PageStatus LIST_PAGE_STATUS = new PageStatus("LIST", false);
	public static final PageStatus READ_PAGE_STATUS = new PageStatus("READ", true);
	public static final PageStatus NEW_PAGE_STATUS = new PageStatus("NEW", false);
	public static final PageStatus EDIT_PAGE_STATUS = new PageStatus("EDIT", false);
	public static final PageStatus DELETE_PAGE_STATUS = new PageStatus("DELETE", true);
	public static final PageStatus POPUP_PAGE_STATUS = new PageStatus("POPUP", true);

	public static final Map<String, PageStatus> map = new HashMap<String, PageStatus>();
	
	static {
		map.put("SEARCH", SEARCH_PAGE_STATUS);
		map.put("LIST", LIST_PAGE_STATUS);
		map.put("READ", READ_PAGE_STATUS);
		map.put("NEW", NEW_PAGE_STATUS);
		map.put("EDIT", EDIT_PAGE_STATUS);
		map.put("DELETE", DELETE_PAGE_STATUS);
		map.put("POPUP", POPUP_PAGE_STATUS);
	}
	
	private String name;
	private boolean readOnly;
	
	public PageStatus(String name, boolean readOnly) {
		super();
		this.name = name;
		this.readOnly = readOnly;
	}
	
	public static PageStatus forName(String name) {
		return map.get(name);
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public boolean isReadOnly() {
		return readOnly;
	}
	public void setReadOnly(boolean readOnly) {
		this.readOnly = readOnly;
	}
	
	public String toString() {
		return getName();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		
		String other = obj.toString();
		return getName().equalsIgnoreCase(other);
	}

	public boolean isSearchMode() {
		return SEARCH_PAGE_STATUS.equals(this);
	}

	public boolean isListMode() {
		return LIST_PAGE_STATUS.equals(this);
	}

	public boolean isReadMode() {
		return READ_PAGE_STATUS.equals(this);
	}

	public boolean isNewMode() {
		return NEW_PAGE_STATUS.equals(this);
	}

	public boolean isEditMode() {
		return EDIT_PAGE_STATUS.equals(this);
	}

	public boolean isDeleteMode() {
		return DELETE_PAGE_STATUS.equals(this);
	}

	public boolean isPopupMode() {
		return POPUP_PAGE_STATUS.equals(this);
	}

	public String getBoxStyle() {
		if (isDeleteMode()) {
			return "box-danger";
		}
		if (isNewMode()) {
			return "box-primary";
		}
		if (isEditMode()) {
			return "box-info";
		}
		return "box-normal";
	}
}
