package com.keyboard.skinning.cool.adpter;


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.keyboard.skinning.cool.data.ClassData;
import com.keyboard.skinning.cool.listener.ClickSeeAllListener;
import com.keyboard.skinning.cool.databinding.ItemMainBinding;
import com.keyboard.skinning.cool.tools.ItemDec;

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

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

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

    private ClickSeeAllListener mCallBack;

    public AdapterParent(Context context, List<ClassData> list) {
        mContext = context;
        this.mList = list;
    }
    public void setClickAction(ClickSeeAllListener 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);
        ItemDec itemDec = new ItemDec(3, 3, 0);
        AdapterChild adapterChild = new AdapterChild(mContext, classData.getKeyboardList().subList(0,9));
        holder.binding.childRecycler.setLayoutManager(new GridLayoutManager(mContext,3));
        holder.binding.childRecycler.setAdapter(adapterChild);
        holder.binding.childRecycler.addItemDecoration(itemDec);
        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;


        }

    }


}
