package com.themes.keyboard.black.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.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.themes.keyboard.black.databinding.ItemMainBinding;
import com.themes.keyboard.black.listener.OnSeeAllListener;
import com.themes.keyboard.black.localdata.ClassData;

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

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

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

    private OnSeeAllListener mCallBack;

    public AdapterMain(Context context, List<ClassData> list) {
        mContext = context;
        this.mList = list;
    }

    public void setClickAction(OnSeeAllListener 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);


        AdapterMainChild adapterMainChild = new AdapterMainChild(mContext, classData.getKeyboardList().subList(0, 6));
        holder.binding.childRecycler.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.HORIZONTAL, false));
        holder.binding.childRecycler.setAdapter(adapterMainChild);

        holder.binding.imAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCallBack.OnClickSeeAll(parentName);
            }
        });
        if (position == (mList.size() - 1)) {
            holder.binding.space.setVisibility(View.VISIBLE);
        } else {
            holder.binding.space.setVisibility(View.GONE);
        }

    }

    @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;


        }

    }


}
