package com.fun.wave.pranksounds.like;

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

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

import com.fun.wave.pranksounds.utils.MyItemSpace;
import com.fun.wave.pranksounds.dataListener.ObjectBoxMan;
import com.fun.wave.pranksounds.dataListener.PrankBean;
import com.fun.pranksounds.databinding.FragmentLikeBinding;

import java.util.List;


public class FragmentLike extends Fragment {

    private AdapterLike adapter;

    public FragmentLike() {
    }

    @SuppressWarnings("unused")
    public static FragmentLike newInstance() {
        FragmentLike fragment = new FragmentLike();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    private FragmentLikeBinding collectBinding;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        collectBinding = FragmentLikeBinding.inflate(getLayoutInflater());
        RecyclerView recyclerView = collectBinding.list;
        recyclerView.setLayoutManager(new GridLayoutManager(requireContext(), 2));
        List<PrankBean> all = ObjectBoxMan.getAllLike();
        if(all.size()==0){
            collectBinding.layoutEmpty.setVisibility(View.VISIBLE);
            collectBinding.list.setVisibility(View.GONE);
        }else {
            collectBinding.layoutEmpty.setVisibility(View.GONE);
            collectBinding.list.setVisibility(View.VISIBLE);
        }
        adapter = new AdapterLike(requireContext(), all,false);
        MyItemSpace itemHelper = new MyItemSpace(7, 7, 7);
        recyclerView.addItemDecoration(itemHelper);
        recyclerView.setAdapter(adapter);

        return collectBinding.getRoot();
    }


    @Override
    public void onResume() {
        super.onResume();
        Log.d("------------------","-------like--onResume");
        refresh();
    }

    public void refresh() {
        if (adapter != null) {
            List<PrankBean> all = ObjectBoxMan.getAllLike();
            if(all.size()==0){
                collectBinding.layoutEmpty.setVisibility(View.VISIBLE);
                collectBinding.list.setVisibility(View.GONE);
            }else {
                collectBinding.layoutEmpty.setVisibility(View.GONE);
                collectBinding.list.setVisibility(View.VISIBLE);
            }
            adapter.updateData(all);
        }
    }
}