package com.sipai.jspatrol.Fault.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.sipai.jspatrol.Bean.Picture;
import com.sipai.jspatrol.R;

import java.util.List;

/**
 * Created by lvliming on 2019/3/25.
 */

public class PicAdapter extends RecyclerView.Adapter<PicAdapter.ViewHolder> {
    private List<Picture> list;
    private Context context;
    private PicAdapterOnClickListener mlistener;

    public PicAdapter(List<Picture> list, Context context, PicAdapterOnClickListener listener) {
        this.list = list;
        this.context = context;
        this.mlistener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_show_pic,parent,false);
        final ViewHolder viewHolder = new ViewHolder(view);
//        viewHolder.pic.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                int position = viewHolder.getAdapterPosition();
//                Intent intent = new Intent(context,PictureDetailActivity.class);
//                intent.putExtra("url",list.get(position).getAbspath());
//                context.startActivity(intent);
//            }
//        });
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Picture item = list.get(position);
        Glide.with(context).load(item.getAbspath()).into(holder.iv);
    }

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

    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        ImageView iv;
        View pic;
        public ViewHolder(View itemView) {
            super(itemView);
            pic = itemView;
            iv = itemView.findViewById(R.id.moudle_item_show_pic_iv);
            iv.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (mlistener!=null){
                mlistener.onItemClick(v,getAdapterPosition());
            }
        }
    }

    public interface PicAdapterOnClickListener{
        void onItemClick(View v,int position);
    }
}
