package com.haircut.pranks.app.uiview;

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.haircut.pranks.app.data.ChildData;
import com.haircut.pranks.app.databinding.FragmentCollectionBinding;
import com.haircut.pranks.app.manager.DataManager;
import com.haircut.pranks.app.manager.RecyclerSpace;
import com.haircut.pranks.app.recyadapter.AdapterCollection;

import java.util.List;


public class FragCollection extends Fragment {

    private AdapterCollection adapter;

    public FragCollection() {
    }

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

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

        return collectBinding.getRoot();
    }

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

    }
}