package com.app.input.color.board.listutils;


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

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

import com.app.input.color.board.bean.BeanWrapper;
import com.app.input.color.board.callback.ViewAllCallback;
import com.app.input.color.board.databinding.AdapterHomeBinding;
import com.app.input.color.board.utils.ListDecoration;

import java.util.ArrayList;
import java.util.List;

public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.MainViewHolder> {
    private Context mContext;
    private List<BeanWrapper> mList = new ArrayList<>();

    private ViewAllCallback mCallBack;

    public HomeAdapter(Context context, List<BeanWrapper> list) {
        mContext = context;
        this.mList = list;
    }
    public void setClickAction(ViewAllCallback callback) {
        mCallBack = callback;
    }

    @NonNull
    @Override
    public MainViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        AdapterHomeBinding inflate = AdapterHomeBinding.inflate(LayoutInflater.from(parent.getContext()));
        return new MainViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull MainViewHolder holder, int position) {
        BeanWrapper beanWrapper = mList.get(position);
        String parentName = beanWrapper.getParentName();
        holder.binding.className.setText(parentName);
        ListDecoration listDecoration = new ListDecoration(3, 3, 0);
        HomeChildAdapter homeChildAdapter = new HomeChildAdapter(mContext, beanWrapper.getKeyboardList().subList(0, 6));
        holder.binding.childRecycler.setLayoutManager(new GridLayoutManager(mContext, 3));
        holder.binding.childRecycler.setAdapter(homeChildAdapter);
        if (holder.binding.childRecycler.getItemDecorationCount() <= 0) {
            holder.binding.childRecycler.addItemDecoration(listDecoration);
        }
        holder.binding.tvAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCallBack.OnClickSeeAll(parentName);
            }
        });
    }
    @Override
    public int getItemCount() {
        return mList.size();
    }

    public static class MainViewHolder extends RecyclerView.ViewHolder {
        private AdapterHomeBinding binding;
        public MainViewHolder(@NonNull AdapterHomeBinding itemView) {
            super(itemView.getRoot());
            binding = itemView;

        }
    }
}
