package com.sound.prankparty.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

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

import com.sound.prankparty.JSON.Category;
import com.sound.prankparty.JSON.SoundItem;
import com.sound.prankparty.R;

import java.util.List;

public class Main2RecyclerViewAdapter extends RecyclerView.Adapter<Main2RecyclerViewAdapter.Main2ViewHolder> {

    private Context context;
    private List<Category> categoryList;

    public Main2RecyclerViewAdapter(Context context, List<Category> categoryList) {
        this.context = context;
        this.categoryList = categoryList;
    }

    @NonNull
    @Override
    public Main2ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_main2, parent, false);
        return new Main2ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull Main2ViewHolder holder, int position) {
        // 获取当前的 Category
        Category category = categoryList.get(position);

        // 设置Category相关的文字
        holder.categoryName.setText(category.getCategoryName());

        // 设置SoundItem的RecyclerView
        List<SoundItem> soundItemList = category.getSoundItemList();  // 获取 Category 下的 SoundItem 列表
        SubSoundItemRecyclerViewAdapter soundItemAdapter = new SubSoundItemRecyclerViewAdapter(context, soundItemList, category.getCategoryName());
        holder.soundItemRecyclerView.setAdapter(soundItemAdapter);
    }

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

    static class Main2ViewHolder extends RecyclerView.ViewHolder {

        TextView categoryName;
        RecyclerView soundItemRecyclerView;

        public Main2ViewHolder(@NonNull View itemView) {
            super(itemView);

            categoryName = itemView.findViewById(R.id.item_textview_main2);
            soundItemRecyclerView = itemView.findViewById(R.id.sound_item_recyclerview);

            // 初始化嵌套的RecyclerView
            soundItemRecyclerView.setLayoutManager(new LinearLayoutManager(itemView.getContext(), LinearLayoutManager.HORIZONTAL, false));
        }
    }
}
