package com.cartoon.wallpapers.live.ui;

import android.os.Bundle;

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

import com.cartoon.wallpapers.live.ada.Main;
import com.cartoon.wallpapers.live.base.BaseFragment;
import com.cartoon.wallpapers.live.databinding.MainFragmentBinding;
import com.cartoon.wallpapers.live.mydata.ResultData;
import com.cartoon.wallpapers.live.utils.Decoration;
import com.cartoon.wallpapers.live.vm.ViewModeMain;

public class MainFragment extends BaseFragment<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() {
        Main main = new Main(requireActivity());
        Vb.recycler.setAdapter(main);
        Vb.recycler.setLayoutManager(new GridLayoutManager(requireContext(),2, RecyclerView.VERTICAL, false));
        Vb.recycler.addItemDecoration(new Decoration(requireContext(),8,8,0));
        viewModeMain = getFragmentScopeViewModel(ViewModeMain.class);
        viewModeMain.getPagingData(wallpaperType).observe(getViewLifecycleOwner(), new Observer<PagingData<ResultData>>() {
            @Override
            public void onChanged(PagingData<ResultData> resultDataPagingData) {
                main.submitData(requireActivity().getLifecycle(),resultDataPagingData);
            }
        });

    }
}