package com.app.input.breeze.board.listutils;


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.breeze.board.bean.CategoryWrapper;
import com.app.input.breeze.board.callback.ViewAllClickListener;
import com.app.input.breeze.board.databinding.HomeAdapterItemBinding;
import com.app.input.breeze.board.utils.GridItemDecoration;

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

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

    private ViewAllClickListener mCallBack;

    public CategoryListAdapter(Context context, List<CategoryWrapper> list) {
        mContext = context;
        this.mList = list;
    }
    public void setClickAction(ViewAllClickListener callback) {
        mCallBack = callback;
    }

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

    @Override
    public void onBindViewHolder(@NonNull MainViewHolder holder, int position) {
        CategoryWrapper CategoryWrapper = mList.get(position);
        String parentName = CategoryWrapper.getParentName();
        holder.binding.className.setText(parentName);
        GridItemDecoration GridItemDecoration = new GridItemDecoration(3, 3, 0);
        ThemeGridAdapter ThemeGridAdapter = new ThemeGridAdapter(mContext, CategoryWrapper.getKeyboardList().subList(0, 6));
        holder.binding.childRecycler.setLayoutManager(new GridLayoutManager(mContext, 3));
        holder.binding.childRecycler.setAdapter(ThemeGridAdapter);
        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 HomeAdapterItemBinding binding;
        public MainViewHolder(@NonNull HomeAdapterItemBinding itemView) {
            super(itemView.getRoot());
            binding = itemView;

        }
    }
}
