package com.fun.airhornprank.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;

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

import com.fun.airhornprank.R;
import com.fun.airhornprank.adapter.LikeAdapter;
import com.fun.airhornprank.data.MyDataBase;
import com.fun.airhornprank.data.Mydata;
import com.fun.airhornprank.databinding.FragmentLikeBinding;
import com.fun.airhornprank.tool.Mytools;

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


public class LikeFragment extends Fragment {

    FragmentLikeBinding binding;

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

        initData();

        return binding.getRoot();
    }

    private void initData() {
        MyDataBase.getInstance().likeDAO().getLikedata(true).observe(getViewLifecycleOwner(), new Observer<List<Mydata>>() {
            @Override
            public void onChanged(List<Mydata> mydata) {
                if (mydata.isEmpty()) {
                    binding.likeHintText.setVisibility(View.VISIBLE);
                    binding.likeHintImage.setVisibility(View.VISIBLE);
                    binding.likeRv.setVisibility(View.GONE);
                } else {
                    binding.likeRv.setVisibility(View.VISIBLE);
                    binding.likeHintImage.setVisibility(View.GONE);
                    binding.likeHintText.setVisibility(View.GONE);
                    binding.likeRv.setAdapter(new LikeAdapter(requireActivity(), mydata));
                    binding.likeRv.setLayoutManager(new LinearLayoutManager(requireContext()));
                }
            }
        });
    }
}