/**
 * 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.util.ArrayList;
import java.util.Date;

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.faces.FacesMessages;
import org.richfaces.event.UploadEvent;
import org.richfaces.model.UploadItem;
import org.richfaces.realworld.domain.Image;
import org.richfaces.realworld.domain.User;
import org.richfaces.realworld.tree.TreeActionManager;
import org.richfaces.realworld.tree.TreeSelectionManager;

/**
 * @author $Autor$
 *
 */
@Name("fileUploadBean")
@Scope(ScopeType.SESSION)
@Synchronized(timeout=2000)
public class FileUploadBean {
	
	@In @Out
	private User user;
	
	@In 
	TreeSelectionManager treeSelectionManager;
	
	@In(create=true) 
	TreeActionManager treeActionManager;
	
	@In(create=true)
	private FileManager fileManager;
	
	@In FacesMessages facesMessages;
	
	private ArrayList<FileItem> files = new ArrayList<FileItem>();
	private boolean autoUpload = false;
	public int getSize() {
		if (getFiles().size()>0){
			return getFiles().size();
		}else 
		{
			return 0;
		}
	}

	public synchronized void paint(OutputStream stream, Object object) throws IOException {
		stream.write(getFiles().get((Integer)object).getData());
	}
	
	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(treeSelectionManager.getSelectedAlbum().getName());
	    image.setAlbum(treeSelectionManager.getSelectedAlbum());
	    file.setImage(image);
	    files.add(file);
	}
	
	public void clearUploadData() {
		files.clear();
	}
	
	public void clearSelectedData() {
		for(int i = 0; i < files.size(); i++){
			if(files.get(i).isSelected()){
				files.remove(i);
			}
		}
	}
	
	public void storeSelectedData() {
		for(FileItem file:files){
			if(file.isSelected()){
				//Save file to disk
				String fileName = user.getLogin() + fileManager.getFileSeparator() + treeSelectionManager.getSelectedAlbum().getName() + fileManager.getFileSeparator() + file.getImage().getPath();
				try {
					fileManager.addImage(fileName , file.getData());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				treeActionManager.addImage(file.getImage());
			}
		}
	}
	
	public void storeAll() {
		for(FileItem file:files){
			//Save file to disk
			String fileName = user.getLogin() + fileManager.getFileSeparator() + treeSelectionManager.getSelectedAlbum().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
			treeActionManager.addImage(file.getImage());
		}
	}
	
	public long getTimeStamp(){
		return System.currentTimeMillis();
	}
	
	public ArrayList<FileItem> getFiles() {
		return files;
	}

	public void setFiles(ArrayList<FileItem> files) { 
		this.files = files;
	}

	public boolean isAutoUpload() {
		return autoUpload;
	}

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


