package de.danoeh.antennapod.adapter.itunes;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.List;

import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.MainActivity;

public class ItunesAdapter extends ArrayAdapter<ItunesAdapter.Podcast> {
    /**
     * Related Context
     */
    private final Context context;

    /**
     * List holding the podcasts found in the search
     */
    private final List<Podcast> data;

    /**
     * Constructor.
     *
     * @param context Related context
     * @param objects Search result
     */
    public ItunesAdapter(Context context, List<Podcast> objects) {
        super(context, 0, objects);
        this.data = objects;
        this.context = context;
    }

    /**
     * Updates the given ImageView with the image in the given Podcast's imageUrl
     */
    class FetchImageTask extends  AsyncTask<Void,Void,Bitmap>{
        /**
         * Current podcast
         */
        private final Podcast podcast;

        /**
         * ImageView to be updated
         */
        private final ImageView imageView;

        /**
         * Constructor
         *
         * @param podcast Podcast that has the image
         * @param imageView UI image to be updated
         */
        FetchImageTask(Podcast podcast, ImageView imageView){
            this.podcast = podcast;
            this.imageView = imageView;
        }

        //Get the image from the url
        @Override
        protected Bitmap doInBackground(Void... params) {
            HttpClient client = new DefaultHttpClient();
            HttpGet get = new HttpGet(podcast.imageUrl);
            try {
                HttpResponse response = client.execute(get);
                return BitmapFactory.decodeStream(response.getEntity().getContent());
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        //Set the background image for the podcast
        @Override
        protected void onPostExecute(Bitmap img) {
            super.onPostExecute(img);
            if(img!=null) {
                imageView.setImageBitmap(img);
            }
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //Current podcast
        Podcast podcast = data.get(position);

        //ViewHolder
        PodcastViewHolder viewHolder;

        //Resulting view
        View view;

        //Handle view holder stuff
        if(convertView == null) {
            view = ((MainActivity) context).getLayoutInflater()
                    .inflate(R.layout.itunes_podcast_listitem, parent, false);
            viewHolder = new PodcastViewHolder(view);
            view.setTag(viewHolder);
        } else {
            view = convertView;
            viewHolder = (PodcastViewHolder) view.getTag();
        }

        //Set the title
        viewHolder.titleView.setText(podcast.title);

        //Update the empty imageView with the image from the feed
        new FetchImageTask(podcast,viewHolder.coverView).execute();

        //Feed the grid view
        return view;
    }

    /**
     * View holder object for the GridView
     */
    class PodcastViewHolder {

        /**
         * ImageView holding the Podcast image
         */
        public final ImageView coverView;

        /**
         * TextView holding the Podcast title
         */
        public final TextView titleView;


        /**
         * Constructor
         * @param view GridView cell
         */
        PodcastViewHolder(View view){
            coverView = (ImageView) view.findViewById(R.id.imgvCover);
            titleView = (TextView) view.findViewById(R.id.txtvTitle);
        }
    }

    /**
     * Represents an individual podcast on the iTunes Store.
     */
    public static class Podcast { //TODO: Move this out eventually. Possibly to core.itunes.model

        /**
         * The name of the podcast
         */
        public final String title;

        /**
         * URL of the podcast image
         */
        public final String imageUrl;
        /**
         * URL of the podcast feed
         */
        public final String feedUrl;

        /**
         * Constructor.
         *
         * @param json object holding the podcast information
         * @throws JSONException
         */
        public Podcast(JSONObject json) throws JSONException {
            title = json.getString("collectionName");
            imageUrl = json.getString("artworkUrl100");
            feedUrl = json.getString("feedUrl");
        }
    }
}

@@ -75,108 +75,8 @@
 xt;%0A
-import android.graphics.Bitmap;%0Aimport android.graphics.BitmapFactory;%0Aimport android.os.AsyncTask;%0A
 impo
@@ -93,24 +93,24 @@
 .view.View;%0A
+
 import andro
@@ -241,181 +241,92 @@
 ort 
-org.apache.http.HttpResponse;%0Aimport org.apache.http.client.HttpClient;%0Aimport org.apache.http.client.methods.HttpGet;%0Aimport org.apache.http.impl.client.DefaultHttpClient
+com.bumptech.glide.Glide;%0Aimport com.bumptech.glide.load.engine.DiskCacheStrategy
 ;%0A
+%0A
 impo
@@ -385,36 +385,8 @@
 t;%0A%0A
-import java.io.IOException;%0A
 impo
@@ -1036,1455 +1036,8 @@
  %7D%0A%0A
-    /**%0A     * Updates the given ImageView with the image in the given Podcast's imageUrl%0A     */%0A    class FetchImageTask extends  AsyncTask%3CVoid,Void,Bitmap%3E%7B%0A        /**%0A         * Current podcast%0A         */%0A        private final Podcast podcast;%0A%0A        /**%0A         * ImageView to be updated%0A         */%0A        private final ImageView imageView;%0A%0A        /**%0A         * Constructor%0A         *%0A         * @param podcast Podcast that has the image%0A         * @param imageView UI image to be updated%0A         */%0A        FetchImageTask(Podcast podcast, ImageView imageView)%7B%0A            this.podcast = podcast;%0A            this.imageView = imageView;%0A        %7D%0A%0A        //Get the image from the url%0A        @Override%0A        protected Bitmap doInBackground(Void... params) %7B%0A            HttpClient client = new DefaultHttpClient();%0A            HttpGet get = new HttpGet(podcast.imageUrl);%0A            try %7B%0A                HttpResponse response = client.execute(get);%0A                return BitmapFactory.decodeStream(response.getEntity().getContent());%0A            %7D catch (IOException e) %7B%0A                e.printStackTrace();%0A            %7D%0A            return null;%0A        %7D%0A%0A        //Set the background image for the podcast%0A        @Override%0A        protected void onPostExecute(Bitmap img) %7B%0A            super.onPostExecute(img);%0A            if(img!=null) %7B%0A                imageView.setImageBitmap(img);%0A            %7D%0A        %7D%0A    %7D%0A%0A
     
@@ -1879,35 +1879,258 @@
     
-new FetchImageTask(podcast,
+Glide.with(context)%0A                .load(podcast.imageUrl)%0A                .placeholder(R.color.light_gray)%0A                .diskCacheStrategy(DiskCacheStrategy.NONE)%0A                .fitCenter()%0A                .dontAnimate()%0A                .into(
 view
@@ -2150,18 +2150,8 @@
 iew)
-.execute()
 ;%0A%0A 

Replace custom image loading with Glide