package acr.browser.lightning.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.support.annotation.NonNull;
import android.util.Log;
import android.widget.ImageView;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import acr.browser.lightning.constant.Constants;
import acr.browser.lightning.database.HistoryItem;

/**
 * Created by Stefano Pacifici on 25/08/15.
 */
public class DownloadImageTask extends AsyncTask<Void, Void, Bitmap> {

    final ImageView bmImage;
    final HistoryItem mWeb;
    final File mCacheDir;
    final String mUrl;
    final Bitmap mDefaultBitmap;

    public DownloadImageTask(@NonNull ImageView bmImage, @NonNull HistoryItem web,
                             @NonNull Bitmap defaultBitmap) {
        this.bmImage = bmImage;
        this.mWeb = web;
        this.mCacheDir = bmImage.getContext().getCacheDir();
        this.mUrl = web.getUrl();
        this.mDefaultBitmap = defaultBitmap;
    }

    protected Bitmap doInBackground(Void... params) {
        Bitmap mIcon = null;
        // unique path for each url that is bookmarked.
        final Uri uri = Uri.parse(mUrl);

        final String hash = String.valueOf(uri.getHost().hashCode());
        final File image = new File(mCacheDir, hash + ".png");
        final Uri urldisplay = Uri.fromParts(uri.getScheme(), uri.getHost(), "favicon.ico");
        // checks to see if the image exists
        if (!image.exists()) {
            FileOutputStream fos = null;
            InputStream in = null;
            try {
                // if not, download it...
                final URL urlDownload = new URL(urldisplay.toString());
                final HttpURLConnection connection = (HttpURLConnection) urlDownload.openConnection();
                connection.setDoInput(true);
                connection.connect();
                in = connection.getInputStream();

                if (in != null) {
                    mIcon = BitmapFactory.decodeStream(in);
                }
                // ...and cache it
                if (mIcon != null) {
                    fos = new FileOutputStream(image);
                    mIcon.compress(Bitmap.CompressFormat.PNG, 100, fos);
                    fos.flush();
                    Log.d(Constants.TAG, "Downloaded: " + urldisplay);
                }

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                Utils.close(in);
                Utils.close(fos);
            }
        } else {
            // if it exists, retrieve it from the cache
            mIcon = BitmapFactory.decodeFile(image.getPath());
        }
        if (mIcon == null) {
            InputStream in = null;
            FileOutputStream fos = null;
            try {
                // if not, download it...
                final URL urlDownload = new URL("https://www.google.com/s2/favicons?domain_url="
                        + uri.toString());
                final HttpURLConnection connection = (HttpURLConnection) urlDownload.openConnection();
                connection.setDoInput(true);
                connection.connect();
                in = connection.getInputStream();

                if (in != null) {
                    mIcon = BitmapFactory.decodeStream(in);
                }
                // ...and cache it
                if (mIcon != null) {
                    fos = new FileOutputStream(image);
                    mIcon.compress(Bitmap.CompressFormat.PNG, 100, fos);
                    fos.flush();
                }

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                Utils.close(in);
                Utils.close(fos);
            }
        }
        if (mIcon == null) {
            return mDefaultBitmap;
        } else {
            return mIcon;
        }
    }

    protected void onPostExecute(Bitmap result) {
        final Bitmap fav = Utils.padFavicon(result);
        bmImage.setImageBitmap(fav);
        mWeb.setBitmap(fav);
        // notifyBookmarkDataSetChanged();
    }
}

@@ -396,16 +396,61 @@
 t.URL;%0A%0A
+import acr.browser.lightning.app.BrowserApp;%0A
 import a
@@ -1037,19 +1037,25 @@
 r = 
-bmImage
+BrowserApp
 .get
+App
 Cont
@@ -1312,41 +1312,222 @@
     
-final Uri uri = Uri.parse(mUrl);%0A
+if (mUrl == null) %7B%0A            return mDefaultBitmap;%0A        %7D%0A        final Uri uri = Uri.parse(mUrl);%0A        if (uri.getHost() == null %7C%7C uri.getScheme() == null) %7B%0A            return mDefaultBitmap;%0A        %7D
 %0A   
@@ -1674,16 +1674,19 @@
 nal 
-Uri
+String
  url
-d
+D
 ispl
@@ -1694,22 +1694,8 @@
 y = 
-Uri.fromParts(
 uri.
@@ -1705,17 +1705,26 @@
 Scheme()
-,
+ + %22://%22 +
  uri.get
@@ -1729,19 +1729,21 @@
 etHost()
-,
+ +
  %22
+/
 favicon.
@@ -1746,17 +1746,16 @@
 con.ico%22
-)
 ;%0A      
@@ -2015,26 +2015,15 @@
 (url
-d
+D
 isplay
-.toString()
 );%0A 
@@ -2664,17 +2664,17 @@
  %22 + url
-d
+D
 isplay);

Fix broken icon downloading, handle edge cases where url parameter is bad