package aicare.net.cn.sdk.ailinksdkdemoandroid.modules.barometric_temp_humidity;

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

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

import java.util.ArrayList;

import aicare.net.cn.sdk.ailinksdkdemoandroid.R;

/**
 * 指令 adapter
 *
 * @author yesp
 */
public class InstructionAdapter extends RecyclerView.Adapter<InstructionAdapter.ViewHolder> {


    private Context context;
    private ArrayList<InstructionBean> list;

    private ItemClickListener itemClickListener;

    public InstructionAdapter(Context context, ArrayList<InstructionBean> list) {
        this.context = context;
        this.list = list;
    }

    public void setList(ArrayList<InstructionBean> list) {
        this.list = list;
    }

    public void setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ViewHolder holder = new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_instruction, parent, false));
        holder.itemView.setOnClickListener(v -> {
            if (itemClickListener != null) {
                int position = holder.getAdapterPosition();
                if (position < 0 || position >= list.size()) {
                    return;
                }
                itemClickListener.onItemClick(list.get(position));
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        InstructionBean bean = list.get(position);
        if (bean == null) {
            return;
        }
        String content = bean.getName();
        holder.tvContent.setText(content);
    }


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

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tvContent;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvContent = itemView.findViewById(R.id.tv_content);
        }
    }

    public interface ItemClickListener {

        /**
         *  item 点击
         *
         * @param bean bean
         */
        void onItemClick(InstructionBean bean);
    }

}
