package com.zztrapp.zztrapp.ui.map.adapter;

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

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.zztrapp.zztrapp.R;
import com.zztrapp.zztrapp.bean.SelectRvItemBean;
import com.zztrapp.zztrapp.databinding.ItemRvSelectItemMapBinding;

import java.util.List;

/**
 * Describe 地图页-rv选项
 * Created by Bby on 2022/6/22
 * Email bby0856@163.com
 */

public class SelectRvItemAdapter extends RecyclerView.Adapter<SelectRvItemAdapter.ItemHolder> {

    private Context mContext;
    private OnItemClickListener onItemClickListener;
    private List<SelectRvItemBean> list;

    public interface OnItemClickListener {
        void onItemClick(int type, int childType, int position);
    }

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


    public SelectRvItemAdapter(Context context) {
        this.mContext = context;
    }

    public void setData(List<SelectRvItemBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemRvSelectItemMapBinding binding = ItemRvSelectItemMapBinding.inflate(LayoutInflater.from(mContext), parent, false);
        return new ItemHolder(binding);
    }

    @Override
    public void onBindViewHolder(ItemHolder holder, @SuppressLint("RecyclerView") int position) {
        holder.bindings.name.setText(list.get(position).getName());
        if (list.get(position).isSelect()) {
            holder.bindings.item.setBackgroundResource(R.drawable.shape_e7f3ff_stroke_007aff_14);
            holder.bindings.name.setTextColor(mContext.getResources().getColor(R.color.color007AFF));
        } else {
            holder.bindings.item.setBackgroundResource(R.drawable.shape_eeeeee_14);
            holder.bindings.name.setTextColor(mContext.getResources().getColor(R.color.color333333));
        }
        holder.bindings.item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(list.get(position).getType(), list.get(position).getChildType(), position);
                }
            }
        });
    }

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

    static class ItemHolder extends RecyclerView.ViewHolder {
        private ItemRvSelectItemMapBinding bindings;

        public ItemHolder(ItemRvSelectItemMapBinding binding) {
            super(binding.getRoot());
            bindings = binding;
        }
    }
}