package com.shiny.keyboard.recycler;


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.shiny.keyboard.bean.ClassData;
import com.shiny.keyboard.databinding.ListMainBinding;
import com.shiny.keyboard.helper.ItemDec;
import com.shiny.keyboard.mylistener.ClickSeeAllListener;

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 ClickSeeAllListener mCallBack;

    public AdapterHome(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) {
        ListMainBinding inflate = ListMainBinding.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,4));
        holder.binding.childRecycler.setLayoutManager(new GridLayoutManager(mContext,2));
        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 ListMainBinding binding;


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


        }

    }


}
