package com.philisense.rdgo.adapter;

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

import com.philisense.rdgo.R;
import com.philisense.rdgo.bean.response.ProductBean;
import com.philisense.rdgo.databinding.ItemProductBinding;

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

/**
 * Created by shaop on 2018/1/5.
 */

public class ProductAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<ProductBean.ListBean> dataArray;
    private Context context;

    public ProductAdapter(Context context) {
        this.context = context;
        dataArray = new ArrayList<>();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemProductBinding mBindHeader = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_product, parent, false);
        return new ViewHolder(mBindHeader.getRoot());
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHolder lineViewHolder = (ViewHolder) holder;
        lineViewHolder.bindView(dataArray.get(position));

        lineViewHolder.itemProductBinding.num.setText(dataArray.get(position).getBottleNum()+"");
        int gene=dataArray.get(position).getGene();
        if(gene==0){
            lineViewHolder.itemProductBinding.Gene.setText("是");
        }else if(gene==1){
            lineViewHolder.itemProductBinding.Gene.setText("否");
        }else {
            lineViewHolder.itemProductBinding.Gene.setText("其他");
        }

        lineViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                monItemClickListener.btnOnClick(dataArray.get(position), position);
            }
        });

    }



    public interface OnItemClickListener {
        void btnOnClick(ProductBean.ListBean s, int postion);
    }

    public OnItemClickListener monItemClickListener;

    public void setOnClickListener(OnItemClickListener onClickListener) {
        this.monItemClickListener = onClickListener;
    }
    @Override
    public int getItemCount() {
        return dataArray.size();
    }

    /**
     * 添加数据
     *
     * @param data 数据
     */
    public void insertData(List<String> data) {

    }

    /**
     * 初始化数据
     *
     * @param data 数据
     */
    public void setData(List<ProductBean.ListBean> data) {
        dataArray = data;
        notifyDataSetChanged();
    }

    private class ViewHolder extends RecyclerView.ViewHolder {

        ItemProductBinding itemProductBinding;

        ViewHolder(View view) {
            super(view);
            itemProductBinding = DataBindingUtil.getBinding(view);
        }

        public void bindView(ProductBean.ListBean dataBean) {
            itemProductBinding.setProduct(dataBean);
        }
    }
}

