/**
 * 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 org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.core.Events;
import org.richfaces.component.Dropzone;
import org.richfaces.event.DropEvent;
import org.richfaces.event.DropListener;
import org.richfaces.realworld.domain.Album;
import org.richfaces.realworld.domain.Image;
import org.richfaces.realworld.domain.Shelf;
import org.richfaces.realworld.domain.User;
import org.richfaces.realworld.service.Constants;
import org.richfaces.realworld.service.FlushStrategy;
import org.richfaces.realworld.service.IAlbumAction;

@Name("dndManager")
public class DnDManager implements DropListener {

	@In User user;
	@In FileManager fileManager;
	@In IAlbumAction albumAction;
	
	public void processDrop(DropEvent dropEvent) {
		Dropzone dropzone = (Dropzone) dropEvent.getComponent();
		Object dragValue = dropEvent.getDragValue();
		Object dropValue = dropzone.getDropValue();
		if(dragValue instanceof Image){
				if(!((Album)dropValue).getOwner().equals(user)){
					Events.instance().raiseEvent(Constants.ADD_ERROR_EVENT, new Exception("You can't add photo's to that album"));
					return;
				}
				handleImage((Image)dragValue, (Album)dropValue);	
		}else if(dragValue instanceof Album){
			handleAlbum((Album)dragValue, (Shelf)dropValue);
		}
	}

	private void handleAlbum(Album dragValue, Shelf dropValue) {
		dropValue.addAlbum(dragValue);
		albumAction.editAlbum(dragValue);
		Events.instance().raiseEvent("clearTree");
	}

	private void handleImage(Image dragValue, Album dropValue) {
		if(dragValue.getAlbum().equals(dropValue)){
			return;
		}
		String newPath = getNewPathOfImage(dragValue, dropValue);
		dragValue.setPath(newPath);
		dropValue.addImage(dragValue);
		albumAction.editAlbum(dropValue);
	}

	private String getNewPathOfImage(Image dragValue, Album dropValue) {
		String fileNameOld = dragValue.getPath();
		int lastIndexOf = dragValue.getPath().lastIndexOf("/");
		String prevPathEnd = dragValue.getPath().substring(lastIndexOf);
		String fileNameNew = user.getLogin()+"/"+dropValue.getId()+"/"+prevPathEnd;
		fileManager.renameImage(fileNameOld, fileNameNew);
		return fileNameNew;
	}
}
