package com.paint.trace.drawing.frag;

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.paint.trace.drawing.item.LikeAdapter;
import com.paint.trace.drawing.databinding.FragmentFavoriteBinding;

import com.paint.trace.drawing.other.AssetData;
import com.paint.trace.drawing.db.ObjectBox;
import com.paint.trace.drawing.alllistener.OnLikeListener;

import java.util.List;

public class FragmentLike extends Fragment {
    private FragmentFavoriteBinding binding;

    public FragmentLike() {

    }
    public static FragmentLike newInstance() {
        FragmentLike fragment = new FragmentLike();
        return fragment;
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentFavoriteBinding.inflate(getLayoutInflater());
        onInit();
        return binding.getRoot();
    }

    private void onInit(){
        List<AssetData> assetData = ObjectBox.queryAllLike();
        LikeAdapter likeAdapter = new LikeAdapter(requireActivity());
        likeAdapter.setData(assetData);
        updateUi(assetData);

        binding.recyclerview.setAdapter(likeAdapter);
        binding.recyclerview.setLayoutManager(new GridLayoutManager(requireContext(), 3));


        ObjectBox.addListener(new OnLikeListener() {
            @Override
            public void onAddLike() {
                List<AssetData> assetData = ObjectBox.queryAllLike();
                likeAdapter.setData(assetData);
                updateUi(assetData);
            }

            @Override
            public void onDeleteLike() {
                List<AssetData> assetData = ObjectBox.queryAllLike();
                likeAdapter.setData(assetData);
                updateUi(assetData);
            }
        });
    }

    private void updateUi(List<AssetData> assetData){
        if(assetData.size()>0){
            binding.recyclerview.setVisibility(View.VISIBLE);
            binding.layoutNoFavorite.setVisibility(View.GONE);
        }else {
            binding.recyclerview.setVisibility(View.GONE);
            binding.layoutNoFavorite.setVisibility(View.VISIBLE);
        }

    }
}