package org.richfaces.realworld.fileupload;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

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

@Name("avatarUpload")
@Scope(ScopeType.PAGE)
public class AvatarUpload {
	
	@In(create=true)
	private FileManager fileManager;
	
	private FileItem avatarItem;
	
	private boolean autoUpload = false;
	
	public void avatarListener(UploadEvent event){
	    		UploadItem item = event.getUploadItem();
	    	    FileItem file = new FileItem();
	    	    file.setLength(item.getData().length);
	    	    Image image = new Image();
	    	    image.setName(item.getFileName());
	    	    file.setImage(image);
	    	    file.setData(item.getData());
	    	    this.setAvatarItem(file);
	}
	
	public synchronized void paintAvatar(OutputStream stream, Object object) throws IOException {
		stream.write(this.getAvatarItem().getData());
	}
	public boolean isAutoUpload() {
		return autoUpload;
	}
	
	/*public FileItem loadAvatar(User user){
		FileItem file = new FileItem();
	    file.setName(user.getAvatarPath());
	    File image = fileManager.getImage(user.getLogin() + fileManager.getFileSeparator() + "avatars"+ fileManager.getFileSeparator() + user.getAvatarPath());
	    InputStream paintData = null;
	    byte[] data = new byte[(int)image.length() - 1];
	    FileInputStream fileInputStream = null;
	    BufferedInputStream bufferedInputStream = null;
	    if (image != null) {
			try {
				fileInputStream = new FileInputStream(image);
				bufferedInputStream = new BufferedInputStream(fileInputStream);
				paintData = bufferedInputStream;
				paintData.read(data);
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				try {
					fileInputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				try {
					bufferedInputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				try {
					paintData.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
	    }
	    file.setData(data);
	    return file;
	}*/

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

	public FileItem getAvatarItem() {
		return avatarItem;
	}

	public void setAvatarItem(FileItem avatarItem) {
		this.avatarItem = avatarItem;
	}
}
