package com.lloydm.geosword.common;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;
import java.net.URLConnection;

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

public class GetFileFromWeb
{
     // constants
     private final static String TAG = "com.lloydm.geosword.common.GetFileFromWeb";

     private static final int DOWNLOAD_BUFFER_SIZE = 8192;

     public static boolean GetFile(String replay_name, String Url)
     {
                     // replay_name is the file on the local machine.
                     // url is obviously the remote location....

                     URL url;
                     URLConnection conn;
                     String folder;
                     BufferedInputStream inStream = null;
                     BufferedOutputStream outStream = null;
                     FileOutputStream fileStream = null;
                     File mDownloaded = null;
                     boolean success = false;
                     try
                     {
                                     Log.i(TAG, "START DOWNLOAD");
                                     url = new URL(Url);
                                     conn = url.openConnection();
                                     conn.setUseCaches(false);
                                     conn.setConnectTimeout(5000);
                                     conn.setReadTimeout(5000);
                                     Log.i(TAG, "D1");
                                     // get the filename
                                     folder = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + Config.DOWNLOADEDREPLAYFOLDER;
                                     if (!folder.endsWith("/"))
                                     {
                                                     folder = folder + "/";
                                     }
                                     Log.i(TAG, "D2");
                                     // start download
                                     inStream = new BufferedInputStream(conn.getInputStream());
                                     Log.i(TAG, "D3:" + folder);
                                     File dir = new File(folder);
                                     Log.i(TAG, "D4");
                                     if (!dir.exists())
                                     {
                                                     dir.mkdirs();
                                     }
                                     if (dir.isDirectory())
                                     {
                                                     Log.i(TAG, "directory shuold exist");
                                     }
                                     Log.i(TAG, "D5");
                                     Log.i(TAG, dir.getAbsolutePath() + "/" + replay_name);
                                     mDownloaded = new File(dir.getAbsolutePath() + "/" + replay_name);
                                     if (mDownloaded.exists())
                                     {
                                                     mDownloaded.delete();
                                     }
                                     Log.i(TAG, "D6");
                                     fileStream = new FileOutputStream(mDownloaded);
                                     Log.i(TAG, "D7");
                                     outStream = new BufferedOutputStream(fileStream, DOWNLOAD_BUFFER_SIZE);
                                     Log.i(TAG, "D8");
                                     byte[] data = new byte[DOWNLOAD_BUFFER_SIZE];
                                     int bytesRead = 0;
                                     while ((bytesRead = inStream.read(data, 0, data.length)) >= 0)
                                     {
                                                     outStream.write(data, 0, bytesRead);
                                     }
                                     Log.i(TAG, "D10");
                     }
                     catch (Exception e)
                     {
                                     Log.e(TAG, "Exception downloading file");
                                     if (e.getMessage() != null)
                                     {
                                                     Log.e(TAG, e.getMessage());
                                     }

                     }
                     finally
                     {
                                     success = true;
                                     try
                                     {
                                                     outStream.close();
                                     }
                                     catch (Exception e)
                                     {
                                                     // TODO Auto-generated catch block
                                                     e.printStackTrace();
                                                     success = false;
                                     }
                                     try
                                     {
                                                     fileStream.close();
                                     }
                                     catch (Exception e)
                                     {
                                                     // TODO Auto-generated catch block
                                                     e.printStackTrace();
                                                     success = false;
                                     }
                                     try
                                     {
                                                     inStream.close();
                                     }
                                     catch (Exception e)
                                     {
                                                     // TODO Auto-generated catch block
                                                     e.printStackTrace();
                                                     success = false;
                                     }
                     }
                     return success;
     }
}