package com.color.keyboard.black.adapter;


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.color.keyboard.black.bean.ClassData;
import com.color.keyboard.black.databinding.ItemMainBinding;
import com.color.keyboard.black.manager.Space;
import com.color.keyboard.black.myapi.ClickAllApi;

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

public class AdapterHome extends RecyclerView.Adapter<AdapterHome.MainViewHolder> {

    private Context mContext;
    private List<ClassData> mList = new ArrayList<>();

    private ClickAllApi mCallBack;

    public AdapterHome(Context context, List<ClassData> list) {
        mContext = context;
        this.mList = list;
    }
    public void setClickAction(ClickAllApi callback) {
        mCallBack = callback;
    }

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


        return new MainViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull MainViewHolder holder, int position) {
        ClassData classData = mList.get(position);
        String parentName = classData.getParentName();
        holder.binding.className.setText(parentName);
        Space space = new Space(3, 3, 0);
        AdapterHomeChild adapterHomeChild = new AdapterHomeChild(mContext, classData.getKeyboardList().subList(0,4));
        holder.binding.childRecycler.setLayoutManager(new GridLayoutManager(mContext,2));
        holder.binding.childRecycler.setAdapter(adapterHomeChild);
        holder.binding.childRecycler.addItemDecoration(space);
        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 ItemMainBinding binding;


        public MainViewHolder(@NonNull ItemMainBinding itemView) {
            super(itemView.getRoot());
            binding = itemView;


        }

    }


}
