/**
 * 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.AutoCreate;
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.core.Events;
import org.jboss.seam.faces.FacesMessages;
import org.richfaces.realworld.domain.Album;
import org.richfaces.realworld.domain.Shelf;
import org.richfaces.realworld.domain.User;
import org.richfaces.realworld.service.FlushStrategy;
import org.richfaces.realworld.service.IAlbumAction;

@Name("albumManager")
@Scope(ScopeType.CONVERSATION)
@AutoCreate
public class AlbumManager implements Serializable{

	private static final long serialVersionUID = 2631634926126857691L;

	private String oncomplete = ";";
	@In private IAlbumAction albumAction;
	
	@In(create=true, required=true) @Out
	private Album album;
	
	@In private User user;
	
	@In FileManager fileManager;
	@In FacesMessages facesMessages;
	@In @Out Model model;
	
	public void addAlbum(Album album){
		if(album.getShelf() == null){
			facesMessages.add("Shelf must be not-null", null);
			return;
		}
		albumAction.addAlbum(album);
		addDirectory();
		model.resetModel(model.getMainArea(), model.getSelectedUser(), model.getSelectedShelf(), album, null);
		Events.instance().raiseEvent("albumAdded");
		Events.instance().raiseEvent("clearTree");
		setOncomplete();
	}
	
	public void initEdit(Shelf shelf){
		album = new Album();
		album.setShelf(shelf);
	}

	public void editAlbum(Album album){
		albumAction.editAlbum(album);
		model.resetModel(NavigationEnum.ALBUM_PREVIEW, model.getSelectedUser(), model.getSelectedShelf(), album, null);
		Events.instance().raiseEvent("albumEdited");
	}
	
	public void deleteAlbum(Album album){
		//Update domain model
		String string = album.getId().toString();
		albumAction.deleteAlbum(album);
		deleteDirectory(string);
		model.resetModel(NavigationEnum.ALL_ALBUMS, model.getSelectedUser(), model.getSelectedShelf(), null, null);
		Events.instance().raiseEvent("albumDeleted");
		Events.instance().raiseEvent("clearTree");
	}
	
	public Long getCountUnvisitedImages(Album album){
		if(album.getCountUnvisitedImages() == 0){
			album.setUnvisitedImages(albumAction.getUnvisitedImages(album));
		}
		return album.getCountUnvisitedImages();
	}

	private void setOncomplete() {
		oncomplete = "$('albumModalPanel').component.hide()";
	}

	public String getOncomplete() {
		return oncomplete;
	}

	public void setOncomplete(String oncomplete) {
		this.oncomplete = oncomplete;
	}
	
	private void deleteDirectory(String albumName) {
		fileManager.deleteDirectory(user.getLogin(), albumName);
	}

	private void addDirectory() {
		fileManager.addDirectory(user.getLogin(), album.getId().toString());
	}
}
