package com.fun.wave.pranksounds.collection;

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.databinding.CollectionFragmentBinding;
import com.fun.wave.pranksounds.listener.ObjectBoxMan;
import com.fun.wave.pranksounds.listener.PrankBean;
import com.fun.wave.pranksounds.utils.ItemDec;

import java.util.List;


public class CollectionFragment extends Fragment {

    private CollectionAdapter adapter;

    public CollectionFragment() {
    }

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

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

    private CollectionFragmentBinding collectBinding;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        collectBinding = CollectionFragmentBinding.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 CollectionAdapter(requireContext(), all,false);
        ItemDec itemHelper = new ItemDec(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);
        }
    }
}