package com.app.keyboard.colorful.ada;


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.keyboard.colorful.databinding.ItemMainBinding;
import com.app.keyboard.colorful.colorlistener.COnSeeAllListener;
import com.app.keyboard.colorful.data.ClassBean;
import com.app.keyboard.colorful.helper.ItemDec;

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

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

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

    private COnSeeAllListener mCallBack;

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

    public void setClickAction(COnSeeAllListener 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) {
        ClassBean classBean = mList.get(position);
        String parentName = classBean.getParentName();
        holder.binding.className.setText(parentName);
        AdaChild adaChild = new AdaChild(mContext, classBean.getKeyboardList().subList(0, 6));
        holder.binding.childRecycler.setAdapter(adaChild);

        if (holder.binding.childRecycler.getLayoutManager() == null) {
            holder.binding.childRecycler.setLayoutManager(new GridLayoutManager(mContext, 2));
        }
        if (holder.binding.childRecycler.getItemDecorationCount() == 0) {
            ItemDec itemDec = new ItemDec(10, 10, 4);
            holder.binding.childRecycler.addItemDecoration(itemDec);
        }
        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;


        }

    }


}
