package com.keyboard.keyboardtheme.skin.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.keyboard.keyboardtheme.skin.databinding.AdaParentBinding;
import com.keyboard.keyboardtheme.skin.datamanager.localdata.ParentData;
import com.keyboard.keyboardtheme.skin.utils.OnViewAll;
import com.keyboard.keyboardtheme.skin.utils.RecyclerDec;

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

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

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

    private OnViewAll mCallBack;

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

    public void setClickAction(OnViewAll callback) {
        mCallBack = callback;
    }

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


        return new MainViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull MainViewHolder holder, int position) {
        ParentData ParentData = mList.get(position);
        String parentName = ParentData.getParentName();
        holder.binding.className.setText(parentName);


        AdaChild adaChild = new AdaChild(mContext, ParentData.getKeyboardList().subList(0, 4));
        holder.binding.childRecycler.setHasFixedSize(true); // 可选：提高性能
        holder.binding.childRecycler.setNestedScrollingEnabled(false); // 禁用嵌套滑动
        holder.binding.childRecycler.setAdapter(adaChild);
        if (holder.binding.childRecycler.getLayoutManager() == null) {

            GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, 2);
            holder.binding.childRecycler.setLayoutManager(gridLayoutManager);
        }

        if (holder.binding.childRecycler.getItemDecorationCount() == 0) {
            RecyclerDec itemDec = new RecyclerDec(15, 5, 0);
            holder.binding.childRecycler.addItemDecoration(itemDec);
        }
        holder.binding.viewAll.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 AdaParentBinding binding;


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


        }

    }


}
