package org.richfaces.realworld.manager;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Observer;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.core.Events;
import org.richfaces.realworld.domain.Album;
import org.richfaces.realworld.domain.Image;
import org.richfaces.realworld.domain.MetaTag;
import org.richfaces.realworld.domain.Shelf;
import org.richfaces.realworld.domain.User;
import org.richfaces.realworld.service.Constants;

@Name("model")
@Scope(ScopeType.CONVERSATION)
@AutoCreate
public class Model {

private Integer selectedImageIndex = 1;
	
	private Image selectedImage;

	private Album selectedAlbum;
	
	private User selectedUser;
	
	private Shelf selectedShelf;
	
	private MetaTag selectedTag;
	
	private NavigationEnum mainArea;
	
	public void resetModel(NavigationEnum mainArea, User selectedUser, Shelf selectedShelf, Album selectedAlbum, Image selectedImage){
		this.setMainArea(mainArea);
		this.setSelectedAlbum(selectedAlbum);
		this.setSelectedImage(selectedImage);
		this.setSelectedShelf(selectedShelf);
		this.setSelectedUser(selectedUser);
	}
	
	@Observer(Constants.UPDATE_MAIN_AREA_EVENT)
	public void setMainArea(NavigationEnum mainArea) {
		if(this.mainArea != null && this.mainArea.equals(NavigationEnum.FILE_UPLOAD)){
			Events.instance().raiseEvent("clearFileUpload");
		}
		this.mainArea = mainArea;
		if(this.mainArea != null && this.mainArea.equals(NavigationEnum.FILE_UPLOAD)){
			if(getSelectedAlbum() == null){
				if(selectedUser != null && selectedUser.getShelfs().size() > 0 && selectedUser.getShelfs().get(0).getAlbums().size() > 0)
				setSelectedAlbum(selectedUser.getShelfs().get(0).getAlbums().get(0));
			}
		}
	}
	
	public Integer getSelectedImageIndex() {
		return selectedImageIndex;
	}

	public NavigationEnum getMainArea() {
		return mainArea;
	}
	
	public void setSelectedImageIndex(Integer selectedImageIndex) {
		this.selectedImageIndex = selectedImageIndex;
	}

	public Image getSelectedImage() {
		return selectedImage;
	}

	private void setSelectedImage(Image selectedImage) {
		this.selectedImage = selectedImage;
	}

	public Album getSelectedAlbum() {
		return selectedAlbum;
	}

	public void setSelectedAlbum(Album selectedAlbum) {
		this.selectedAlbum = selectedAlbum;
	}
	
	public User getSelectedUser() {
		return selectedUser;
	}

	private void setSelectedUser(User selectedUser) {
		this.selectedUser = selectedUser;
	}

	public Shelf getSelectedShelf() {
		return selectedShelf;
	}

	private void setSelectedShelf(Shelf selectedShelf) {
		this.selectedShelf = selectedShelf;
	}
	
	public MetaTag getSelectedTag() {
		return selectedTag;
	}
	
	@Observer("updateSelectedTag")
	public void setSelectedTag(MetaTag selectedTag) {
		this.selectedTag = selectedTag;
	}
}
