package com.tofly.ghrq_route.common.ui.adapter;

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

import androidx.appcompat.widget.AppCompatCheckedTextView;
import androidx.recyclerview.widget.RecyclerView;

import com.lcp.arecyclerview.widget.ARecyclerBaseAdapter;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.module.user.domain.CarBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Amu on 2017/5/22.
 */

public class CarListAdapter extends ARecyclerBaseAdapter<CarListAdapter.MyViewHolder, CarBean> {

    private int selection = 0;

    public CarListAdapter(Context context, List<CarBean> data) {
        super(context, data);
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View rootView = LayoutInflater.from(mContext).inflate(R.layout.danger_report_spinner_item, parent, false);
        MyViewHolder viewHolder = new MyViewHolder(rootView);
        return viewHolder;
    }

    public CarBean getSelectedItem(){
        CarBean item = null;
        if (selection >= 0 && selection < mList.size()){
            item = mList.get(selection);
        }
        return item;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        CarBean carBean = getItem(position);
        if (carBean != null) {
            holder.checkedTextView.setText(carBean.getPlate_number());
        }

        holder.checkedTextView.setChecked(false);
        if (selection == position){
            holder.checkedTextView.setChecked(true);
        }

        holder.checkedTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int postion = holder.getLayoutPosition();
                if (selection != position){
                    selection = position;
                    notifyDataSetChanged();
                }
            }
        });
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.text1)
        AppCompatCheckedTextView checkedTextView;

        MyViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
