package com.oo.eye.adapter;

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

import com.app.baselibrary.commonUtil.CheckUtil;
import com.app.baselibrary.recycler.adapter.BaseRecyclerViewAdapter;
import com.app.baselibrary.recycler.itemholder.ItemViewHolder;
import com.oo.eye.R;
import com.oo.eye.bean.StudentBean;

import java.util.List;

/**
 * Date : 2018/8/28.
 * Author:  cxh
 */

public class SinglePeopleAdapter extends BaseRecyclerViewAdapter<StudentBean, ItemViewHolder> {
    private final LayoutInflater mInflater;

    private StudentBean lastSelectBean;

    public SinglePeopleAdapter(List<StudentBean> list, Context context) {
        super(list, context);
        if (!CheckUtil.isEmpty(mList)) {
            lastSelectBean = mList.get(0);
            lastSelectBean.setChick(true);
        }
        mInflater = LayoutInflater.from(mContext);
    }

    public StudentBean getLastSelectBean() {
        return lastSelectBean;
    }

    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ItemViewHolder(mInflater.inflate(R.layout.item_single_people, parent, false));
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, final int position) {
        final StudentBean studentBean = mList.get(position);
        TextView orderText = (TextView) holder.itemView.findViewById(R.id.test_order);
        orderText.setText(CheckUtil.isEmpty(studentBean.getNumber()) ? "--" : studentBean.getNumber());
        TextView groupText = (TextView) holder.itemView.findViewById(R.id.test_group);
        groupText.setText(studentBean.getClassname());
        TextView nameText = (TextView) holder.itemView.findViewById(R.id.test_name);
        nameText.setText(studentBean.getRealname());
        TextView birthdayText = (TextView) holder.itemView.findViewById(R.id.test_birthday);
        birthdayText.setText(CheckUtil.isEmpty(studentBean.getBirthday()) ? "--" : studentBean.getBirthday());
        ImageView imageView = (ImageView) holder.itemView.findViewById(R.id.chick_iv);
        imageView.setImageResource(studentBean.isChick() ? R.drawable.select1_sel : R.drawable.select1_nor);
        holder.itemView.findViewById(R.id.choice_lay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StudentBean bean = null;
                for (int i = 0; i < mList.size(); i++) {
                    if (position == i) {
                        bean = mList.get(i);
                        bean.setChick(true);
                    }
                }
                if (!CheckUtil.isNull(lastSelectBean) && !bean.getNumber().equals(lastSelectBean.getNumber())) {
                    lastSelectBean.setChick(false);
                }
                lastSelectBean = bean;
                notifyDataSetChanged();
            }
        });
    }


    @Override
    public int getItemCount() {
        return mList != null ? mList.size() : 0;
    }
}
