package com.app.input.personalised.board.listhelpers;


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.personalised.board.databean.ThemeCategory;
import com.app.input.personalised.board.itemback.ViewAllListener;
import com.app.input.personalised.board.databinding.ItemHomeSectionHeaderBinding;
import com.app.input.personalised.board.helpers.GridItemDecoration;

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

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

    private ViewAllListener mCallBack;

    public HomeSectionAdapter(Context context, List<ThemeCategory> list) {
        mContext = context;
        this.mList = list;
    }
    public void setClickAction(ViewAllListener callback) {
        mCallBack = callback;
    }

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

    @Override
    public void onBindViewHolder(@NonNull MainViewHolder holder, int position) {
        ThemeCategory beanWrapper = mList.get(position);
        String parentName = beanWrapper.getParentName();
        holder.binding.className.setText(parentName);
        GridItemDecoration gridItemDecoration = new GridItemDecoration(3, 3, 0);
        ThemePreviewAdapter themePreviewAdapter = new ThemePreviewAdapter(mContext, beanWrapper.getKeyboardList().subList(0, 6));
        holder.binding.childRecycler.setLayoutManager(new GridLayoutManager(mContext, 3));
        holder.binding.childRecycler.setAdapter(themePreviewAdapter);
        if (holder.binding.childRecycler.getItemDecorationCount() <= 0) {
            holder.binding.childRecycler.addItemDecoration(gridItemDecoration);
        }
        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 ItemHomeSectionHeaderBinding binding;
        public MainViewHolder(@NonNull ItemHomeSectionHeaderBinding itemView) {
            super(itemView.getRoot());
            binding = itemView;

        }
    }
}
