package com.keypalette.theme;


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.keypalette.theme.dataabout.resource.WrapperData;
import com.keypalette.theme.databinding.AdapterMainBinding;
import com.keypalette.theme.other.ItemDecoration;
import com.keypalette.theme.other.listener.ListenerClickViewAll;

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

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

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

    private ListenerClickViewAll mCallBack;

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

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

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


        return new MainViewHolder(inflate);
    }

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


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

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

        if (holder.binding.childRecycler.getItemDecorationCount() == 0) {
            ItemDecoration itemDec = new ItemDecoration(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 AdapterMainBinding binding;


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


        }

    }


}
