package com.app.input.inputaura.onjfsgvfsf;

import android.content.Context;
import android.graphics.Rect;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.app.input.inputaura.R;
import com.app.input.inputaura.dasda.Inhj;
import com.app.input.inputaura.dasda.Shikamaru;
import com.app.input.inputaura.bgtng.TngOui;
import com.app.input.inputaura.databinding.RsadasdasBinding;

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

public class EubDnb extends RecyclerView.Adapter<EubDnb.MainViewHolder> {
    private Context mContext;
    private List<Inhj> mList = new ArrayList<>();
    private TngOui mCallBack;

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

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

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

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

        // 获取前6个数据
        List<Shikamaru> displayList = inhj.getKeyboardList();
        int displayCount = Math.min(displayList.size(), 6);
        List<Shikamaru> subList = displayList.subList(0, displayCount);

        // 设置横向布局管理器
        LinearLayoutManager layoutManager = new LinearLayoutManager(
                mContext,
                LinearLayoutManager.HORIZONTAL,
                false
        );

        // 创建适配器
        UegYf uegYf = new UegYf(mContext, subList);

        // 移除旧的装饰器
        if (holder.binding.childRecycler.getItemDecorationCount() > 0) {
            holder.binding.childRecycler.removeItemDecorationAt(0);
        }

        // 添加新的间距装饰器
        int spacing = mContext.getResources().getDimensionPixelSize(R.dimen.item_spacing_small);
        holder.binding.childRecycler.addItemDecoration(new HorizontalItemDecoration(spacing));

        // 设置适配器和布局管理器
        holder.binding.childRecycler.setLayoutManager(layoutManager);
        holder.binding.childRecycler.setAdapter(uegYf);

        // 禁用滑动监听（去掉提示图标）
        // holder.binding.childRecycler.clearOnScrollListeners();

        // 查看全部按钮点击事件
        holder.binding.tvAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCallBack != null) {
                    mCallBack.OnClickSeeAll(parentName);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public static class MainViewHolder extends RecyclerView.ViewHolder {
        private RsadasdasBinding binding;
        public MainViewHolder(@NonNull RsadasdasBinding itemView) {
            super(itemView.getRoot());
            binding = itemView;
        }
    }

    // 自定义横向间距装饰器（只设置右间距）
    public static class HorizontalItemDecoration extends RecyclerView.ItemDecoration {
        private final int spacing;

        public HorizontalItemDecoration(int spacing) {
            this.spacing = spacing;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            int position = parent.getChildAdapterPosition(view);
            int itemCount = parent.getAdapter().getItemCount();

            // 每个item都设置右间距，最后一个除外
            if (position < itemCount - 1) {
                outRect.right = spacing;
            } else {
                outRect.right = 0; // 最后一个item不要右边距
            }
        }
    }
}