package com.sharefaith.thesharefaithapp.base;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.widget.ImageView;

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

/**
 * Created by Anthony on 12/13/16.
 * Class for loading images from url
 */

public class SFImageLoaderFromURL
{
    public void loadImageFromURL( String url, ImageView imageView )
    {
        if( cancelPotentialLoadFromURL( url, imageView ) )
        {
            SFImageLoaderFromURL.BitmapLoaderFromURLTask task = new SFImageLoaderFromURL.BitmapLoaderFromURLTask( imageView );
            SFImageLoaderFromURL.LoadedFromURLDrawable loadedDrawable = new SFImageLoaderFromURL.LoadedFromURLDrawable( task );
            imageView.setImageDrawable( loadedDrawable );
            task.execute( url );
        }
    }

    private static boolean cancelPotentialLoadFromURL( String url, ImageView imageView )
    {
        SFImageLoaderFromURL.BitmapLoaderFromURLTask bitmapLoaderFromURLTask = getBitmapLoaderFromURLTask(imageView);

        if ( bitmapLoaderFromURLTask != null )
        {
            String bitmapPath = bitmapLoaderFromURLTask.url;
            if( ( bitmapPath == null ) || ( !bitmapPath.equals( url ) ) )
            {
                bitmapLoaderFromURLTask.cancel( true );
            }
            else
            {
                return false;
            }
        }

        return true;
    }

    private static SFImageLoaderFromURL.BitmapLoaderFromURLTask getBitmapLoaderFromURLTask( ImageView imageView )
    {
        if( imageView != null )
        {
            Drawable drawable = imageView.getDrawable();
            if( drawable instanceof SFImageLoaderFromURL.LoadedFromURLDrawable)
            {
                SFImageLoaderFromURL.LoadedFromURLDrawable loadedDrawable = (SFImageLoaderFromURL.LoadedFromURLDrawable) drawable;
                return  loadedDrawable.getBitmapLoaderFromURLTask();
            }
        }
        return null;
    }

    class BitmapLoaderFromURLTask extends AsyncTask<String, Void, Bitmap>
    {
        private String url;
        private final WeakReference<ImageView> imageViewReference;

        public BitmapLoaderFromURLTask(ImageView imageView) {
            imageViewReference = new WeakReference<ImageView>(imageView);
        }

        @Override
        // Actual load method, run in the task thread
        protected Bitmap doInBackground(String... urls) {
            // params comes from the execute() call: params[0] is the url.
            Bitmap returnBitmap = null;

            try
            {
                InputStream in = (InputStream) new URL(urls[0]).getContent();
                returnBitmap = BitmapFactory.decodeStream( in, null ,SFUtil.commonImgOpt() );
            } catch (IOException e)
            {
                e.printStackTrace();
            }
            return returnBitmap;
        }

        @Override
        // Once the image is downloaded, associates it to the imageView
        protected void onPostExecute(Bitmap bitmap) {
            if (isCancelled()) {
                bitmap = null;
            }

            if (imageViewReference != null) {
                ImageView imageView = imageViewReference.get();
                SFImageLoaderFromURL.BitmapLoaderFromURLTask bitmapLoaderTask = getBitmapLoaderFromURLTask( imageView );
                // Change bitmap only if this process is still associated with it
                if ( this == bitmapLoaderTask ) {
                    imageView.setImageBitmap(bitmap);
                }
            }
        }
    }

    static class LoadedFromURLDrawable extends Drawable
    {
        private final WeakReference<SFImageLoaderFromURL.BitmapLoaderFromURLTask> bitmapLoaderFromURLTaskReference;

        public LoadedFromURLDrawable( BitmapLoaderFromURLTask bitmapDownloaderFromURLTask) {
            super();
            bitmapLoaderFromURLTaskReference = new WeakReference<SFImageLoaderFromURL.BitmapLoaderFromURLTask>(bitmapDownloaderFromURLTask);
        }


        public SFImageLoaderFromURL.BitmapLoaderFromURLTask getBitmapLoaderFromURLTask() {
            return bitmapLoaderFromURLTaskReference.get();
        }

        @Override
        public void draw( Canvas canvas )
        {

        }

        @Override
        public void setAlpha( int alpha )
        {

        }

        @Override
        public void setColorFilter( ColorFilter colorFilter )
        {

        }

        @Override
        public int getOpacity()
        {
            return 0;
        }

    }
}
