package com.loosli.christian.popularmovieapp.android.app;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.loosli.christian.popularmovieapp.android.app.entity.Movie;
import com.loosli.christian.popularmovieapp.android.app.util.Util;
import com.squareup.picasso.Picasso;

import java.util.Collection;
import java.util.List;

/**
 * Created by ChristianL on 25.11.15.
 */
public class MoviesAdapter extends BaseAdapter {

    private final Activity mContext;
    private final List<Movie> mMovies;
    //private final int mHeight;
    private final int mWidth;

    public MoviesAdapter(Activity context, List<Movie> movies) {
        mContext = context;
        mMovies = movies;
        //mHeight = Math.round(mContext.getResources().getDimension(R.dimen.poster_height));
        mWidth = Math.round(Util.getScreenWidth(mContext)/2);
        //TODO: check connectivity speed
    }

    public void addAll(Collection<Movie> xs) {
        mMovies.addAll(xs);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mMovies.size();
    }

    @Override
    public Movie getItem(int position) {
        if (position >= 0 && position < mMovies.size()) {
            return mMovies.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Movie movie = getItem(position);
        if (movie == null) {
            return null;
        }

        ImageView imageView;
        if (convertView == null) {
            // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            //imageView.setLayoutParams(new GridView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            //imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            //imageView.setScaleType(ImageView.ScaleType.FIT_START);
            imageView.setAdjustViewBounds(true);
            imageView.setPadding(0, 0, 0, 0);
        } else {
            imageView = (ImageView) convertView;
        }

        Picasso.with(mContext)
                .load(Util.buildPosterUrl(movie.getPosterPath(), mWidth))
                .placeholder(R.drawable.empty_photo)
                .into(imageView);

        return imageView;
    }

    public void clearData() {
        mMovies.clear();
    }

    public List<Movie> getItems() {
        return mMovies;
    }
}

@@ -531,16 +531,95 @@
 pter %7B%0A%0A
+    private static final String LOG_TAG = MoviesAdapter.class.getSimpleName();%0A
     priv
@@ -958,24 +958,190 @@
 r_height));%0A
+        // TODO set height with a ratio from width%0A        //FIXME this works not if gridview set the column_width to 150dp, in landscape mode the column number is 3%0A
         mWid
@@ -1185,17 +1185,19 @@
 Context)
-/
+ / 
 2);%0A    
@@ -1244,122 +1244,8 @@
  %7D%0A%0A
-    public void addAll(Collection%3CMovie%3E xs) %7B%0A        mMovies.addAll(xs);%0A        notifyDataSetChanged();%0A    %7D%0A%0A
     
@@ -2645,32 +2645,146 @@
 ageView;%0A    %7D%0A%0A
+    public void addAll(Collection%3CMovie%3E xs) %7B%0A        mMovies.addAll(xs);%0A        notifyDataSetChanged();%0A    %7D%0A%0A
     public void 

add todo message: fix column width