package com.sinyuk.yuk.ui.feeds;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.DrawableRequestBuilder;
import com.bumptech.glide.Priority;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.sinyuk.yuk.R;
import com.sinyuk.yuk.data.shot.Shot;
import com.sinyuk.yuk.utils.glide.CropCircleTransformation;

import java.util.ArrayList;

import butterknife.ButterKnife;
import timber.log.Timber;

/**
 * Created by Sinyuk on 16/7/6.
 */
public class FeedsAdapter extends RecyclerView.Adapter<FeedsAdapter.FeedItemViewHolder> {
    private final DrawableRequestBuilder<String> avatarBuilder;
    private final DrawableRequestBuilder<String> shotBuilder;
    private final DrawableRequestBuilder<String> PNGBuilder;

    private Context mContext;
    private ArrayList<Shot> mDataSet = new ArrayList<>();
    private boolean mAutoPlayGif = false;


    public FeedsAdapter(Context context, RequestManager requestManager, ArrayList<Shot> dataSet) {
        Timber.tag("FeedsAdapter");
        this.mContext = context;

        this.shotBuilder = requestManager
                .fromString()
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .dontAnimate()
                .centerCrop();

        this.PNGBuilder = requestManager
                .fromString()
                .diskCacheStrategy(DiskCacheStrategy.RESULT)
                .dontAnimate()
                .centerCrop();

        this.mDataSet = dataSet;

        avatarBuilder = requestManager.fromString()
                .priority(Priority.NORMAL)
                .diskCacheStrategy(DiskCacheStrategy.RESULT)
                .centerCrop()
                .dontAnimate()
                .bitmapTransform(new CropCircleTransformation(mContext));
    }


    @Override
    public FeedItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new FeedItemViewHolder((FeedItemView) View.inflate(mContext, R.layout.feed_list_item, null));
    }

    @Override
    public int getItemCount() {
        return mDataSet.size();
    }

    @Override
    public void onBindViewHolder(FeedItemViewHolder holder, int position) {
        final Shot data = mDataSet.get(position);
        if (data.isAnimated()) {
            holder.bindTo(data, shotBuilder, mAutoPlayGif, avatarBuilder);
        }else {
            holder.bindTo(data, PNGBuilder, mAutoPlayGif, avatarBuilder);
        }
    }



    public void setAutoPlayGif(boolean autoPlayGif) {
        this.mAutoPlayGif = autoPlayGif;
    }


    public class FeedItemViewHolder extends RecyclerView.ViewHolder {
        private final FeedItemView itemView;

        public FeedItemViewHolder(FeedItemView itemView) {
            super(itemView);
            this.itemView = itemView;
        }

        public void bindTo(Shot data,
                           DrawableRequestBuilder<String> shotBuilder,
                           boolean isAutoPlayGif,
                           DrawableRequestBuilder<String> avatarBuilder) {
            itemView.bindTo(data, shotBuilder, isAutoPlayGif, avatarBuilder);
        }

    }
}

@@ -126,20 +126,30 @@
 id.view.
-View
+LayoutInflater
 ;%0Aimport
@@ -516,40 +516,8 @@
 t;%0A%0A
-import butterknife.ButterKnife;%0A
 impo
@@ -575,16 +575,29 @@
 16/7/6.%0A
+ * %E5%B0%BD%E9%87%8F%E5%87%8F%E5%B0%91%E8%BF%99%E9%87%8C%E7%9A%84%E9%80%BB%E8%BE%91%0A
  */%0Apubl
@@ -791,28 +791,27 @@
 der%3CString%3E 
-shot
+GIF
 Builder;%0A   
@@ -873,38 +873,8 @@
 r;%0A%0A
-    private Context mContext;%0A
     
@@ -947,17 +947,18 @@
 boolean 
-m
+is
 AutoPlay
@@ -970,18 +970,16 @@
  false;%0A
-%0A%0A
     publ
@@ -1122,46 +1122,11 @@
 his.
-mContext = context;%0A%0A        this.shot
+GIF
 Buil
@@ -1808,18 +1808,17 @@
 rmation(
-mC
+c
 ontext))
@@ -1981,31 +1981,74 @@
 ew) 
-View.inflate(mContext, 
+LayoutInflater.from(parent.getContext())%0A                .inflate(
 R.la
@@ -2072,12 +2072,21 @@
 em, 
-null
+parent, false
 ));%0A
@@ -2374,36 +2374,35 @@
 er.bindTo(data, 
-shot
+GIF
 Builder, mAutoPl
@@ -2386,33 +2386,34 @@
 ta, GIFBuilder, 
-m
+is
 AutoPlayGif, ava
@@ -2434,16 +2434,17 @@
        %7D
+ 
 else %7B%0A 
@@ -2486,17 +2486,18 @@
 uilder, 
-m
+is
 AutoPlay
@@ -2535,17 +2535,16 @@
     %7D%0A%0A%0A
-%0A
     publ
@@ -2602,17 +2602,18 @@
    this.
-m
+is
 AutoPlay

Remove context flied which is unnecessary