package com.app.intelligent.board.uiadapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

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

import com.app.intelligent.board.uimodel.KeyboardItemDataWrapper;
import com.app.intelligent.board.uimodel.KeyboardItemInfo;
import com.app.intelligent.board.uilistener.KeyboardViewExpandListener;
import com.app.intelligent.board.databinding.AdapterHomeItemBinding;
import com.app.intelligent.board.utils.KeyboardRvItemDecoration;

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

public class HomeItemAdapter extends RecyclerView.Adapter<HomeItemAdapter.MainViewHolder> {
    private Context mContext;
    private List<KeyboardItemDataWrapper> mList = new ArrayList<>();
    private KeyboardViewExpandListener mCallBack;

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

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

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

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

        KeyboardRvItemDecoration listDecoration = new KeyboardRvItemDecoration(8, 8, 0);
        // 核心修复：将List<?>转为HomeChildAdapter需要的List<BeanDetails>
        List<KeyboardItemInfo> keyboardList = new ArrayList<>();
        if (beanWrapper.getKeyboardList() != null) {
            // 强转成目标类型（匹配HomeChildAdapter的构造函数）
            for (Object item : beanWrapper.getKeyboardList()) {
                if (item instanceof KeyboardItemInfo) {
                    keyboardList.add((KeyboardItemInfo) item);
                }
            }
        }

        // 传入正确类型的List<BeanDetails>
        HomeItemSubAdapter homeChildAdapter = new HomeItemSubAdapter(mContext, keyboardList);

        // 核心修改：自定义GridLayoutManager，强制子项高度为80dp（可调整）
        holder.binding.childRecycler.setLayoutManager(new GridLayoutManager(mContext, 1) {
            @Override
            public RecyclerView.LayoutParams generateDefaultLayoutParams() {
                RecyclerView.LayoutParams params = super.generateDefaultLayoutParams();
                // 将80dp转为像素值（关键：和adapter_home_child.xml的高度一致）
                int targetHeight = (int) (240 * mContext.getResources().getDisplayMetrics().density);
                params.height = targetHeight;
                return params;
            }
        });

        holder.binding.childRecycler.setAdapter(homeChildAdapter);

        if (holder.binding.childRecycler.getItemDecorationCount() == 0) {
            holder.binding.childRecycler.addItemDecoration(listDecoration);
        }

        holder.binding.tvAll.setOnClickListener(v -> {
            if (mCallBack != null) {
                mCallBack.OnClickSeeAll(parentName);
            }
        });
    }

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

    public void refreshData(List<KeyboardItemDataWrapper> newList) {
        this.mList.clear();
        this.mList.addAll(newList);
        notifyDataSetChanged();
    }

    public static class MainViewHolder extends RecyclerView.ViewHolder {
        private AdapterHomeItemBinding binding;

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