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.lang.ref.WeakReference;

/**
 * Created by Anthony on 9/9/16.
 * Class for loading Images in the background to keep the UI from freezing
 */
public class SFImageLoader
{

    public void loadImage( String filePath, ImageView imageView )
    {
        if( cancelPotentialLoad( filePath, imageView ) )
        {
            BitmapLoaderTask task = new BitmapLoaderTask( imageView );
            LoadedDrawable loadedDrawable = new LoadedDrawable( task );
            imageView.setImageDrawable( loadedDrawable );
            task.execute( filePath );
        }
    }


    private static boolean cancelPotentialLoad( String filePath, ImageView imageView )
    {
        BitmapLoaderTask bitmapLoaderTask = getBitmapLoaderTask(imageView);

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

        return true;
    }


    private static BitmapLoaderTask getBitmapLoaderTask( ImageView imageView )
    {
        if( imageView != null )
        {
            Drawable drawable = imageView.getDrawable();
            if( drawable instanceof LoadedDrawable )
            {
                LoadedDrawable loadedDrawable = (LoadedDrawable) drawable;
                return  loadedDrawable.getBitmapLoaderTask();
            }
        }
        return null;
    }


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

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

        @Override
        // Actual load method, run in the task thread
        protected Bitmap doInBackground(String... params) {
            // params comes from the execute() call: params[0] is the url.
            return BitmapFactory.decodeFile( params[0] , SFUtil.commonImgOpt() );
        }

        @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();
                BitmapLoaderTask bitmapLoaderTask = getBitmapLoaderTask( imageView );
                // Change bitmap only if this process is still associated with it
                if ( this == bitmapLoaderTask ) {
                    imageView.setImageBitmap(bitmap);
                }
            }
        }
    }


    static class LoadedDrawable extends Drawable
    {
        private final WeakReference<BitmapLoaderTask> bitmapLoaderTaskReference;

        public LoadedDrawable(BitmapLoaderTask bitmapDownloaderTask) {
            super();
            bitmapLoaderTaskReference = new WeakReference<BitmapLoaderTask>(bitmapDownloaderTask);
        }


        public BitmapLoaderTask getBitmapLoaderTask() {
            return bitmapLoaderTaskReference.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;
        }
    }
}
