package com.blue.running.keyboard.run_adapter;


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.blue.running.keyboard.run_jsonbean.ClassData;
import com.blue.running.keyboard.databinding.ItemMainBinding;
import com.blue.running.keyboard.run_manager.ListSpace;
import com.blue.running.keyboard.run_api.ApiClickList;

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

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

    public void setClickAction(ApiClickList 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);
        ListSpace listSpace = new ListSpace(5, 5, 3);
        boolean b = position == (mList.size() - 1);
        AdapterMainChild adapterMainChild = new AdapterMainChild(mContext, classData.getKeyboardList().subList(0, 4),b );
        holder.binding.childRecycler.setLayoutManager(new GridLayoutManager(mContext, 2));
        holder.binding.childRecycler.setAdapter(adapterMainChild);
        holder.binding.childRecycler.addItemDecoration(listSpace);
        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 ItemMainBinding binding;


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


        }

    }


}
