package com.cartoon.live.wallpaper.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.live.wallpaper.myadapter.AdapterMain;
import com.cartoon.live.wallpaper.parent.BaseFragment;
import com.cartoon.live.wallpaper.mydata.ResultData;
import com.cartoon.live.wallpaper.databinding.FragmentMainBinding;
import com.cartoon.live.wallpaper.utils.ItemDecoration;
import com.cartoon.live.wallpaper.myviewmode.VmMainFragment;

public class HomeFragment extends BaseFragment<FragmentMainBinding> {


    private static final String ARG_PARAM1 = "param1";


    private int wallpaperType;

    private VmMainFragment vmMainFragment;

    public HomeFragment() {

    }
    public static HomeFragment newInstance(int type) {
        HomeFragment fragment = new HomeFragment();
        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 FragmentMainBinding getFragmentVb() {
        return FragmentMainBinding.inflate(getLayoutInflater());
    }

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

    }
}