package com.group5.android.fd.helper;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

import android.os.AsyncTask;
import android.os.Environment;
import android.util.Log;

import com.group5.android.fd.FdConfig;

abstract public class ImageHelper extends AsyncTask<Void, Void, File> {

	protected static HashMap<String, File> m_cachedFiles = new HashMap<String, File>();
	// where to store in SD card
	final protected static File packageDirectory = new File(Environment
			.getExternalStorageDirectory().toString()
			+ "/Android/data/com.group5.android.fd/cache/");

	protected String imageUrl;

	// get image file from server and save in cache in SD card

	public ImageHelper(String imageUrl) {
		this.imageUrl = imageUrl;
	}

	@Override
	protected File doInBackground(Void... arg0) {
		File file = ImageHelper.getCachedFileUnchecked(imageUrl);

		if (file != null && file.exists() == false) {
			ImageHelper.packageDirectory.mkdirs();

			try {
				// receive image from server
				InputStream in = HttpHelper.getRaw(imageUrl);
				FileOutputStream out = new FileOutputStream(file);

				// create buffer
				byte[] buffer = new byte[4096];
				int bufferTemp = 0;
				// writting

				while ((bufferTemp = in.read(buffer)) > 0) {
					out.write(buffer, 0, bufferTemp);
				}

				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		ImageHelper.m_cachedFiles.put(imageUrl, file);
		return file;

	}

	@Override
	protected void onPostExecute(File cachedFile) {
		onSuccess(cachedFile);
	}

	abstract protected void onSuccess(File cachedFile);

	// get image name from url and create file
	protected static File getCachedFileUnchecked(String url) {
		if (url == null) {
			return null;
		}

		String[] parts = url.split("/");

		return new File(ImageHelper.packageDirectory, parts[parts.length - 1]);
	}

	public static File getCachedFile(String imageUrl) {
		if (imageUrl == null) {
			return null;
		}
		File cachedFile = ImageHelper.m_cachedFiles.get(imageUrl);

		if (cachedFile == null) {
			File file = ImageHelper.getCachedFileUnchecked(imageUrl);

			if (file.exists()) {
				cachedFile = file;

				ImageHelper.m_cachedFiles.put(imageUrl, file);
			}
		}

		return cachedFile;
	}

	public static void removeCachedFiles() {
		String[] files = ImageHelper.packageDirectory.list();
		File file;
		if (files != null) {
			for (int i = 0; i < files.length; i++) {
				file = new File(ImageHelper.packageDirectory, files[i]);
				file.delete();

				Log.d(FdConfig.DEBUG_TAG, files[i]);
			}
		}

		ImageHelper.m_cachedFiles.clear();
	}
}

@@ -1113,16 +1113,27 @@
 geUrl);%0A
+%09%09%09%09try %7B%0A%09
 %09%09%09%09File
@@ -1179,17 +1179,29 @@
 (file);%0A
-%0A
+%09%09%09%09%09try %7B%0A%09%09
 %09%09%09%09// c
@@ -1213,16 +1213,18 @@
  buffer%0A
+%09%09
 %09%09%09%09byte
@@ -1255,16 +1255,18 @@
 6%5D;%0A%09%09%09%09
+%09%09
 int buff
@@ -1281,16 +1281,18 @@
  0;%0A%09%09%09%09
+%09%09
 // writt
@@ -1296,16 +1296,18 @@
 itting%0A%0A
+%09%09
 %09%09%09%09whil
@@ -1348,24 +1348,26 @@
 %3E 0) %7B%0A%09%09%09%09%09
+%09
+%09
 out.write(bu
@@ -1396,27 +1396,93 @@
 %09%09%09%09
-%7D%0A%0A%09%09%09%09out.close();
+%09%09%7D%0A%09%09%09%09%09%7D finally %7B%0A%09%09%09%09%09%09out.close();%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D finally %7B%0A%09%09%09%09%09in.close();%0A%09%09%09%09%7D
 %0A%09%09%09

add finally block in ImageHelper