package aicare.net.cn.sdk.ailinksdkdemoandroid.home;

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;

/**
 * 说明：
 *
 * @author yesp
 * 2024/1/5
 */
public class HomeListAdapter extends RecyclerView.Adapter<HomeListAdapter.MyHolder> {

    private Context context;
    private ArrayList<JumpBean> list;

    private ItemClickListener itemClickListener;

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

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

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

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

    @Override
    public void onBindViewHolder(@NonNull MyHolder holder, int position) {
        JumpBean jumpBean = list.get(position);
        String cidStr = "";
        if (jumpBean.getCid() > 0) {
            cidStr = "(" + jumpBean.getCidStr() + ")";
        }
        holder.tvContent.setText(context.getString(jumpBean.getItemNameResId()) + cidStr);
    }

    @Override
    public int getItemCount() {
        if (list != null && list.size() > 0) {
            return list.size();
        } else {
            return 0;
        }
    }

    class MyHolder extends RecyclerView.ViewHolder {
        private TextView tvContent;

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

    }

    public interface ItemClickListener {

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