package org.keyscience.BackgroundTask;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.text.Html;
import android.util.DisplayMetrics;
import android.widget.TextView;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class HttpDrawableAsync extends AsyncTask<String, Void, Drawable> {

    private WeakReference<TextView> taskTextView;
    private WeakReference<String> taskHtmlString;
    private WeakReference<Context> context;

    public HttpDrawableAsync(TextView v, String s, Context _context) {
        taskTextView = new WeakReference<>(v);
        taskHtmlString = new WeakReference<>(s);
        context = new WeakReference<>(_context);
    }

    @Override
    protected Drawable doInBackground(String... params) {

        Drawable drawable = null;

        URL sourceURL;

        try {
            sourceURL = new URL(params[0]);
            URLConnection urlConnection = sourceURL.openConnection();
            urlConnection.connect();
            InputStream inputStream = urlConnection.getInputStream();

            BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
            Bitmap bm = BitmapFactory.decodeStream(bufferedInputStream);

            int width = 500;
            int height = (bm.getHeight() * width / bm.getWidth());
            Bitmap scale = Bitmap.createScaledBitmap(bm, width, height, true);

            drawable = new BitmapDrawable(context.get().getResources(), scale);

            DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();

            int dwidth = (metrics.widthPixels * 90) / 100;

            int dheight = drawable.getIntrinsicHeight() * dwidth
                    / drawable.getIntrinsicWidth();

            int totalGapW = dwidth - scale.getWidth() - 200/*My Text view Padding*/;
            int totalGapH = dheight - scale.getHeight() - 200/*My Text view Padding*/;

            // convert Bitmap to Drawable
            drawable.setBounds(totalGapW /= 8, totalGapH /= 8, dwidth, dheight);

        } catch (MalformedURLException e) {
             e.printStackTrace();
        } catch (IOException e) {
             e.printStackTrace();
        }
        return drawable;
    }

    @Override

    protected void onPostExecute(Drawable result) {
        final Drawable taskDrawable = result;

        if (taskDrawable != null) {
            taskTextView.get().setText(Html.fromHtml(taskHtmlString.get(),
                    new Html.ImageGetter() {
                        @Override
                        public Drawable getDrawable(String source) {
                            return taskDrawable;
                        }
                    }, null));
        }
    }
}