package com.sound.prankparty.ThirdFragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sound.prankparty.R;
import com.sound.prankparty.Room.AppDatabase;
import com.sound.prankparty.Room.FavoriteSounds;
import com.sound.prankparty.Room.FavoriteSoundsDao;
import com.sound.prankparty.Utils.FavoriteSoundsViewModel;
import com.sound.prankparty.Utils.ItemDecoration;

import java.util.List;


public class FavoriteFragment extends Fragment {

    private AppDatabase appDatabase;
    private FavoriteSoundsDao favoriteSoundsDao;
    private List<FavoriteSounds> favoriteSoundsList;
    private RecyclerView recyclerView;
    private FavoriteRecyclerViewAdapter favoriteRecyclerViewAdapter;
    private ImageView tips;
    private ImageView image;
    private FavoriteSoundsViewModel viewModel;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_favorite, container, false);

        // 使用 ApplicationContext 来获取 ViewModel
        viewModel = new ViewModelProvider(this, new ViewModelProvider.AndroidViewModelFactory(getActivity().getApplication())).get(FavoriteSoundsViewModel.class);

        recyclerView = view.findViewById(R.id.favorite_recyclerview);
        tips = view.findViewById(R.id.fragment_favorite_tips);
        image = view.findViewById(R.id.fragment_favorite_image);

        recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 3));

        favoriteRecyclerViewAdapter = new FavoriteRecyclerViewAdapter(requireContext(), this);
        recyclerView.setAdapter(favoriteRecyclerViewAdapter);

        // 为 RecyclerView 添加自定义的间距装饰。
        ItemDecoration itemDecoration = new ItemDecoration(16, 19, 10);
        recyclerView.addItemDecoration(itemDecoration);

        appDatabase = AppDatabase.getDatabase(requireContext());
        favoriteSoundsDao = appDatabase.favoriteSoundsDao();

        // 观察 LiveData 的变化
        viewModel.getFavoriteSoundsLiveData().observe(getViewLifecycleOwner(), favoriteSoundsList -> {
            if (favoriteSoundsList == null || favoriteSoundsList.isEmpty()) {
                recyclerView.setVisibility(View.GONE);
                tips.setVisibility(View.VISIBLE);
                image.setVisibility(View.VISIBLE);
            } else {
                recyclerView.setVisibility(View.VISIBLE);
                tips.setVisibility(View.GONE);
                image.setVisibility(View.GONE);
            }
            favoriteRecyclerViewAdapter.updateData(favoriteSoundsList);
        });

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        // 触发数据刷新
        viewModel.refreshData();
    }
}
