package com.free.video.wallpaper.uipage;

import android.os.Bundle;

import androidx.lifecycle.Observer;
import androidx.paging.PagingData;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.free.video.wallpaper.mylist.MainAdapter;
import com.free.video.wallpaper.baseapp.MyBaseFragment;
import com.free.video.wallpaper.databinding.MainFragmentBinding;
import com.free.video.wallpaper.content.ResultData;
import com.free.video.wallpaper.helper.MyItemDecoration;
import com.free.video.wallpaper.vmhelper.ViewModeMain;

public class MainFragment extends MyBaseFragment<MainFragmentBinding> {


    private static final String ARG_PARAM1 = "param1";


    private int wallpaperType;

    private ViewModeMain viewModeMain;

    public MainFragment() {

    }
    public static MainFragment newInstance(int type) {
        MainFragment fragment = new MainFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_PARAM1, type);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            wallpaperType = getArguments().getInt(ARG_PARAM1);
        }
    }


    @Override
    protected MainFragmentBinding getFragmentVb() {
        return MainFragmentBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView() {
        MainAdapter mainAdapter = new MainAdapter(requireActivity());
        Vb.recycler.setAdapter(mainAdapter);
        Vb.recycler.setLayoutManager(new GridLayoutManager(requireContext(),2, RecyclerView.VERTICAL, false));
        Vb.recycler.addItemDecoration(new MyItemDecoration(requireContext(),5,5,0));
        viewModeMain = getFragmentScopeViewModel(ViewModeMain.class);
        viewModeMain.getPagingData(wallpaperType).observe(getViewLifecycleOwner(), new Observer<PagingData<ResultData>>() {
            @Override
            public void onChanged(PagingData<ResultData> resultDataPagingData) {
                mainAdapter.submitData(requireActivity().getLifecycle(),resultDataPagingData);
            }
        });

    }
}