package com.fun.wave.pranksounds.mine;

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

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

import com.fun.wave.pranksounds.collection.CollectionAdapter;
import com.fun.wave.pranksounds.databinding.MineFragmentBinding;
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 MineFragment extends Fragment {

    private MineFragmentBinding binding;
    private CollectionAdapter adapter;
    public MineFragment() {

    }

    public static MineFragment newInstance() {
        MineFragment fragment = new MineFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = MineFragmentBinding.inflate(getLayoutInflater());
        ItemDec itemHelper = new ItemDec(7, 7, 7);
        binding.recyclerImport.addItemDecoration(itemHelper);
        binding.recyclerImport.setLayoutManager(new GridLayoutManager(requireContext(), 2));
        List<PrankBean> all = ObjectBoxMan.getAllCustomization();
        adapter = new CollectionAdapter(requireContext(), all, true);
        updateEmpty(all);
        binding.recyclerImport.setAdapter(adapter);
        return binding.getRoot();
    }


    public void refresh() {
        if (adapter != null) {
            List<PrankBean> all = ObjectBoxMan.getAllCustomization();
            adapter.updateData(all);
            updateEmpty(all);
        }

    }


    @Override
    public void onResume() {
        super.onResume();
        refresh();
    }
    private void updateEmpty(List<PrankBean> data) {
        if (data.isEmpty()) {
            binding.layoutEmpty.setVisibility(View.VISIBLE);
        } else {
            binding.layoutEmpty.setVisibility(View.GONE);
        }

    }


}