package org.richfaces.realworld.tree;

import java.util.Date;

import javax.faces.model.SelectItem;

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.realworld.domain.Album;
import org.richfaces.realworld.domain.Comment;
import org.richfaces.realworld.domain.Image;
import org.richfaces.realworld.domain.User;
import org.richfaces.realworld.fileupload.FileManager;
import org.richfaces.realworld.service.IAlbumAction;
import org.richfaces.realworld.service.IImageAction;

@Name("treeActionManager")
@Scope(ScopeType.PAGE)
public class TreeActionManager {
	
	@In
	private User user;
	
	@In(create=true, required=true)
	private IAlbumAction albumAction;
	
	@In(create=true, required=true)
	private IImageAction imageAction;
	
	@In(create=true, required=true) @Out
	private Album album;
	
	@In(create=true)
	FileManager fileManager;
	
	@In TreeRootItem treeRoot;
	
	@In(create=true, required=true)
	TreeSelectionManager treeSelectionManager;
	
	public void addAlbum(){
		albumAction.addAlbum(album);
		treeRoot.getAvailableAlbums().add(new SelectItem(album.getName()));
		TreeAlbumItem albumItem = new TreeAlbumItem(album.getId(), treeRoot, album);
		treeRoot.addAlbumToTree(albumItem);
		fileManager.addDirectory(user.getLogin() + fileManager.getFileSeparator() + album.getName());
		treeSelectionManager.setSelectedImage(null);
		treeSelectionManager.setSelectedAlbum(albumItem.getAlbum());
		
	}

	public void editAlbum(Album album){
		boolean nameChanged = !album.getName().equals(album.getChangedName());
		if(nameChanged){
			String directoryOld = user.getLogin() + fileManager.getFileSeparator() + album.getName();
			String directoryNew = user.getLogin() + fileManager.getFileSeparator() + album.getChangedName();
			try {
				fileManager.renameDirectory(directoryOld, directoryNew);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			album.setName(album.getChangedName());
			album.setChangedName(null);
		}
		albumAction.editAlbum(album);
		treeRoot.updateChild(album);
		
	}
	
	public void deleteAlbum(Album album){
		String albumName = album.getName();
		albumAction.deleteAlbum(album);
		SelectItem item = treeRoot.getSelectItemByName(album.getName());
		treeRoot.getAvailableAlbums().remove(item);
		treeRoot.removeChild(album.getId());
		fileManager.deleteDirectory(user.getLogin() + fileManager.getFileSeparator() + albumName);
		treeSelectionManager.setSelectedImage(null);
		treeSelectionManager.setSelectedAlbum(null);
	}
	
	public void deleteImage(Image image){
		String albumName = image.getAlbum().getName();
		String imagePath = image.getPath();
		TreeAlbumItem parent = treeRoot.getAlbums().get(image.getAlbum().getId());
		imageAction.deleteImage(image);
		parent.removeChild(image.getId());
		fileManager.deleteImage(user.getLogin() + fileManager.getFileSeparator() + albumName + fileManager.getFileSeparator() + imagePath);
		treeSelectionManager.setSelectedImage(null);
	}
	
	public void editImage(Image image){
		TreeAlbumItem parent = treeRoot.getAlbums().get(image.getAlbum().getId());
		boolean parentChanged = !parent.getAlbum().getName().equals(image.getAlbumName());
		imageAction.editImage(image, parentChanged);
		
		if(parentChanged){
			parent.removeChild(image.getId());
			TreeAlbumItem albumItem = treeRoot.getAlbumByName(image.getAlbumName());
			TreeImageItem imageItem = new TreeImageItem(image.getId(),albumItem,image);
			albumItem.addChild(image.getId(), imageItem);
			String fileNameOld = user.getLogin() + fileManager.getFileSeparator() + image.getAlbum().getName() + fileManager.getFileSeparator() + image.getPath();
			String fileNameNew = user.getLogin() + fileManager.getFileSeparator() + image.getAlbumName() + fileManager.getFileSeparator() + image.getPath();
			fileManager.renameImage(fileNameOld, fileNameNew);
			treeSelectionManager.setSelectedImage(imageItem.getImage());
			treeSelectionManager.setSelectedAlbum(albumItem.getAlbum());
		}else{
			TreeImageItem imageItem = (TreeImageItem)parent.getChild(image.getId());
			imageItem.setImage(image);
		}	
	}

	public void addImage(Image image) {
		imageAction.addImage(image);
		TreeAlbumItem albumItem = treeRoot.getAlbumByName(image.getAlbumName());
		TreeImageItem imageItem = new TreeImageItem(image.getId(),albumItem,image);
		albumItem.addChild(image.getId(), imageItem);
		treeSelectionManager.setSelectedImage(imageItem.getImage());
		treeSelectionManager.setSelectedAlbum(albumItem.getAlbum());
	}

	public void editComment(Comment comment) {
		imageAction.editComment(comment);
	}
	
	public void addComment(Comment comment) {
		imageAction.addComment(comment);
	}
	
	public void addAutoComment(User user, Image image) {
		Comment comment = new Comment();
		comment.setAuthor(user);
		comment.setDate(new Date());
		comment.setImage(image);
		comment.setMessage("+1");
		imageAction.addComment(comment);
	}
}
