package com.live.mylivewallpaper.fragment;

import android.os.Bundle;

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

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.live.mylivewallpaper.R;
import com.live.mylivewallpaper.adapter.MainAdapter;
import com.live.mylivewallpaper.base.BaseFragment;
import com.live.mylivewallpaper.data.ResultData;
import com.live.mylivewallpaper.databinding.FragmentMainBinding;
import com.live.mylivewallpaper.help.Common;
import com.live.mylivewallpaper.help.Db;
import com.live.mylivewallpaper.help.ItemDecoration;
import com.live.mylivewallpaper.viewmode.VmMainFragment;

import java.util.List;

public class MainFragment extends BaseFragment<FragmentMainBinding> {


    private static final String ARG_PARAM1 = "param1";


    private int wallpaperType;

    private VmMainFragment vmMainFragment;

    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 FragmentMainBinding getFragmentVb() {
        return FragmentMainBinding.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 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) {
                mainAdapter.submitData(requireActivity().getLifecycle(),resultDataPagingData);
            }
        });

    }
}