package com.offline.music.player.ui.fragmnt;

import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.offline.music.player.adapter.A_InstrumentAdapter;
import com.offline.music.player.adapter.A_NatureAdapter;
import com.offline.music.player.adapter.A_WhiteAdapter;
import com.offline.music.player.databinding.FragmentAHomeBinding;
import com.offline.music.player.helper.ItemDecoration;
import com.offline.music.player.ui.fragmnt.viewmodel.A_VMHome;


public class A_HomeFragment extends BaseFragment<FragmentAHomeBinding> {

    private A_InstrumentAdapter instrumentAdapter;
    private A_WhiteAdapter whiteAdapter;
    private A_NatureAdapter natureAdapter;

    @Override
    protected FragmentAHomeBinding getFragmentVb() {
        return FragmentAHomeBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView() {

        initData();

        initEvent();

    }

    public void initData() {
        // 设置 LayoutManager
        Vb.recyclerViewFirst.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
        Vb.recyclerViewSecond.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
        Vb.recyclerViewThird.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));

        // 初始化适配器
        instrumentAdapter = new A_InstrumentAdapter(requireActivity());
        whiteAdapter = new A_WhiteAdapter(requireActivity());
        natureAdapter = new A_NatureAdapter(requireActivity());

        // 设置适配器到 RecyclerView
        Vb.recyclerViewFirst.setAdapter(instrumentAdapter);
        Vb.recyclerViewSecond.setAdapter(whiteAdapter);
        Vb.recyclerViewThird.setAdapter(natureAdapter);

        ItemDecoration itemDecoration = new ItemDecoration(12, 10, 9);
        Vb.recyclerViewSecond.addItemDecoration(itemDecoration);
    }

    public void initEvent() {
        // 观察 ViewModel 中的 LiveData
        A_VMHome viewModel = new ViewModelProvider(this).get(A_VMHome.class);

        viewModel.getSoundOfInstrumentList().observe(getViewLifecycleOwner(), audioItems -> {
            instrumentAdapter.updateData(audioItems);
        });

        viewModel.getWhiteNoiseList().observe(getViewLifecycleOwner(), audioItems -> {
            whiteAdapter.updateData(audioItems);
        });

        viewModel.getVoiceOfNatureList().observe(getViewLifecycleOwner(), audioItems -> {
            natureAdapter.updateData(audioItems);
        });
    }

}
