package com.prank.master.ui;

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 androidx.recyclerview.widget.RecyclerView;

import com.prank.master.adapter.likeAdapter;
import com.prank.master.databinding.FragmentLikeBinding;
import com.prank.master.db.Bean;
import com.prank.master.db.ObManager;

import java.util.List;


public class LikeFragment extends Fragment {

    private likeAdapter adapter;

    public LikeFragment() {
    }

    @SuppressWarnings("unused")
    public static LikeFragment newInstance() {
        LikeFragment fragment = new LikeFragment();
        Bundle args = new Bundle();
//        args.putInt(ARG_COLUMN_COUNT, columnCount);
        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<Bean> all = ObManager.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 likeAdapter(requireContext(), all);
        recyclerView.setAdapter(adapter);

        return collectBinding.getRoot();
    }

    public void refresh() {
        if (adapter != null) {
            List<Bean> all = ObManager.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);
        }

    }
}