package com.fun.wave.pranksounds.custom;

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.dataListener.ObjectBoxMan;
import com.fun.wave.pranksounds.dataListener.PrankBean;
import com.fun.pranksounds.databinding.FragmentCustomBinding;
import com.fun.wave.pranksounds.like.AdapterLike;
import com.fun.wave.pranksounds.utils.MyItemSpace;

import java.util.List;


public class FragmentCustom extends Fragment {

    private FragmentCustomBinding binding;
    private AdapterLike adapter;
    public FragmentCustom() {

    }

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


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentCustomBinding.inflate(getLayoutInflater());
        MyItemSpace itemHelper = new MyItemSpace(7, 7, 7);
        binding.recyclerImport.addItemDecoration(itemHelper);
        binding.recyclerImport.setLayoutManager(new GridLayoutManager(requireContext(), 2));
        List<PrankBean> all = ObjectBoxMan.getAllCustomization();
        adapter = new AdapterLike(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);
        }

    }


}