package com.danding.yang800.Adapter;

import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.GlideDrawableImageViewTarget;
import com.danding.yang800.DocumentActivity;
import com.danding.yang800.Entity.ThumbViewInfo;
import com.danding.yang800.R;
import com.danding.yang800.View.PreviewLayout;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.List;

/**
 * Created by Administrator on 2017/7/26.
 */
public class DocumentRecyAdapter extends RecyclerView.Adapter<DocumentRecyAdapter.ViewHolder> implements View.OnClickListener {
    private PreviewLayout mPreviewLayout;
    private DocumentActivity ctx;
    private List<ThumbViewInfo> infos;
    private FrameLayout mContentContainer;

    public DocumentRecyAdapter(DocumentActivity ctx, List<ThumbViewInfo> infos, FrameLayout mContentContainer) {
        super();
        this.ctx = ctx;
        this.infos = infos;
        this.mContentContainer = mContentContainer;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        // 创建一个View，简单起见直接使用系统提供的布局，就是一个TextView
        View view = LayoutInflater.from(ctx).inflate(R.layout.document_item, viewGroup, false);

        // 创建一个ViewHolder
        ViewHolder holder = new ViewHolder(view);

        holder.document_itemIv = (ImageView) view.findViewById(R.id.document_itemIv);
        holder.document_itemIv.setOnClickListener(this);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, int position) {
        // 绑定数据到ViewHolder上
        //将position保存在itemView的Tag中，以便点击时进行获取
        ThumbViewInfo info = infos.get(position);

        Glide.with(ctx)
                .load(info.getUrl())
                .error(R.drawable.huisebeijing)
                .into(new GlideDrawableImageViewTarget(viewHolder.document_itemIv) {
                    public void onResourceReady(GlideDrawable drawable, GlideAnimation anim) {
                        super.onResourceReady(drawable, anim);
                        viewHolder.document_itemIv.setImageDrawable(drawable);
                    }

                    @Override
                    public void onLoadFailed(Exception e, Drawable errorDrawable) {
                        viewHolder.document_itemIv.setOnClickListener(null);
                        viewHolder.document_itemIv.setImageDrawable(errorDrawable);
                    }
                });
        viewHolder.document_itemIv.setTag(R.id.recyItem_id, position);
    }

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

    @Override
    public void onClick(View v) {
        ctx.openImage((int) v.getTag(R.id.recyItem_id));
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView document_itemIv;

        public ViewHolder(View itemView) {
            super(itemView);
            AutoUtils.autoSize(itemView);
        }
    }
}