/**
 * License Agreement.
 *
 *  JBoss RichFaces - Ajax4jsf Component Library
 *
 * Copyright (C) 2007  Exadel, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1 as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 */
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.jboss.seam.annotations.Synchronized;
import org.jboss.seam.core.Events;
import org.richfaces.event.DataScrollerEvent;
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("controller")
@Scope(ScopeType.CONVERSATION)
@Synchronized(timeout=200000)
public class Controller implements Serializable{

	private static final long serialVersionUID = 5656562187249324512L;

	@In("#{messages['acess_not_granted']}")
	private String HAVENT_ACCESS;
	
	@In @Out Model model;

	@In
	private User user;

	public void selectShelfs(){
		model.resetModel(NavigationEnum.ALL_SHELFS, user, null, null, null);
	}
	
	public void selectAlbums(){
		model.resetModel(NavigationEnum.ALL_ALBUMS, user, null, null, null);
	}
	
	public void selectImages(){
		model.resetModel(NavigationEnum.ALL_IMAGES, user, null, null, null);
	}
	
	public void startEditShelf(Shelf shelf){
		if(!canViewShelf(shelf)){
			pushEvent(Constants.ADD_ERROR_EVENT, new Exception(HAVENT_ACCESS));
			return;
		}
		model.resetModel(NavigationEnum.SHELF_EDIT, shelf.getOwner(), shelf, null, null);		
	}
	
	public void cancelEditShelf(Shelf shelf){
		model.setMainArea(NavigationEnum.SHELF_PREVIEW);
	}

	public void showAlbum(Album album){
		if(!canViewAlbum(album)){
			pushEvent(Constants.ADD_ERROR_EVENT, new Exception(HAVENT_ACCESS));
			return;
		}
		model.resetModel(NavigationEnum.ALBUM_PREVIEW, album.getOwner(), album.getShelf(), album, null);		
	}
	
	public void showImage(Image image){
		if(!canViewImage(image)){
			pushEvent(Constants.ADD_ERROR_EVENT, new Exception(HAVENT_ACCESS));
			return;
		}
		model.resetModel(NavigationEnum.ALBUM_IMAGE_PREVIEW, image.getAlbum().getOwner(), image.getAlbum().getShelf(), image.getAlbum(), image);
		model.setSelectedImageIndex(model.getSelectedAlbum().getIndex(model.getSelectedImage())+1);
		image.getAlbum().getShelf().visitImage(image);
		image.getAlbum().visitImage(image);
	}
	
	public void scroll(DataScrollerEvent e){
		Integer index = e.getPage();
		model.setSelectedImageIndex(index);
		Image image = model.getSelectedAlbum().getImages().get(index - 1);
		model.resetModel(NavigationEnum.ALBUM_IMAGE_PREVIEW, image.getAlbum().getOwner(), image.getAlbum().getShelf(), image.getAlbum(), image);
		image.getAlbum().visitImage(image);
		image.getAlbum().getShelf().visitImage(image);
	}
	
	public void startEditImage(Image image){
		if(!canViewImage(image)){
			pushEvent(Constants.ADD_ERROR_EVENT, new Exception(HAVENT_ACCESS));
			return;
		}
		model.setMainArea(NavigationEnum.ALBUM_IMAGE_EDIT);
	}
	
	public void cancelEditImage(Image image){
		model.setMainArea(NavigationEnum.ALBUM_IMAGE_PREVIEW);
	}
	
	public void showShelf(Shelf shelf){
		model.resetModel(NavigationEnum.SHELF_PREVIEW, shelf.getOwner(), shelf, null, null);
	}
	
	public boolean isUserImage(Image image){
		if(image == null || image.getAlbum() == null || image.getAlbum().getOwner() == null){
			return false;
		}
		return image.getAlbum().getOwner().getLogin().equals(user.getLogin());
	}
	
	public void startEditAlbum(Album album){
		if(!album.getOwner().equals(user)){
			pushEvent(Constants.ADD_ERROR_EVENT, new Exception(HAVENT_ACCESS));
			return;
		}
		model.setMainArea(NavigationEnum.ALBUM_EDIT);
	}
	
	public void cancelEditAlbum(Album album){
		model.setMainArea(NavigationEnum.ALBUM_PREVIEW);
	}
	
	public void showFileUpload(){
		model.setMainArea(NavigationEnum.FILE_UPLOAD);
	}	
	
	public void showSharedAlbums(User user){
		model.resetModel(NavigationEnum.USER_SHARED_ALBUMS, user, null, null, null);
	}
	
	public void showSharedImages(User user){
		model.resetModel(NavigationEnum.USER_SHARED_IMAGES, user, null, null, null);
	}
	
	public boolean isUserShelf(Shelf shelf){
		return shelf.getOwner()!=null && shelf.getOwner().getLogin().equals(user.getLogin());
	}
	
	public boolean isUserAlbum(Album album){
		if(album == null || album.getOwner() == null){
			return false;
		}
		return album.getOwner().getLogin().equals(user.getLogin());
	}
	
	public void showUser(User user){
		model.resetModel(NavigationEnum.USER_PREFS, user, null, null, null);
	}
	
	public void showUnvisitedImages(Shelf shelf){
		model.resetModel(NavigationEnum.SHELF_UNVISITED, shelf.getOwner(), shelf, shelf.getFirstAlbum(), null);
	}
	
	public void showUnvisitedImages(Album album){
		model.resetModel(NavigationEnum.ALBUM_UNVISITED, album.getShelf().getOwner(), album.getShelf(), album, null);
	}

	public void showTag(MetaTag metatag){
		model.resetModel(NavigationEnum.TAGS, model.getSelectedUser(), model.getSelectedShelf(), model.getSelectedAlbum(), model.getSelectedImage());
		model.setSelectedTag(metatag);
	}
	
	private boolean canViewShelf(Shelf shelf) {
		return shelf.getOwner().equals(user);
	}
	
	private boolean canViewImage(Image image) {
		return image.getAlbum().getShelf().isShared() || image.getAlbum().getOwner().equals(user);
	}
	
	private boolean canViewAlbum(Album album) {
		return album.getShelf().isShared() || album.getOwner().equals(user);
	}
	
	private void pushEvent(String type, Object... parameters) {
		Events.instance().raiseEvent(type, parameters);
	}
	
	public boolean isProfileEditable(User selectedUser){
		if(selectedUser == null){
			return false;
		}
		if(selectedUser == user){
			return true;
		}
		return false;
	}
}