package com.tjzhxx.union.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.tjzhxx.union.R;
import com.tjzhxx.union.custom_view.RoundImageView;
import com.tjzhxx.union.entity.WelfareResponse;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 描述:
 *
 * @author yanghuaxiong
 * @create 2018/5/3 下午4:40
 */
public class WelfareAdapter extends RecyclerView.Adapter<WelfareAdapter.ViewHolder> {

    Context mContext;
    List<WelfareResponse> mList;
    OnItemClickListener mOnItemClickListener;


    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public WelfareAdapter(Context mContext, List<WelfareResponse> list) {
        this.mContext = mContext;
        this.mList = (list == null ? new ArrayList<WelfareResponse>() : list);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_welfare, parent, false);
        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnItemClickListener != null)
                    mOnItemClickListener.onItemClick(view, (WelfareResponse) view.getTag());
            }
        });
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        WelfareResponse bean = mList.get(position);
        holder.itemView.setTag(bean);
        Glide.with(mContext)
                .load(bean.getWfinfo().getPicurl())
                .placeholder(R.mipmap.bg_placeholder)
                .error(R.mipmap.bg_placeholder)
                .into(holder.image);
        holder.title.setText(bean.getWfinfo().getTitle());
        holder.descrble.setText(bean.getWfinfo().getDisstr());
        if (bean.getStatus() == 1){
            holder.item.setBackgroundResource(R.mipmap.icon_get_welfare);
        }else {
            holder.item.setBackgroundResource(R.mipmap.icon_noget_welfare);
        }
    }

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

    public static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.image)
        RoundImageView image;
        @BindView(R.id.title)
        TextView title;
        @BindView(R.id.descrble)
        TextView descrble;
        @BindView(R.id.item)
        LinearLayout item;


        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, WelfareResponse entity);
    }

}
