package com.sound.prankparty.Fragment;

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

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.GridLayoutManager;

import com.sound.prankparty.R;
import com.sound.prankparty.Room.AppDatabase;
import com.sound.prankparty.Room.FavoriteSoundsDao;
import com.sound.prankparty.Adapter.FavoriteRecyclerViewAdapter;
import com.sound.prankparty.Utils.FavoriteSoundsViewModel;
import com.sound.prankparty.Utils.ItemDecoration;
import com.sound.prankparty.databinding.FragmentFavoriteBinding;

public class FavoriteFragment extends Fragment {

    private AppDatabase appDatabase;
    private FavoriteSoundsDao favoriteSoundsDao;
    private FavoriteRecyclerViewAdapter favoriteRecyclerViewAdapter;
    private FavoriteSoundsViewModel viewModel;
    private FragmentFavoriteBinding binding;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentFavoriteBinding.inflate(inflater, container, false);
        View view = binding.getRoot();

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

        binding.favoriteRecyclerview.setLayoutManager(new GridLayoutManager(getContext(), 3));

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

        binding.favoriteRecyclerview.setItemAnimator(null); // 关闭默认动画


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

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

        // 观察 LiveData 的变化
        viewModel.getFavoriteSoundsLiveData().observe(getViewLifecycleOwner(), favoriteSoundsList -> {
            if (favoriteSoundsList == null || favoriteSoundsList.isEmpty()) {
                binding.favoriteRecyclerview.setVisibility(View.GONE);
                binding.fragmentFavoriteTips.setVisibility(View.VISIBLE);
                binding.fragmentFavoriteImage.setVisibility(View.VISIBLE);
            } else {
                binding.favoriteRecyclerview.setVisibility(View.VISIBLE);
                binding.fragmentFavoriteTips.setVisibility(View.GONE);
                binding.fragmentFavoriteImage.setVisibility(View.GONE);
            }

            favoriteRecyclerViewAdapter.updateData(favoriteSoundsList);
        });

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}
