package com.zztrapp.zztrapp.ui.index.adapter;

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

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

import com.zztrapp.zztrapp.bean.IndexBean;
import com.zztrapp.zztrapp.databinding.ItemRvIndexBinding;

import java.util.List;

/**
 * Describe 首页列表
 * Created by Bby on 2022/6/22
 * Email bby0856@163.com
 */

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

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

    public interface OnItemClickListener {
        void onLookDetailClick(int type, String name);
    }

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


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

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

    @NonNull
    @Override
    public ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemRvIndexBinding binding = ItemRvIndexBinding.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());

        holder.bindings.rv.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        IndexChildAdapter adapter = new IndexChildAdapter(mContext);
        holder.bindings.rv.setAdapter(adapter);
        adapter.setData(list.get(position).getValueList(), list.get(position).getType());
        adapter.setOnItemClickListener(new IndexChildAdapter.OnItemClickListener() {
            @Override
            public void onLookDetailClick(int type, String name) {
                if (onItemClickListener != null) {
                    onItemClickListener.onLookDetailClick(type, name);
                }
            }
        });
    }

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

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

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