package org.richfaces.realworld.util;

import java.awt.image.BufferedImage;
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 java.io.Serializable;

import javax.imageio.ImageIO;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.richfaces.realworld.domain.Image;
import org.richfaces.realworld.domain.User;
import org.richfaces.realworld.fileupload.FileManager;

@Name("imageLoader")
@Scope(ScopeType.CONVERSATION)
public class ImageLoader implements Serializable{

	@In(create=true)
	FileManager fileManager;
	
	public synchronized void paintSearchImage(OutputStream stream, Object object) throws IOException {
		Image painted = (Image)object;
		
		File image = fileManager.getImage(painted.getAlbum().getOwner().getLogin() + fileManager.getFileSeparator() + painted.getAlbum().getName()+ fileManager.getFileSeparator() + painted.getPath());
	    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();
				}
			}
	    }
		stream.write(data);
	}
	
	public synchronized void paint(OutputStream out, Object data) throws IOException{
		if (null == data) {
			return;
		}
		Image image = (Image)data;
		File imageResource = fileManager.getImage(image.getAlbum().getOwner().getLogin() + fileManager.getFileSeparator() + image.getAlbum().getName() + fileManager.getFileSeparator() + image.getPath());
		if (imageResource != null) {
			FileInputStream fileInputStream = new FileInputStream(imageResource);
			BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
			InputStream paintData = bufferedInputStream;
			if (null == paintData) {
				// throw new Exception("");
				// TODO
			}
			try{
			BufferedImage images = ImageIO.read(paintData);
			ImageIO.write(images, "jpeg", out);
			}catch(Exception e){
				
			}finally{
				fileInputStream.close();
				bufferedInputStream.close();
				paintData.close();
			}
		}
	}
	
	public synchronized void paintAvatar(OutputStream out, Object data) throws IOException{
		if (null == data) {
			return;
		}
		User user = (User)data;
		File imageResource = fileManager.getImage(user.getLogin() + fileManager.getFileSeparator() + "avatars" + fileManager.getFileSeparator() + user.getAvatarPath());
		if (imageResource != null) {
			FileInputStream fileInputStream = new FileInputStream(imageResource);
			BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
			InputStream paintData = bufferedInputStream;
			if (null == paintData) {
				// throw new Exception("");
				// TODO
			}
			try{
			BufferedImage image = ImageIO.read(paintData);
			ImageIO.write(image, "jpeg", out);
			}catch(Exception e){
				
			}finally{
				fileInputStream.close();
				bufferedInputStream.close();
				paintData.close();
			}
		}
	}
	
	public synchronized void paintFriendAvatar(OutputStream out, Object data) throws IOException{
		if (null == data) {
			return;
		}
		String temp = data.toString();
		String[] array = temp.split(":");
		File imageResource = fileManager.getImage(array[0] + fileManager.getFileSeparator() + "avatars" + fileManager.getFileSeparator() + array[1]);
		if (imageResource != null) {
			FileInputStream fileInputStream = new FileInputStream(imageResource);
			BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
			InputStream paintData = bufferedInputStream;
			if (null == paintData) {
				// throw new Exception("");
				// TODO
			}
			try{
			BufferedImage image = ImageIO.read(paintData);
			ImageIO.write(image, "jpeg", out);
			}catch(Exception e){
				
			}finally{
				fileInputStream.close();
				bufferedInputStream.close();
				paintData.close();
			}
		}
	}
}
