package org.richfaces.realworld.manager;

import java.io.Serializable;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import org.richfaces.component.UITree;
import org.richfaces.realworld.domain.Album;
import org.richfaces.realworld.domain.Shelf;
import org.richfaces.realworld.domain.User;

@Name("treeManager")
@Scope(ScopeType.CONVERSATION)
public class TreeManager implements Serializable{

	@In 
	private User user;
	@In @Out Model model;
	private static final long serialVersionUID = 5656562187249324512L;
	
	@SuppressWarnings("unchecked")
	public Boolean adviseNodeSelected(UITree tree) {
		Object currentNode = tree.getRowData();
		if (currentNode instanceof Album) {
			boolean selected = currentNode == model.getSelectedAlbum();
			return selected;
		}else if(currentNode instanceof Shelf){
			return currentNode == model.getSelectedShelf() && model.getSelectedAlbum() == null;
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public Boolean adviseNodeOpened(UITree tree) {
		Object currentNode = tree.getRowData();
		if (currentNode instanceof Album) {
			boolean selected = currentNode == model.getSelectedAlbum();
			return selected;
		}else if(currentNode instanceof Shelf){
			return currentNode == model.getSelectedShelf();
		}
		return null;
	}
}
