package com.sharefaith.thesharefaithapp.adapters;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.view.animation.AnimationUtils;

import com.sharefaith.thesharefaithapp.R;

/**
 * Asynctask for the post/newsletter items. Shows items before load and that's not great though.
 * Created by Luke on 2/16/2015.
 * @deprecated
 */
public class ImgDecodeTask extends AsyncTask<String, Void, Bitmap> {

	private ViewHolder holder;
    private static LimitedMapCache<String, Bitmap> BitMapCache;

	public ImgDecodeTask(ViewHolder h)
    {
		this.holder = h;
        if (this.BitMapCache == null)
        {
            this.BitMapCache = new LimitedMapCache<String, Bitmap>( 10);
        }
	}

	protected Bitmap doInBackground(String... params) {

        Bitmap bitmap = this.BitMapCache.get(params[0]);
        if (bitmap == null) {
			long start = System.currentTimeMillis();
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 2;
            options.inScaled = false;

            if (isCancelled()) {
                return bitmap;
            }

            bitmap = BitmapFactory.decodeFile(params[0], options);
            this.BitMapCache.add(params[0], bitmap);
        } else {
            Log.w("SFAPP", "CACHED");
        }

		/*BitmapFactory.decodeFile(params[0], options);
		while(opt.outHeight > MaxTextureSize || opt.outWidth > MaxTextureSize) {
			opt.inSampleSize++;
			BitmapFactory.decodeFile(params[0], opt);
		}
		opt.inJustDecodeBounds = false;
*/
		return bitmap;
	}

	@Override
	protected void onPostExecute(Bitmap result) {
		if(this.holder != null) {
			holder.dateView.setVisibility(View.INVISIBLE);
			holder.imageView.startAnimation( AnimationUtils.loadAnimation(holder.imageView.getContext(), R.anim.sf_fade_in ) );
			holder.imageView.setImageBitmap(result);
		}
	}

}
