package com.prank.sounds.fart.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 androidx.recyclerview.widget.RecyclerView;

import com.prank.sounds.fart.bean.Info;
import com.prank.sounds.fart.databinding.FragLikeBinding;
import com.prank.sounds.fart.myadapter.AdapterLike;
import com.prank.sounds.fart.tools.Db;
import com.prank.sounds.fart.tools.MyItemDecoration;

import java.util.List;


public class FragLike extends Fragment {

    private AdapterLike adapter;

    public FragLike() {
    }

    @SuppressWarnings("unused")
    public static FragLike newInstance() {
        FragLike fragment = new FragLike();
        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 FragLikeBinding collectBinding;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        collectBinding = FragLikeBinding.inflate(getLayoutInflater());
        RecyclerView recyclerView = collectBinding.list;
        recyclerView.setLayoutManager(new GridLayoutManager(requireContext(), 2));
        List<Info> all = Db.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 AdapterLike(requireContext(), all,false);
        MyItemDecoration itemHelper = new MyItemDecoration(10, 10, 5);
        recyclerView.addItemDecoration(itemHelper);
        recyclerView.setAdapter(adapter);

        return collectBinding.getRoot();
    }

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