/**
 * License Agreement.
 *
 * Rich Faces - Natural Ajax for Java Server Faces (JSF)
 *
 * 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.fileupload;

import java.io.IOException;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Begin;
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.jboss.seam.faces.FacesMessages;
import org.richfaces.event.UploadEvent;
import org.richfaces.model.UploadItem;
import org.richfaces.realworld.domain.Album;
import org.richfaces.realworld.domain.Image;
import org.richfaces.realworld.domain.User;
import org.richfaces.realworld.tree.TreeActionManager;
import org.richfaces.realworld.tree.TreeSelectionManager;
import org.richfaces.realworld.util.NavigationEnum;
import org.richfaces.realworld.util.SelectedImageIndex;

/**
 * @author $Autor$
 *
 */
@Name("fileUploadBean")
@Scope(ScopeType.CONVERSATION)

public class FileUploadBean implements Serializable{
	
	@In
	private User user;
	
	@In(create=true) @Out
	FileWrapper fileWrapper;

	@In(required=false) 
	private Album selectedAlbum;
	
	@In(create=true)
	private FileManager fileManager;
	
	private boolean autoUpload = false;

	public synchronized void listener(UploadEvent event) throws Exception{
	    UploadItem item = event.getUploadItem();
	    FileItem file = new FileItem();
	    file.setLength(item.getData().length);
	    file.setData(item.getData());
	    file.setSelected(false);
	    Image image = new Image();
	    image.setCreated(new Date());
	    image.setDescription("Sample Description");
	    image.setName("Sample Name");
	    image.setPath(item.getFileName());
	    image.setAlbumName(selectedAlbum.getName());
	    image.setAlbum(selectedAlbum);
	    file.setImage(image);
	    fileWrapper.getFiles().add(file);
	}
	
	public void clearUploadData() {
		fileWrapper.getFiles().clear();
	}
	
	public void clearSelectedData() {
		for(int i = 0; i < fileWrapper.getFiles().size(); i++){
			if(fileWrapper.getFiles().get(i).isSelected()){
				fileWrapper.getFiles().remove(i);
			}
		}
	}
	
	public void storeSelectedData() {
		List<FileItem> temp = new ArrayList<FileItem>();
		for(FileItem file:fileWrapper.getFiles()){
			if(file.isSelected()){
				//Save file to disk
				String fileName = user.getLogin() + fileManager.getFileSeparator() + selectedAlbum.getName() + fileManager.getFileSeparator() + file.getImage().getPath();
				try {
					fileManager.addImage(fileName , file.getData());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				Events.instance().raiseEvent("addImage", file.getImage());
			}else{
				temp.add(file);
			}
		}
		fileWrapper.getFiles().clear();
		fileWrapper.getFiles().addAll(temp);
	}
	
	public void storeAll() {
		for(FileItem file:fileWrapper.getFiles()){
			//Save file to disk
			String fileName = user.getLogin() + fileManager.getFileSeparator() + selectedAlbum.getName() + fileManager.getFileSeparator() + file.getImage().getPath();
			try {
				fileManager.addImage(fileName , file.getData());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//Update domain model
			Events.instance().raiseEvent("addImage", file.getImage());
		}
		fileWrapper.getFiles().clear();
	}
	
	public long getTimeStamp(){
		return System.currentTimeMillis();
	}

	public boolean isAutoUpload() {
		return autoUpload;
	}

	public void setAutoUpload(boolean autoUpload) {
		this.autoUpload = autoUpload;
	}
	
}